Display the image read by mysql on the jsp page

Source: Internet
Author: User
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









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

  1. Out. clear ();
  2. 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 ();
  • 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.