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