Storing pictures in a BLOB format into the database, and then displaying the image to the interface via a servlet

Source: Internet
Author: User

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

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.