servlet Upload Image Server path

Source: Internet
Author: User
Tags apache tomcat

1. Upload the image in the servlet, upload the folder is imge under Webroot, the main code is as follows

Private voidSaveImage (httpservletrequest request, httpservletresponse response) throws IOException {//name of the saved pictureFileName =system.currenttimemillis () +". jpg"; //get the Imge folder in Tomcat decision path, BasePath value is C:\Program files\apache software foundation\apache Tomcat 6.0.20\webapps\ Flextest\imgeString basepath = Request.getsession (). Getservletcontext (). Getrealpath ("/imge/"); FilePath=BasePath; System. out. println ("the address for saving the picture is:"+FilePath); Realfilepath= filepath+"\\"+FileName; //get a picture file stream, I'm coming from flex here.BufferedImage BufferedImage =Imageio.read (Request.getinputstream ()); if(BufferedImage! =NULL) {               //Save the picture to the specified directory and fileImageio.write (BufferedImage,"JPEG",NewFile (FilePath, fileName)); }       }    


2. When you want to display the above uploaded image through the servlet to the viewer, take the following path:

    Private voidPrintimage (httpservletrequest request, httpservletresponse response) throws IOException {RESPONSE.S Etcontenttype ("text/html"); Request.setcharacterencoding ("Utf-8"); PrintWriter out=Response.getwriter (); //get the address of the server, cannot get the absolute path of native Tomcat directly, otherwise the viewer cannot read the specified picture file//the value of BasePath is http://localhost:8080/flextestString basepath = request.getscheme () +"://"+request.getservername () +":"+request.getserverport () +request.getcontextpath () +"/"; String Showfile= basepath+"/imge/"+ This. FileName;  out. println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" >");  out. println ("<HTML>");  out. println ("");  out. println ("<BODY>");  out. Print (" This is");  out. Print (""+showfile+">");  out. Print ("</img>"); System. out. println ("the address where the picture is displayed is"+showfile); //the value of the Showfile ishttp://localhost: 8080/flextest//imge/1299470395060.jpg             out. println (", using the POST method");  out. println ("</BODY>");  out. println ("</HTML>");  out. Flush ();  out. Close (); }    

servlet Upload Image Server path

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.