2、多文件上传
修改action
private
List
<
File
>
file;
private List < String > fileFileName;
private List < String > fileContentType;
private List < String > fileFileName;
private List < String > fileContentType;
public
String execute()
throws
Exception
{
for (int i = 0; i < file.size(); ++i) {
InputStream is = new FileInputStream(file.get(i));
String root = ServletActionContext.getRequest().getRealPath(
"/upload");
File destFile = new File(root, this.getFileFileName().get(i));
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close();
}
return SUCCESS;
}
修改upload3.jsp
for (int i = 0; i < file.size(); ++i) {
InputStream is = new FileInputStream(file.get(i));
String root = ServletActionContext.getRequest().getRealPath(
"/upload");
File destFile = new File(root, this.getFileFileName().get(i));
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close();
}
return SUCCESS;
}
<
tr
>
< td >
file1
</ td >
< td >
< s:file name ="file" ></ s:file >
</ td >
</ tr >
< tr >
< td >
file2
</ td >
< td >
< s:file name ="file" ></ s:file >
</ td >
</ tr >
< tr >
< td >
file3
</ td >
< td >
< s:file name ="file" ></ s:file >
</ td >
</ tr >
结果:
< td >
file1
</ td >
< td >
< s:file name ="file" ></ s:file >
</ td >
</ tr >
< tr >
< td >
file2
</ td >
< td >
< s:file name ="file" ></ s:file >
</ td >
</ tr >
< tr >
< td >
file3
</ td >
< td >
< s:file name ="file" ></ s:file >
</ td >
</ tr >
3、任意数量文件上传
在多文件上传的基础上修改upload3.jsp
<
script
type
="text/javascript"
>
function addMore()
{
var td = document.getElementById("more");
var br = document.createElement("br");
var input = document.createElement("input");
var button = document.createElement("input");
input.type = "file";
input.name = "file";
button.type = "button";
button.value = "Remove";
button.onclick = function()
{
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</ script >
function addMore()
{
var td = document.getElementById("more");
var br = document.createElement("br");
var input = document.createElement("input");
var button = document.createElement("input");
input.type = "file";
input.name = "file";
button.type = "button";
button.value = "Remove";
button.onclick = function()
{
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</ script >
<
tr
>
< td >
file1
</ td >
< td id ="more" >
< s:file name ="file" ></ s:file >
< input type ="button" value ="Add More.." onclick ="addMore()" >
</ td >
</ tr >
< td >
file1
</ td >
< td id ="more" >
< s:file name ="file" ></ s:file >
< input type ="button" value ="Add More.." onclick ="addMore()" >
</ td >
</ tr >
结果:
(四)文件上传类型、大小的限制
使用struts的拦截器,struts2-core-2.1.6.jar/org.apache.struts2.interceptor.FileUploadInterceptor.class的源码中我们可以看到:
public class FileUploadInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = -4764627478894962478L;
protected static final Logger LOG = LoggerFactory.getLogger(FileUploadInterceptor.class);
private static final String DEFAULT_MESSAGE = "no.message.found";
protected boolean useActionMessageBundle;
protected Long maximumSize;
protected Set < String > allowedTypesSet = Collections.emptySet();
protected Set < String > allowedExtensionsSet = Collections.emptySet();
所以我们只需的struts.xml中配置它的属性allowedTypesSet即可。在action节点中修改拦截器(默认的拦截器中已经有fileUpload拦截器,我们必须提取出来进行参数设置,然后在加上默认的拦截器)。
private static final long serialVersionUID = -4764627478894962478L;
protected static final Logger LOG = LoggerFactory.getLogger(FileUploadInterceptor.class);
private static final String DEFAULT_MESSAGE = "no.message.found";
protected boolean useActionMessageBundle;
protected Long maximumSize;
protected Set < String > allowedTypesSet = Collections.emptySet();
protected Set < String > allowedExtensionsSet = Collections.emptySet();
<
action
name
="upload"
class
="com.test.action.UploadAction"
>
< result name ="success" > /upload/result3.jsp </ result >
< result name ="input" > /upload/upload3.jsp </ result >
< interceptor-ref name ="fileUpload" >
< param name ="maximumSize" > 409600 </ param >
< param name ="allowedTypes" >
application/vnd.ms-powerpoint
</ param >
</ interceptor-ref >
< interceptor-ref name ="defaultStack" ></ interceptor-ref >
</ action >
其中
<
param
name
="allowedTypes"
>
application/vnd.ms-powerpoint
</
param
>的allowedTypes的值可在C:\Tomcat 6.0\conf的web.xml文件中查找。< result name ="success" > /upload/result3.jsp </ result >
< result name ="input" > /upload/upload3.jsp </ result >
< interceptor-ref name ="fileUpload" >
< param name ="maximumSize" > 409600 </ param >
< param name ="allowedTypes" >
application/vnd.ms-powerpoint
</ param >
</ interceptor-ref >
< interceptor-ref name ="defaultStack" ></ interceptor-ref >
</ action >
报错信息:
严重: Content
-
Type not allowed: file
"
intrl.txt
"
"
upload__138d8aca_120b73e9cf4__8000_00000002.tmp
"
text
/
plain
(五)文件的下载
download.jsp
<
s:a
href
="/MyStruts2/download.action"
>
download
</
s:a
>
DownloadAction.java
1
package
com.test.action;
2
3 import java.io.InputStream;
4
5 import org.apache.struts2.ServletActionContext;
6
7 import com.opensymphony.xwork2.ActionSupport;
8
9 public class DownloadAction extends ActionSupport {
10 public InputStream getDownloadFile() {
11 return ServletActionContext.getServletContext().getResourceAsStream(
12 "/upload/intrl.ppt");
13 }
14
15 @Override
16 public String execute() throws Exception {
17 return SUCCESS;
18 }
19}
20
web.xml中action配置
2
3 import java.io.InputStream;
4
5 import org.apache.struts2.ServletActionContext;
6
7 import com.opensymphony.xwork2.ActionSupport;
8
9 public class DownloadAction extends ActionSupport {
10 public InputStream getDownloadFile() {
11 return ServletActionContext.getServletContext().getResourceAsStream(
12 "/upload/intrl.ppt");
13 }
14
15 @Override
16 public String execute() throws Exception {
17 return SUCCESS;
18 }
19}
20
<
action
name
="download"
class ="com.test.action.DownloadAction" >
< result name ="success" type ="stream" >
< param name ="contentType" >
application/vnd.ms-powerpoint
</ param >
< param name ="contentDisposition" >
filename="intrl.ppt"
</ param >
< param name ="inputName" > downloadFile </ param >
</ result >
</ action >
结果:
class ="com.test.action.DownloadAction" >
< result name ="success" type ="stream" >
< param name ="contentType" >
application/vnd.ms-powerpoint
</ param >
< param name ="contentDisposition" >
filename="intrl.ppt"
</ param >
< param name ="inputName" > downloadFile </ param >
</ result >
</ action >