Preparing files
1. Upload multiple file pages
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <% @taglib uri= "/struts-tags" prefix= "s"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
2. Configure the appropriate action in the Struts.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "http://struts.apache.org/dtds/ Struts-2.0.dtd "><struts><!--dynamic method call-- <constant name=" Struts.devmode "value=" false "/> <package name= "Default" namespace= "/" extends= "Struts-default" > <action name= "upload" class= " Cn.happy.action.UploadAction "method=" execute "> <param name=" Savepath ">/image</param> <result name= "Success" >/upload/success.jsp</result> </action> </package></ Struts>
3. Find the corresponding class based on the action node
Package Cn.happy.action;import Java.io.file;import Java.io.fileinputstream;import java.io.fileoutputstream;import Org.apache.struts2.servletactioncontext;import Com.opensymphony.xwork2.actionsupport;public class UploadAction Extends Actionsupport {/*** * multi-File Upload * @return *///properties of uploaded files private file[] upload;//upload file type private string[] Uploadcontentty pe;//upload file name private string[] uploadfilename;//the address of the uploaded file is private string Savepath; @Overridepublic string Execute () throws Exception{byte[] buffer=new byte[1024];for (int i=0;i<upload.length;i++) {//Set up stream of uploaded files FileInputStream fis=new FileInputStream (Getupload () [i]);//build the output stream of the uploaded file, getimagefilename () [I]fileoutputstream fos=new FileOutputStream ( Getsavepath () + "\ \" +getuploadfilename () [i]); int length=fis.read (buffer); while (length>0) {fos.write (buffer,0,length); Length=fis.read (buffer); } fis.close (); Fos.flush (); Fos.close ();} return SUCCESS;} Public file[] Getupload () {return upload;} public void Setupload (file[] upload) {this.upload = uPload;} Public string[] Getuploadcontenttype () {return uploadcontenttype;} public void Setuploadcontenttype (string[] uploadcontenttype) {this.uploadcontenttype = Uploadcontenttype;} Public string[] Getuploadfilename () {return uploadfilename;} public void Setuploadfilename (string[] uploadfilename) {this.uploadfilename = Uploadfilename;} Public String Getsavepath () {return Servletactioncontext.getservletcontext (). Getrealpath (Savepath);} public void Setsavepath (String savepath) {this.savepath = Savepath;}}4. If successful, find a success page
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <% @taglib uri= "/struts-tags" prefix= "s"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Multiple file uploads