问题描述
我上传了我在 Web 应用程序中使用 input type="file"
浏览的文件.问题是我得到 FileItem
列表大小为 0 虽然我可以在
I upload the file which I browse with input type="file"
in my web App. The issue is I get the FileItem
list size as 0 though I can see all uploaded file info under
request
-> JakartaMutltiPartRequest
-> 文件属性
request
-> JakartaMutltiPartRequest
-> files attribute
这里是读取文件的java代码
Here is java code that reads the file
public InputStream parseRequestStreamWithApache(HttpServletRequest request)
throws FileUploadException, IOException {
InputStream is = null;
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
// here the item size is 0 ,i am not sure why i am not getting my file upload in browser with type="file"
// but If inspect request in debugger i can see my file realted info in request--->JakartaMutltiPartRequest----->files attribute
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
is = item.getInputStream();
}
}
return is;
}
这里是 JSP 部分:
<form NAME="form1" action="customer/customerManager!parseRequestStreamWithApache.action" ENCTYPE="multipart/form-data" method="post" >
<TABLE >
<tr>
<th>Upload File</th>
<td>
<input name="fileUploadAttr" id="filePath" type="file" value="">
</td>
<td >
<Input type="submit" value ="uploadFile"/>
</td>
</tr>
</TABLE>
</form>
推荐答案
正如我在您之前发布的同一问题的评论中所说,这很可能是因为您之前已经解析过请求.这些文件是请求正文的一部分,您只能解析一次.
As I said in a comment to the same question, you posted earlier, this is most likely because you have parsed the request already before.The files are part of the request body and you can parse it only one time.
更新:
我通常以这种方式使用commons-upload:
I usually do use commons-upload in that way:
if (ServletFileUpload.isMultipartContent(request)) {
ServletFileUpload fileUpload = new ServletFileUpload();
FileItemIterator items = fileUpload.getItemIterator(request);
// iterate items
while (items.hasNext()) {
FileItemStream item = items.next();
if (!item.isFormField()) {
is = item.openStream();
}
}
}
这篇关于使用 ServletFileUpload 的 parseRequest 上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!