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