JSP reads the image BLOB field from the Oracle database and displays it on the page < go >

Source: Internet
Author: User

  Java code: First define a method that reads a BLOB field from an Oracle database and writes bytes to an output stream. 
1 Public Static voidwriteimg (outputstream os) {2 Connection con;3 Try {4Con =connectionfactory.getconnection ();5Statement stmt =con.createstatement ();6ResultSet rs = stmt.executequery ("Select pic from blob_table where b_id=1");7 byte[] B =New byte[1024];8 if(Rs.next ()) {9Blob blob = Rs.getblob (1);TenInputStream is =Blob.getbinarystream (); One inti = 0; A while((i = Is.read (b))! =-1) { -Os.write (b, 0, i); - } the os.close (); - is.close (); - } - rs.close (); + stmt.close (); - con.close (); +}Catch(Exception e) { A e.printstacktrace (); at } -}

Then make a servlet, call the Writeimg method in the Doget method
1publicvoid doget (httpservletrequest request, httpservletresponse response) 2 throws servletexception, IOException { 3 4 dboperator.writeimg ( Response.getoutputstream ()); 5 6 }

JSP Code:<img src="Servlet/newservlet" > Reprint: Author Longdick http://longdick.iteye.com

JSP reads the image BLOB field from the Oracle database and displays it on the page < go >

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.