(SPRINGMVC) Read blob type pictures from Oracle and display them in JSP

Source: Internet
Author: User

Recent work encountered from the database read the Blob type of picture, and in the page display problems, think of the next, there are probably two ways, one is to convert the data into a file, save it locally, and then upload the file address to the foreground page, read the saved picture file, the second is to convert the file into a data stream, directly on the page, I think the second method is relatively simple, and the processing speed is faster. Below I will record my operation process, give everybody a reference.

Ideas, steps:

1: Read blob picture from database

2: Convert to data stream

3: Show on page


First, we set up a controller method in Springmvc.

@RequestMapping ("/toolutil/") public class toolutil{     @AutoWired      private toolutilmanager toolutilmanager;//Persistence Layer     /**      *  Get pictures      *  @param  request      *  @param  response     */     @RequestMapping (" Getphoto_blob ")     public void getphoto_blob (Httpservletrequest request, Httpservletresponse response) {    string id = request.getparameter ("id     //This I will not write in detail, that is, by passing the ID to query the picture you want to display     map resultmap =  toolutilmanager.getphoto_blob (ID)     //blob is the picture you want to display     blob  blob =  (BLOB)  resultmap.get ("Photo");     inputstream in =  null;try {    in = blob.getbinarystream ();    outputstream out =  Response.getoutputstream ();         data = new byte[(int ) Blob.length ()];        int i = 0;     while (I=in.read (data))!=-1) {       out.write (data);     }    out.close ();     in.close ();}  catch  (exception e)  {    e.printstacktrace ();}     }    }

OK, this is the controller in the method, these lines of code to complete steps 1 and 2, below we want to display on the page

<!--get the BLOB type image with ID 1, define width 128, high 185-->/toolutil/getphoto_ Blob.xhtml?id=1 "width=" "height=" 185 "/>

Step 3 is complete, now your picture can be displayed on the page, simple and convenient.


Code hand, inevitably there is missing words, typos, description is not clear phenomenon, welcome criticism advice, not personal attacks, if you feel useful, tell me a sound, give me a little encouragement.


(SPRINGMVC) Read blob type pictures from Oracle and display them in JSP

Related Article

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.