The Struts1 file upload operation summary, including single file upload and multi-file upload, the content is as follows, left as a memo:
Struts2 implementation File upload article (http://blog.csdn.net/itwit/article/details/7031785)
1. Single File Upload
1) JSP page (singlefileupload.jsp)
<% @page pageencoding = "UTF-8"%>
Operating point: enctype= "Multipart/form-data"
2) Form (Uploadform)
Package Fileupload.singlefileupload.form;import Org.apache.struts.action.actionform;import Org.apache.struts.upload.formfile;public class Uploadform extends Actionform {private static final long Serialversionuid = 1l;private string name;private formfile file;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public Formfile GetFile () {return file;} public void Setfile (formfile file) {this.file = file;}}
Operating point: Struts1 provides a formfile class to handle file upload operations, noting that the property Name,file name must be consistent with the JSP page, otherwise a null pointer is reported.
3) Action (uploadaction)
Package Fileupload.singlefileupload.action;import Java.io.fileoutputstream;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.apache.struts.action.action;import Org.apache.struts.action.actionform;import Org.apache.struts.action.actionforward;import Org.apache.struts.action.actionmapping;import Org.apache.struts.upload.formfile;import Fileupload.singlefileupload.form.uploadform;public class UploadAction Extends Action {//@Overridepublic Actionforward Execute (actionmapping mapping, actionform form, httpservletrequest Request, HttpServletResponse response) throws Exception {uploadform uf = (uploadform) Form; String name = Uf.getname (); Formfile file = Uf.getfile (); System.out.println ("name=" + name); String filename = File.getfilename (); System.out.println ("filename=" + fileName); FileOutputStream fos = new FileOutputStream ("d:\\[" +name+ "]" + filename); Create an output stream Fos.write (File.getfiledata ()); Write Fos.flush ();//Release FOs.close (); Close return Mapping.findforward ("Success"); }}
Action points: Methods of the Formfile class
4) Struts configuration file (Struts-config.xml)
<!--Configure a single upload--><form-bean name= "uploadform" type= "FileUpload.singleFileUpload.form.UploadForm"/>
<!--Configure a single upload--><action path= "/upload" type= "FileUpload.singleFileUpload.action.UploadAction" Name= " Uploadform "scope=" request "> <forward name=" Success "Path="/singlefileupload.jsp "/></action>
<!--profile Upload size--><!--Specifies that the maximum upload size is 2M, if set to 1 the size is unlimited--><controller maxfilesize= "2M"/>
Operation Point: Controller maxfilesize is used to configure the size of the upload file, can be configured according to the actual situation, set to 1 means no limit. It must be positioned between </action-mappings> and <message-resources>
2, multi-file upload
1) jsp page (mutityfileupload.jsp) (the code obtained from the Internet, can be increased and reduced, good)
<%@ page pageencoding= "Utf-8"%><%@ taglib uri= "http://struts.apache.org/tags-html" prefix= "html"%>< Html>2) Form (uploadmoreform)
Package Fileupload.mutityfileupload.form;import Java.util.arraylist;import Java.util.list;import Org.apache.struts.action.actionform;import Org.apache.struts.upload.formfile;public class UploadMoreForm extends Actionform{private static final Long serialversionuid = 1l;private list<formfile> myFiles = new Arraylist<formfi Le> (); Used to hold an indefinite number of Formfile objects public formfile getFile (int i) //indexed property { return myfiles.get (i); } public void Setfile (int i, formfile myFile) //indexed property { if (myfile.getfilesize () > 0) // Upload this file { myfiles.add (myFile) only if the number of bytes uploaded is greater than 0.} public int Getfilecount () //Get the number of uploaded files { return myfiles.size ();} }
3) Action (uploadmoreaction)
Package Fileupload.mutityfileupload.action;import Java.io.fileoutputstream;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.apache.struts.action.action;import Org.apache.struts.action.actionform;import Org.apache.struts.action.actionforward;import Org.apache.struts.action.actionmapping;import Org.apache.struts.upload.formfile;import Fileupload.mutityfileupload.form.uploadmoreform;public Class Uploadmoreaction extends Action {public Actionforward execute (actionmapping mapping, actionform form, HTTPSERVL Etrequest request, HttpServletResponse response) {Uploadmoreform Umform = (uploadmoreform) Form; int count = 0; try {count = Umform.getfilecount (); Get the total number of uploaded files for (int i = 0; i < count; i++) {Formfile file = Umform.getfile (i); System.out.println (File.getfilename ()); FileOutputStream fos = new FileOutputStream ("d:\\" + FIle.getfilename ()); Create an output stream Fos.write (File.getfiledata ()); Write Fos.flush ();//release fos.close (); Close}} catch (Exception e) {e.printstacktrace (); } return null; }}
4) Struts configuration file (struts-config.xml)
<!--Configure multiple uploads--><form-bean name= "Uploadmoreform" type= "FileUpload.mutityFileUpload.form.UploadMoreForm"/ >
<!--Configure multiple uploads--><action path= "/multiupload" type= "fileUpload.mutityFileUpload.action.UploadMoreAction" name = "Uploadmoreform" scope= "request" > <forward name= "Success" Path= "/mutityfileupload.jsp"/></action >
<!--profile Upload size--><!--Specifies the maximum upload size is 2M, if set to 1 size unlimited--><controller maxfilesize= "-1"/>
Struts1 file Upload, single file, multiple file upload "Struts1"