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"));