java 使用Apache commons文件上传API在Jsp中上传多个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5887214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple file upload in Jsp using Apache commons file upload API
提问by saurabh
I am not able to upload multiple files.I am using this code. Here itr.hasNext() is returning false.
我无法上传多个文件。我正在使用此代码。这里 itr.hasNext() 返回 false。
FileUpload.jsp
文件上传.jsp
<%@ page import="java.util.*" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<center>
<table border="2">
<tr>
<td>
<h1>Your files uploaded </h1>
</td>
</tr>
<%
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
System.out.println("isMultipart="+isMultipart);
System.out.println(config.getServletContext());
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
System.out.println(upload.parseRequest(request));
List items=null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem)(itr.next());
out.println("itr");
if (item.isFormField()) {
try{
String field=item.getFieldName();
String value=item.getString();
System.out.println("field="+value);
}
catch(Exception e){}
}
else {
try {
out.println("nor done");
String itemName = item.getName();
out.println("1done");
File savedFile = new File("/home/saurabh/assignments/"+itemName);
item.write(savedFile);
out.println("done");
} catch (Exception e) {
e.printStackTrace();
}
}
}
%>
</table>
</center>
html file is:-
html文件是:-
<html>
<head><title>Upload page</title></head></p> <p><body>
<form action="FileUpload.jsp" method="post" enctype="multipart/form-data" name="form1" id="form1">
<center>
<table border="2">
<tr>
<td align="center"><b>Multipale file Uploade</td>
</tr>
<tr>
<td>
Specify file: <input name="file" type="file" id="file">
<td>
</tr>
<tr>
<td>
Specify file:<input name="file" type="file" id="file">
</td>
<tr>
<td>
Specify file:<input name="file" type="file" id="file">
</td>
</tr>
<tr>
<td align="center">
<input type="submit" name="Submit" value="Submit files"/>
</td>
</tr>
</table>
<center>
</form>
</body>
</html>
How is this caused and how can I solve this?
这是怎么引起的,我该如何解决?
回答by BalusC
You're parsing the request twice. The first time to print the items and the second time to really get the items for processing. This isn't going to work. It can be parsed only once.
您正在解析请求两次。第一次打印物品,第二次真正拿到物品进行处理。这行不通。它只能解析一次。
Remove the following useless line
删除以下无用的行
System.out.println(upload.parseRequest(request));
If you really intend to print the parsed items for some reason, then you should do
如果您出于某种原因确实打算打印已解析的项目,那么您应该这样做
System.out.println(items);
afteryou have parsed the request once inside that try
block.
在该try
块中解析一次请求之后。
Unrelatedto the concrete problem, a JSP is not the best place for this job. Do this job in a servlet and collect the results in some map/bean which you put in the request scope and then forward the request to a JSP to display the results.
与具体问题无关,JSP 不是这项工作的最佳场所。在 servlet 中完成这项工作,并在您放入请求范围的某个 map/bean 中收集结果,然后将请求转发到 JSP 以显示结果。