Struts upload download must introduce two jar files:
Commons-fileupload-x.x.x.jar and Comons-io-x.x.x.jar uploading files
ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportOrg.apache.struts2.ServletActionContext;ImportPojo. User;ImportCom.opensymphony.xwork2.ActionSupport;PublicClass FileupactionExtendsactionsupport{PrivateStaticFinalint buffer_size = 40*40;PrivateFile upload;PrivateString Uploadcontenttype;PrivateString Uploadfilename;PrivateString Savepath;PrivateUser user;PrivateStaticvoidCopy (file source, file target) {InputStream is =Null; OutputStream OS =Null;Try{is =New Bufferedinputstream (NewFileInputStream (source), buffer_size); OS =New Bufferedoutputstream (NewFileOutputStream (target), buffer_size);int len = 0;Byte[] bs =NewByte[Buffer_size];while ((Len=is.read (BS))!=-1) {os.write (BS, 0, Len); } }Catch(Exception e) {E.printstacktrace ();}Finally{if (is!=Null){Try{Is.close ();}Catch(Exception E2) {//Todo:handle exceptionE2.printstacktrace (); } }if (os!=Null){Try{Os.close ();}Catch(Exception E3) {//Todo:handle exceptionE3.printstacktrace (); }}} @OverridePublic String Execute ()Throwsexception{String Path = Servletactioncontext.getservletcontext (). Getrealpath (This.getsavepath ()) + "\ \" +This. Getuploadfilename (); User.setphone (This. uploadfilename); File target =NewFile (path); CopyThis. Upload, target);ReturnSUCCESS; }PublicFile Getupload () {ReturnUpload }PublicvoidSetupload (File upload) {This.upload =Upload }PublicString Getuploadcontenttype () {ReturnUploadcontenttype; }PublicvoidSetuploadcontenttype (String uploadcontenttype) {This.uploadcontenttype =Uploadcontenttype; }PublicString Getuploadfilename () {ReturnUploadfilename; }PublicvoidSetuploadfilename (String uploadfilename) {this.uploadfilename = Uploadfilename;} public String Getsavepath () {return Savepath; } public voidthis.savepath = Savepath ; } public User GetUser () { return user;} public void SetUser ( User user) {this.user = user;}}
Action configuration in the Struts2
<Actionname= "Fileup" class= "actions." Fileupaction "> <param name=" Savepath ">/upload</param> <result>/userinfo.jsp</result></action>
Form
<s:form action= "Fileup" namespace= "/" method= "POST" enctype= "Multipart/form-data" > <s:textfield name= " User.Name "label=" name "size="/> <s:file name= "upload" label= "image" Size= "/> <s:textfield" name = "User.age" label= "Age" size= "/> <s:radio list=" #{1: ' Male ', 2: ' Female '} ' name= ' user.sex ' listkey= ' key ' listvalue= "Value" value= "1" label= "gender" cssstyle= "border:0px;" /> <s:textfield name= "User.icard" label= "Social Security Number" size= ""/> <s:textfield name= "user.phone" label = "Contact Phone" size= "/> <s:textfield name=" user.address "label=" Home Address "size="/> <s:submit value= " Confirm Entry "align=" center "/></s:form>
If the form contains a file field with the name attribute xxx, you can use the following 3 properties to encapsulate the file field information in the action:
File contents for file XXX package files
String xxxcontexttype file type for file domain corresponding file
String xxxfilename file name of the file domain corresponding to the package
When using the picture
Download
ImportCom.opensymphony.xwork2.ActionSupport;PublicClass DownloadactionExtendsactionsupport{PrivateString Downpath;Public InputStream getInputStream ()Throwsexception{ReturnServletactioncontext.getservletcontext (). getResourceAsStream (Downpath); }PublicString Getdownpath () {ReturnDownpath; }PublicvoidSetdownpath (String downpath) {This.downpath =Downpath; }Public string Getdownloadfilename () {String downfilename = downpath.substring (7); try{downfilename = new String (Downfilename.getbytes (), "iso8859-1" );} catch (Exception e) {e.printstacktrace (); // Todo:handle exception} return Downfilename;} @Override public String Execute () throws exception{return SUCCESS;}
Struts2.xml Configuration
<ActionName= "DownLoad"Class= "actions. Downloadaction "><ResultType= "Stream"><ParamName= "ContentType">Application/msword,text/plain,application/vnd.ms-powerpoint,application/vnd.ms-excel</Param><ParamName= "InputName">inputstream</Param><param name= " Contentdisposition "> Attachment;filename=" ${ DownloadFileName} "</param > <param name= "buffersize" >40960 </param> </result> < /action>
Use
<a href= "download.action?downpath=upload/123.png" > Downloads </a>
struts2 File Upload Download