Create a database first, for example: createtablebcctphoto (photoidintprimarykeyauto_increment, photonamevarchar (50) NOTNULL, photoblob); 2 set show. place jsp in any directory of tomcat. show. jsp function: Read blob from the database and generate imagejpg. show. the jsp file is as follows: % @ pagec
Create a database first, for example, create table bcctphoto (photoid int primary key auto_increment, photoname varchar (50) not null, photo blob); 2 sets show. place jsp in any directory of tomcat. show. jsp function: Read blob from the database and generate image/jpg. show. the jsp file is as follows: % @ page c
Create a database first
For example:
Create table bcctphoto (
Photoid int primary key auto_increment,
Photoname varchar (50) not null,
Photo blob
);
<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/bigdate? User = root & password = mysqladmin ";
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:
Image Testing
Exception Handling: If getOutputStream () has already been called for this response occurs
Exception parsing: The objects used in jsp are released. response. getWriter () is called because this method conflicts with response. getOutputStream! Therefore, the above exception occurs. Then, of course, we need to propose a solution, which is actually quite simple. After using the output stream, we can call the following two lines of code:
Solution: Add code
- Out. clear ();
- Out = pageContext. pushBody ();
The following code:
// Pay attention to the usage of the following statements:
Outs. flush ();
Outs. close ();
Outs = null;
Response. flushBuffer ();
Out. clear ();
Out = pageContext. pushBody ();