How to solve the JSP display picture problem

Source: Internet
Author: User
Tags stmt

Prerequisite: Use JSP to display the picture. The location of the picture is stored in the database.

Methods: The database was connected with JDBC, and the data was read from the database and output to the page with the output stream.

<%@ page contenttype= "text/html" language= "java"%>
<%@ page buffer= "16kb"%>
<%@ page import= "Java.sql.*"%>
<%@ page import= "java.io.*"%>
 <% int len = 10 * 1024 * 1024;
Class.forName ("Oracle.jdbc.driver.OracleDriver"). newinstance ();
String Url= "Jdbc:oracle:thin:@10.168.8.99:1521:orafy";//orcl for your database Sid String user= "Lhzy";
 string password= "QWERTYUIOP";
Connection conn= drivermanager.getconnection (Url,user,password);
Statement stmt=conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);
String sql= "select pic from test";
ResultSet rs=stmt.executequery (SQL);//Navigate to record Rs.next ();
InputStream in = Rs.getbinarystream (1);//①
Response.reset ();//returns the location marked in the stream
Response.setcontenttype (" Image/jpg "); or GIF etc//Get input stream
OutputStream toclient = Response.getoutputstream ();//②
byte[] p_buf = new Byte[len];
 i NT I;
while ((i = In.read (p_buf))!=-1)br> {
Toclient.write (p_buf, 0, I);
} in.close ();
Toclient.flush ();//force Clear Buffer
Toclient.close ();//②
%>
<% rs.close ();
Stmt.close ();
Conn.close ();%>

Places to look for:

There are two aspects to note: The ① code, as noted, should call the next () function before going to the record, and navigate to the first record, where the index of the column in the record starts at 1, not from 0. ② If the error, check is not forgotten to write the flow of the shutdown. That's the following sentence. The reason may be that response.getoutputstread () was also called in other places. This call cannot be successful if it is not closed.

Related Article

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.