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.