前面的配置和上传单个文件差不多,只是在Action类中不同,而且加入了线程的操作。
1.在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar两个包。(如果当前项目在之前配置好了struts就不需要重复添加了)
2.在form表单中enctype设置为:"multipart/form-data",如下:<form action="dofile/fileac" enctype="multipart/form-data" method="post">
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="dofile/fileac" enctype="multipart/form-data" method="post">
<input type="file" name="newfile"><br/>
<input type="file" name="newfile"><br/>
<input type="file" name="newfile"><br/>
<input type="submit">
</form>
</body>
</html>
3.建立FileAction类 声明属性(我设计的该类在com.action包内)
需要说明的是你定义的这些属性的名字,newfile必须和表单中的name的值一样。其他属性的名字前缀必须也是表单中name的值后面加上FileName获得它的文件名称。如果名字不一致会报错。因为上传的是多个文件,因此全都变为数组
package com.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.tool.FileUploadTool;
public class FileAction {
private File newfile[];
private String newfileFileName[];
public File[] getNewfile() {
return newfile;
}
public void setNewfile(File[] newfile) {
this.newfile = newfile;
}
public String[] getNewfileFileName() {
return newfileFileName;
}
public void setNewfileFileName(String[] newfileFileName) {
this.newfileFileName = newfileFileName;
}
public String execute(){
String path=ServletActionContext.getServletContext().getRealPath("/Images");
for (int i = 0; i < newfile.length; i++) {
//启动线程
new FileUploadTool(newfile[i], path+"/"+newfileFileName[i]).start();
}
return "success";
}
}
4.利用线程进行操作,写了一个线程类(我放在com.tool包下)
package com.tool;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class FileUploadTool extends Thread{
private File f;
private String path;
public FileUploadTool(File f,String path) {
this.f=f;
this.path=path;
}
@Override
public void run() {
try {
FileUtils.copyFile(f, new File(path));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5.配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.multipart.maxSize" value="104857600"></constant>
<package name="file" extends="struts-default" namespace="/dofile">
<action name="fileac" class="com.action.FileAction">
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
这是用刚学的文件上传写的一个例子 希望大家多多指教。