其实实现多文件的上传和单文件的上传实现原理相同,都是将文件进行复制,多文件的上传只是将文件进行逐个的复制。
下面代码实现多文件的上传:
首先确定jsp页面:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>多文件上传</title>
</head>
<body>
<s:form action="muchFileUpload" method="post" enctype="multipart/form-data">
username:<s:textfield name="username"></s:textfield><br>
<s:file name="uploadFile"></s:file><br>
<s:file name="uploadFile"></s:file><br>
<s:file name="uploadFile"></s:file><br>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
然后编写上传文件的action
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 实现多文件上传的Action
* @author acer
*
*/
public class MuchFileUpload extends ActionSupport{
private String username;
private File []uploadFile;
private String []uploadFileFileName;
public String getUsername() {
return username;
}
public File[] getUploadFile() {
return uploadFile;
}
public String[] getUploadFileFileName() {
return uploadFileFileName;
}
public void setUsername(String username) {
this.username = username;
}
public void setUploadFile(File[] uploadFile) {
this.uploadFile = uploadFile;
}
public void setUploadFileFileName(String[] uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
@Override
public String execute() throws Exception {
System.out.println("username的值为:"+username);
String dir = ServletActionContext.getRequest().getRealPath("/upload");
for(int i=0;i<uploadFile.length;i++) {
File file = new File(dir,uploadFileFileName[i]);
FileUtils.copyFile(uploadFile[i], file);
}
return null;
}
}
这里需要注意的就是将文件该文文件数组,文件名改为文件名数组。
最后在struts.xml文件中配置action即可实现多文件的上传。
上面的方式实现了文件的上传,但是如果变量较多,那么就会出现很多的set,get方法,这样类显得很臃肿。所以可以使用属性驱动的方式实现文件的上传。所谓属性驱动,就是将属性用javaBean进行封装。
下面直接上代码:
1.确定jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>属性驱动实现多文件上传</title>
</head>
<body>
<s:form action="uploadMethod1" method="post" enctype="multipart/form-data">
username:<s:textfield name="username"></s:textfield>
<br>
<!-- 表单中name的值也发生了变化 -->
<s:file name="fileUploadTools.uploadFile"></s:file><br>
<s:file name="fileUploadTools.uploadFile"></s:file><br>
<s:file name="fileUploadTools.uploadFile"></s:file><br>
<s:submit value="提交"/>
</s:form>
</body>
</html>
这里表单的name值发生了变化。name的属性值加入了fileUploadTools,这个值要与Action的属性名对应一致。
2.确定javaBean
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.struts2.ServletActionContext;
/**
* 上传文件的封装类
* @author acer
*/
public class FileUploadTools {
private String username;
private File []uploadFile;
private String []uploadFileFileName;
private String [] uploadFileContentType;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public File[] getUploadFile() {
return uploadFile;
}
public String[] getUploadFileFileName() {
return uploadFileFileName;
}
public String[] getUploadFileContentType() {
return uploadFileContentType;
}
public void setUploadFile(File[] uploadFile) {
this.uploadFile = uploadFile;
}
public void setUploadFileFileName(String[] uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
public void setUploadFileContentType(String[] uploadFileContentType) {
this.uploadFileContentType = uploadFileContentType;
}
/**
* 用来上传文件及取得普通表单域的值
* @return
* @throws IOException
*/
public String beginUpload() throws IOException{
System.out.println("username的值是:"+username);
String targetDir = ServletActionContext.getRequest().getRealPath("/upload");
for(int i=0;i<uploadFile.length;i++) {
File target = new File(targetDir,
new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date()).toString()+
System.nanoTime()+uploadFileFileName[i]);
FileUtils.copyFile(uploadFile[i], target);
}
return null;
}
}
3.上传文件的Action
import com.opensymphony.xwork2.ActionSupport;
/**
* 属性驱动实现文件上传
* @author acer
* 就是将属性用javaBean进行封装,避免了大量的set,get方法
*/
public class UploadMethod1 extends ActionSupport{
private FileUploadTools fileUploadTools = new FileUploadTools();
public FileUploadTools getFileUploadTools() {
return fileUploadTools;
}
public void setFileUploadTools(FileUploadTools fileUploadTools) {
this.fileUploadTools = fileUploadTools;
}
@Override
public void validate() {
}
@Override
public String execute() throws Exception {
fileUploadTools.beginUpload();
return null;
}
}
基于属性驱动的上传模型非常有利于代码的整洁和后期良好的维护性,在开发中,应多使用属性驱动的形式,不论是上传还是普通的功能。