与上传单个文件类似,需要把File文件变为File数组
一、新建包cn.xs.fileupload,并建立FileUploadAction.java文件,内容如下:
// ---------------------------------------------------------
// @author sheng.xu
// @version 1.0.0
// @date 2014年6月8日
// ---------------------------------------------------------
package cn.xs.fileupload;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
/**
* @author sheng.xu
*
*/
public class FileUploadAction {
private File[] image;//获取文件
private String[] imageFileName;//获取文件名称
private String[] imageContentType;//获取文件类型
/**
* @return the image
*/
public File[] getImage() {
return image;
}
/**
* @param image the image to set
*/
public void setImage(File[] image) {
this.image = image;
}
/**
* @return the imageFileName
*/
public String[] getImageFileName() {
return imageFileName;
}
/**
* @param imageFileName the imageFileName to set
*/
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
/**
* @return the imageContentType
*/
public String[] getImageContentType() {
return imageContentType;
}
/**
* @param imageContentType the imageContentType to set
*/
public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
}
public String execute() throws IOException {
String realpath = ServletActionContext.getServletContext().getRealPath("/images");//获取文件上传后的存放目录的绝对路径
System.out.println(realpath);
if(image != null){
File savedir = new File(realpath);
if(!savedir.exists()){
savedir.mkdirs();
}
for(int i = 0; i < image.length; i++){
File savefile = new File(savedir,imageFileName[i]);
FileUtils.copyFile(image[i], savefile);
}
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
public String requestFile(){
return "success";
}
}
二、建立对应的jsp文件:fileupload.jsp内容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/test3/fileupload_execute.action" enctype="multipart/form-data" method="post">
<span style="white-space:pre"> </span>文件1:<input type="file" name="image"><br>
<span style="white-space:pre"> </span>文件2:<input type="file" name="image"><br>
<span style="white-space:pre"> </span>文件3:<input type="file" name="image"><br>
<span style="white-space:pre"> </span><input type="submit" value="上传"/>
</form>
{$message}
</body>
</html>
三、建立struts.xml中配置action如下:
<action name="fileupload_*" class="cn.xs.fileupload.FileUploadAction" method = "{1}">
<!-- 配置返回结果 -->
<result name="success">/WEB-INF/page/fileupload.jsp</result>
<result name="error">/error.jsp</result>
</action>
四、访问http://localhost:8080/Struts2/test3/fileupload_requestFile.action
注:上传文件大小的限制,可以在struts.xml文件中加入如下配置:<constant name="struts.mutipart.maxSize" value="10701096">