一次将多个文件上传到 Struts2 @Action [英] Upload multiple files at once to a Struts2 @Action
问题描述
我需要使用 Struts2 一次上传1000 个文件"或一个包含所有文件的 zip 文件".(通过 1000 个文件或 zip 文件,我的意思是我需要将所有文件上传到系统上,无论用户一次选择所有 1000 个文件还是将它们压缩并作为单个文件上传,所以我正在寻找一种更容易实现和更高效的)
I need to upload "1000 files" or "a zip file including all the files" at once using Struts2. (By 1000 Files or a zip file, I mean I need all the files to be uploaded on the system it does not matter if user choose all the 1000 files at once or zip them and upload as a single file, so I am looking for the one which is easier to implement and more efficient)
我已阅读以下答案,但没有一个适合目的.
I have read the following answers but none of them suits the purpose.
使用下面的代码,当我使用一个简单的List文件时;它显示列表的名称,但是当我使用列表文件时,它不显示任何内容并且我无法上传文件.
Using the following code, when I use a simple List files; it shows name of lists, but when I use List files it does not show any thing and I can not upload the files.
上传.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="upload" enctype="multipart/form-data" method="post">
<input name="files" type="file" multiple/>
<button type="submit"/>
</form>
</body>
</html>
上传.java
@Action
public class upload implements Addresses {
private List <File> files = new ArrayList <File> ();
public String execute(){
return "success";
}
public upload() {
System.out.println("in upload 1");
for(int i=0;i<files.size();i++)
System.out.println(i + ")" + files.get(i));
System.out.println("in upload 2");
}
public List <File> getFiles() {
return files;
}
public void setFiles(List <File> files) {
this.files = files;
for(int i=0;i<files.size();i++)
System.out.println(i + ")" + files.get(i));
// File fileToCreate = new File("c:image", files.get(i).toString());
// FileUtils.copyFile(files.get(i), fileToCreate);
}
}
输出
in upload 1
in upload 2
推荐答案
我建议你使用 Struts 标签而不是普通的 HTML 标签,并扩展 ActionSupport(返回其 Result 常量而不是手动组合结果字符串,如result").
I suggest you to use Struts Tags instead of plain HTML tags, and to extend ActionSupport (returning its Result constants instead of manually composing the result String, like "result").
也就是说,这是一个经过测试和工作的示例.
注意:它不适用于旧版本的 IE,但由于您在自己的问题中使用 HTML5,我敢打赌您已经知道它并且您不是针对旧版本的 IE.
Note: It won't work on old versions of IE, but since you are using HTML5 in your own question, I bet you already know it and you are not targeting old IE.
JSP
<%@page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Multiple File Upload Example</title>
</head>
<body>
<s:form action="upload" enctype="multipart/form-data" >
<s:file name="files" multiple="multiple" />
<s:submit value="Upload files" />
</s:form>
</body>
</html>
注意 multiple="multiple"
部分:即使 在官方文档中,<s:file/>
标签的属性没有定义,因为 Struts 2.1 它是允许的,因为
Note about the multiple="multiple"
part: even if in the official documentation, that attribute for the <s:file />
tag is not defined, since Struts 2.1 it is allowed because of
允许的动态属性:真
这意味着它将按原样在 JSP 上绘制,不受 Struts 的任何干扰.这样 Struts 就不需要在每次 HTML5 提供新特性时更新它的标签;您也可以将 foo="bar"
放入允许动态属性 (<s:file/>
, <s:textarea/>
等),您会在 HTML 中找到它.
this means that it will be drawn on the JSP as-is, without any interference by Struts. This way Struts doens't need to update its Tags each time HTML5 provides a new feature; you could put foo="bar"
too in a tag that allows Dynamic Attributes (<s:file />
, <s:textarea />
, etc), and you will find it in the HTML.
动作
public class Upload extends ActionSupport{
private List<File> files;
private List<String> filesContentType;
private List<String> filesFileName;
/* GETTERS AND SETTERS */
public String execute() throws Exception{
System.out.print("
---------------------------------------");
int i=0;
for (File file : files){
System.out.print("
File ["+i+"] ");
System.out.print("; name:" + filesFileName.get(i));
System.out.print("; contentType: " + filesContentType.get(i));
System.out.print("; length: " + file.length());
i++;
}
System.out.println("
---------------------------------------
");
return SUCCESS;
}
}
然后你可能想要设置请求的最大大小,以及每个文件的最大大小,就像这里描述的那样:
Then you may want to set the maximum size of the Request, and the maximum size of each single file, like described here:
Struts.xml - 最大多部分大小:
Struts.xml - Max multipart size:
<constant name="struts.multipart.maxSize" value="20000000" />
Struts.xml - 文件的最大大小(全局到包,或本地到动作)
Struts.xml - Max size of a file (globally to a package, or locally to an Action)
<interceptor-ref name="fileUpload">
<param name="maximumSize">10485760</param>
</interceptor-ref>
这篇关于一次将多个文件上传到 Struts2 @Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!