Java Web implementation img Read the image under the drive letter

Source: Internet
Author: User

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

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.