Environment mysql + tomcat:
<1> Create the following table in mysql and insert the image.
The mysql. SQL file is as follows:
Create table photo (
Photo_no int (6) unsigned not null auto_increment,
Image blob,
Primary key ('photo _ no ')
)
<2> place show. jsp in any tomcat directory. show. jsp: Read blob from the database and generate image/jpg.
The show. jsp file is as follows:
<% @ Page contentType = "text/html; charset = gbk" %>
<% @ Page import = "java. io. *" %>
<% @ Page import = "java. SQL. *, javax. SQL. *" %>
<% @ Page import = "java. util. *" %>
<% @ Page import = "java. math. *" %>
<%
String photo_no = request. getParameter ("photo_no ");
// Mysql connection
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
String URL = "jdbc: mysql: // localhost: 3306/job? User = root & password = 111111 ";
Connection con = DriverManager. getConnection (URL );
// Oracle connection
// String URL = "jdbc: oracle: thin @ localhost: 1521: orcl2 ";
// User = "system ";
// Password = "manager ";
// Connection con = DriverManager. getConnection (URL, user, password );
Try {
// Prepare the statement execution object
Statement stmt = con. createStatement ();
String SQL = "SELECT * FROM PHOTO WHERE photo_no =" + photo_no;
ResultSet rs = stmt.exe cuteQuery (SQL );
If (rs. next ()){
Blob B = rs. getBlob ("photo_image ");
Long size = B. length ();
// Out. print (size );
Byte [] bs = B. getBytes (1, (int) size );
Response. setContentType ("image/jpeg ");
OutputStream outs = response. getOutputStream ();
Outs. write (bs );
Outs. flush ();
Rs. close ();
}
Else {
Rs. close ();
Response. sendRedirect ("./images/error.gif ");
}
}
Finally {
Con. close ();
}
%>
<3> place the following file in the same directory of show. jsp.
The index.html file is as follows:
<HTML>
<HEAD>
<TITLE> image testing </TITLE>
</HEAD>
<BODY>
<TABLE>
<TR>
<TD> image test </TD>
</TR>
<TR>
<TD> </TD>
</TR>
</TABLE>
</BODY>
</HTML>