It is also easy to upload multiple files in struts2. In action, you can use an array or a set to receive the corresponding attributes of the file.
File [] file;
String [] filefilename;
String [] filecontenttype;
1. fileuploadactionCode
Package CN. luxh. struts2.action; import Java. io. file; import Java. io. ioexception; import Java. text. simpledateformat; import Java. util. date; import Org. apache. commons. io. fileutils; import Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; import Org. apache. struts2.servletactioncontext; import COM. opensymphony. xwork2.actionsupport;/*** Upload File * @ author luxh */public class fileuploadac Tion extends actionsupport {Private Static final long serialversionuid = 2642160699551232611l; Private Static log = logfactory. getlog (fileuploadaction. class); protected file [] file; protected string [] filefilename; protected string [] filecontenttype;/*** Upload File */Public String upload () {try {file [] files = uploadfile ("/upload"); // After get files // do some work //...} catch (exception e) {log. error ("Upload text An error occurred! "); Throw new runtimeexception (" File Upload error ");} return success ;} /*** process uploaded files * @ Param savedir * @ return * @ throws ioexception */Public file [] uploadfile (string savedir) throws ioexception {If (savedir = NULL | "". equals (savedir. trim () {savedir = "/upload";} file [] files = NULL; If (file! = NULL & file. length> 0) {string savedirpath = servletactioncontext. getservletcontext (). getrealpath (savedir); file dirfile = new file (savedirpath); If (! Dirfile. exists () {dirfile. mkdir () ;}files = new file; For (INT I = 0; I <file. length; I ++) {If (file [I]! = NULL) {string newfilename = generatefilename (filefilename [I]); file destfile = new file (savedirpath, newfilename); fileutils. copyfile (file [I], destfile); files [I] = destfile ;}} return files ;} /*** generate the file name * @ Param filename * @ return */private string generatefilename (string filename) {simpledateformat SDF = new simpledateformat ("yyyymmddhhmmsssss"); string formatdate = SDF. format (new date (); int positio N = filename. lastindexof ("."); string extension = ""; if (position! =-1) {extension = filename. substring (position);} return formatdate + extension;} public file [] GetFile () {return file;} public void setfile (file [] File) {This. file = file;} Public String [] getfilefilename () {return filefilename;} public void setfilefilename (string [] filefilename) {This. filefilename = filefilename;} Public String [] getfilecontenttype () {return filecontenttype;} public void setfilecontenttype (string [] filecontenttype) {This. filecontenttype = filecontenttype ;}}
2. upload page upload. jsp
<Form action = "$ {pagecontext. request. contextpath}/fileupload/upload "method =" Post "enctype =" multipart/form-Data "> <Table> <tr> <TD> select a file: </TD> </tr> <TD> <input type = "file" name = "file"> </TD> </tr> <TD> <input type = "file" name = "file"> </TD> </tr> <TD> <input type = "file" name = "file"> </TD> </tr> </table> <tr> <TD> <input type = "Submit" value = "Submit" id = "Submit"> </TD> </tr> </form>
3. configuration file struts. xml
<Action name = "Upload" class = "CN. luxh. struts2.action. fileuploadaction "approach =" Upload "> <result>/WEB-INF/pages/common/success. JSP </result> </Action>