JSP Display picture Problem solving

Source: Internet
Author: User
Tags sql string stmt
js| Solution | problem | show

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); Locate to record rs.next ();
InputStream in = Rs.getbinarystream (1);//①
Response.reset (); Returns the position that was marked in the stream
Response.setcontenttype ("image/jpg"); or GIF etc//Get input stream
OutputStream toclient = Response.getoutputstream ();//②
byte[] P_buf = new Byte[len];
int i;
while ((i = In.read (p_buf))!=-1)
{
Toclient.write (p_buf, 0, I);
} in.close ();
Toclient.flush (); Force clear the 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.