Recently, a project was created, and the user uploads the image and displays it via the IMG control. We all know that IMG can display pictures through the src attribute. such as . This does not have any problem, I believe many friends do the same.
But there is a problem with this, and the picture must be placed below the application. And if you do a cluster, then the picture is scattered in the rich applications below, very bad management. It is natural for us to think that it would be nice to put the picture in one place, preferably outside of the app, such as the IMG directory under the C drive. Of course uploading pictures is good to do, but what should I do to read pictures? It is impossible to do this by src= the "http://127.0.0.1/a/b/abc.jpg" approach. In this case, we can read the following image by using the following method:
@RequestMapping (value= "Showimg") Public voidSHOWIMG (httpservletrequest request,httpservletresponse response)throwsioexception{String imgfile= Request.getparameter ("Imgfile");//file nameString path= urlutil.getvalue ("goodsimg");//here is the folder address where the picture is storedFileInputStream fileis=NULL; Try{Fileis=NewFileInputStream (path+ "/" +imgfile); } Catch(Exception e) {log.error ("The system cannot find the image file:" +path+ "/" +imgfile); return; } intI=fileis.available ();//Get File Size bytedata[]=New byte[i]; Fileis.read (data); //Read DataResponse.setcontenttype ("image/*");//set the returned file typeOutputStream Outstream=response.getoutputstream ();//gets the object that outputs the binary data to the clientOutstream.write (data);//Output DataOutstream.flush (); Outstream.close (); Fileis.close (); }
Here I am using the SPRINGMVC framework. @RequestMapping (value= "showimg") is actually a request.
At this time we will be the IMG SRC changed to "http://127.0.0.1/a/showImg?imgFile=abc.jpg" can display the picture, that is, the SRC changed to a request address, imgfile after the name of the picture.
Java Web implementation img Read the image under the drive letter