在 struts.xml 中:
<struts>
<constant name="struts.multipart.maxSize=5242880" ></constant> #控制最大缓存值
<package name="upload" namespace="/upload" extends="struts-default">
<action name="upload2" class="cn.itcast.action.UploadAction2" method="execute">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
在 UploadAction2 中:
public class UploadAction2 extends ActionSupport implements Serializable{
private File[] image; // 对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型
private String[] imageFileName; // 上传输入域 FileName 即文件名
private String[] imageContentType; // 文件上传的MIME类型
封装……
public String execute(){
try{
if( image!=null && images.length >0){
ServletContext sc = ServletActionContext.getServletContext();
String storePath = sc.getRealPath("/files");
for(int i =0; i<images.length; i++){
FileUtils.copyFile( image[i], new File(storePath, imageFileName[i]) );
}
}
ActionContext.getContext().put("message","上传成功");
return SUCCESS;
} catch(Exception e){
return ERROR;
}
}
}
在上传页面 upload2.jsp 中
<form action="${pageContext.request.contextPath}/upload/upload2" method="post" enctype="multipart/form-data">
文件1:<input type="file" name="image"><br/>
文件2:<input type="file" name="image"><br/>
文件3:<input type="file" name="image"><br/>
<input type="submit" value="上传">
</form>