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.