1.struts.xml
1 <Struts>2 <constantname= "Struts.multipart.maxSize"value= "209715200" /><!--set File upload size, 2097152=2m -3 < Packagename= "Action"extends= "Struts-default"namespace="/">4 <!--File Upload -5 <Actionname= "Upload"class= "Com.fileupload.action.UploadAction"Method= "Execute">6 <resultname= "Success">load_success.jsp</result><!--jump page After successful file upload -7 <resultname= "message">/message.jsp</result><!--upload failed jump page -8 <Interceptor-refname= "Defaultstack">9 <paramname= "Fileupload.allowedextensions">Jpg,png</param><!--Set upload format -Ten </Interceptor-ref> One </Action> A <!--File Download - - <Actionname= "Download"class= "Com.fileupload.action.DownloadAction"Method= "Execute"> - <resulttype= "Stream"> the <paramname= "InputName">Is</param> <!--Specifies the name of the return stream - - <paramname= "ContentType">Application/pdf/jpg</param><!--Specify the type of the rebate data - - <paramname= "Contentdisposition">Attachement;filename=${filename}</param><!--Specify the type and name of the file that the user downloads - - </result> + </Action> - </ Package> + </Struts>
2. Upload action
1 ImportJava.io.File;2 Importjava.io.IOException;3 Importorg.apache.commons.io.FileUtils;4 ImportOrg.apache.struts2.ServletActionContext;5 6 Public classuploadaction {7 PrivateFile img;//file8 PrivateString Imgfilename;//file name: +filename9 Ten PublicString Execute ()throwsioexception{ One A if(img! =NULL){ -String path = Servletactioncontext.getservletcontext (). Getrealpath ("/images"); -File DestFile =NewFile (path, imgfilename); the fileutils.copyfile (IMG, destfile); - return"Success"; - } - return"Message"; + } - + PublicFile getimg () { A returnimg; at } - Public voidsetimg (File img) { - This. IMG =img; - } - PublicString Getimgfilename () { - returnImgfilename; in } - Public voidsetimgfilename (String imgfilename) { to This. Imgfilename =Imgfilename; + } -}
3. Download action
Importjava.io.FileNotFoundException;ImportJava.io.InputStream;Importjava.io.UnsupportedEncodingException;ImportOrg.apache.struts2.ServletActionContext; Public classdownloadaction {PrivateInputStream is;//input stream, first read the file into the server memory PrivateString FileName;//Casual PublicString Execute ()throwsFileNotFoundException, unsupportedencodingexception{fileName= "1.png";//set the path to get the file name in the project by connecting DBFileName =NewString (Filename.getbytes ("iso-8859-1"), "UTF-8");//Set Encodingis = Servletactioncontext.getservletcontext (). getResourceAsStream ("/images/" +filename);//Get file Stream return"Success"; } PublicInputStream Getis () {returnis ; } Public voidSetis (InputStream is) { This. is =is ; } PublicString GetFileName () {returnFileName; } Public voidsetfilename (String fileName) { This. FileName =FileName; }}
[Note]:
1. The download appears 500:can not find a java.io.InputStream with the name [was] in the invocation stack. Check the <param Nam
Solution: 1) Check if the is input stream is NULL, if NULL, do not get to the correct path, check the file name is correct
2) The Get method in the action that does not have a write in the config file "<param name=" InputName ">" After property
3) When using return Servletactioncontext.getservletcontext (). getResourceAsStream ("...") when this method obtains the input stream, ensure that the file location is ServletContext, that is, in the context of the current application, if you want to get an external file, such as a file in D, you must create an input stream yourself, such as:
New File ("D://spring.doc"); New fileinputstream (file); return
File upload and download based on STRUTS2