The character stream of a JPG file read from the database, converted to a picture display

Source: Internet
Author: User
Tags int size query tostring
Data | database | display | conversion

The character stream of a JPG file read from the database, converted to a picture showing the relevant code on the page

public void DoPost (HttpServletRequest req, httpservletresponse Res)
Throws Servletexception, IOException {
Servletoutputstream out = Res.getoutputstream ();
Statement dispstmt = null, setstmt = NULL;
try {
dispstmt = Con.createstatement ();
setstmt = Con.createstatement ();
Setstmt.executeupdate ("Set TEXTSIZE 2048000");
catch (Exception e) {
Out.println ("Create Statement Error:" + e.tostring () + "<br>");
}

String ls_sql = null;
Ls_sql = Req.getparameter ("Imagesql");
if (Ls_sql = null)
Ls_sql = "";
Ls_sql = Ls_sql.trim ();
if (!ls_sql.equals ("")) {
ResultSet rs = null;
try {
rs = Dispstmt.executequery (ls_sql);
catch (Exception e) {
System.out.println (
"Unable to Exec statment" + e.tostring () + "<br>");
}
try {
while (Rs.next ()) {
try {
Res.setcontenttype ("Image/jpeg");
InputStream is = Rs.getbinarystream (1);
int size = is.available ();
byte[] BZP = new Byte[size];
Is.read (BZP);
Out.write (BZP);
catch (Exception e) {
System.out.println ("Wirte image error:" + e.tostring ());
}
}
Rs.close ();
catch (Exception e) {
System.out.println ("Unable to close statment" + e.tostring ());
}
}
try {
Dispstmt.close ();
catch (Exception e) {
System.out.println ("Close Statement Error:" + e.tostring ());
}
Out.close ();
}

-------------------------------------------------------

One more:

Read the pictures from the library to show the main source on the page
<%@ page contenttype= "Text/html;charset=iso8859_1"%>
<%@ page import= "Java.sql.ResultSet"%>
<jsp:usebean id= "Query" class= "pub. Makedb "/>
<%
String sql = "SELECT * from Images";
ResultSet rs = query.executequery (SQL);
Rs.next ();
InputStream in = Rs.getbinarystream ("image");
Servletoutputstream SOS = Response.getoutputstream ();
int len = 0;
Byte[] B = new byte[1024];
Response.reset ();
Response.setcontenttype ("Image/gif");
while (len = In.read (b)) > 0)
Sos.write (B,0,len);
In.close ();
Sos.close ();
Query.cls ();
%>

If it's written to a file, just turn SOS
Defined as DataOutputStream SOS = new DataOutputStream (New FileOutputStream ("/a.gif"));



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.