1: In order to facilitate testing, the 1.png image is directly deposited into the database.
Public Static voidStorepicblog ()throwsFileNotFoundException, SQLException, ioexception{Connection Conn=jdbcutil.getconnection (); File F=NewFile ("1.jpg"); FileInputStream FIS=NewFileInputStream (f); String SQL= "INSERT into Pic_test (ID,ZP) VALUES (?,?)"; PreparedStatement PS=conn.preparestatement (SQL); Ps.setstring (1, Stringutil.getuuid ()); Ps.setbinarystream (2, FIS, (int) f.length ()); Ps.executeupdate (); Fis.close (); Ps.close (); Jdbcutil.release (conn); }
2: Read operation
Public StaticInputStream Getpicinputstream () {String ID= "F304733361E243779B2340AFE20E62BF"; Connection Conn=jdbcutil.getconnection (); PreparedStatement PS=NULL; ResultSet RS=NULL; InputStream is=NULL; String SQL= "Select ZP from Pic_test where id=?"; Try{PS=conn.preparestatement (SQL); Ps.setstring (1, id); RS=Ps.executequery (); if(Rs.next ()) { is= Rs.getblob ("ZP"). Getbinarystream (); } } Catch(SQLException ex) {Logger.getlogger (Test.class. GetName ()). log (Level.severe,NULL, ex); }finally{ Try { if(NULL!=PS) {Ps.close (); } } Catch(SQLException ex) {Logger.getlogger (Test.class. GetName ()). log (Level.severe,NULL, ex); } Try { if(NULL!=RS) {Rs.close (); } } Catch(SQLException ex) {Logger.getlogger (Test.class. GetName ()). log (Level.severe,NULL, ex); } jdbcutil.release (conn); } returnis ; }
3: Test servlet, Access this servlet through a browser, be sure to set:
Response.setcontenttype ("Image/jpeg") to display pictures in the browser
protected voidProcessRequest (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Image/jpeg"); InputStream is=Test.getpicinputstream (); if(NULL!=is ) {outputstream OS=Response.getoutputstream (); intLen; byteBuf[] =New byte[1024]; while((Len=is.read (BUF))!=-1) {os.write (buf,0, Len); } is.close (); Os.close (); } }
Storing pictures in a BLOB format into the database, and then displaying the image to the interface via a servlet