用ServletFileUpload的parseRequest进行文件上传? [英] File upload with ServletFileUpload's parseRequest?
问题描述
我在我的网络应用程序中上传了使用 input type =file
浏览的文件。问题是我得到 FileItem
列表大小为0,虽然我可以看到所有上传的文件信息下
请求
- > JakartaMutltiPartRequest
- > files属性
读取文件的代码
public InputStream parseRequestStreamWithApache(HttpServletRequest请求)
抛出FileUploadException,IOException {
InputStream is = null;
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
//这里的项目大小是0,我不知道为什么我没有得到我的文件上传在类型=文件
/ /但如果浏览请求调试器中我可以看到我的文件请求中的真实信息---> JakartaMutltiPartRequest ----->文件属性
Iterator iter = items.iterator(); (iter.hasNext()){
FileItem item =(FileItem)iter.next(); (!item.isFormField()){
is = item.getInputStream();
if
}
}
return is;
编辑: b
下面是JSP部分:
< form NAME = form1action =customer / customerManager!parseRequestStreamWithApache.actionENCTYPE =multipart / form-datamethod =post>
< TABLE>
< tr>
< th>上传文件< / th>
< td>
< input name =fileUploadAttrid =filePathtype =filevalue =>
< / td>
< td>
< Input type =submitvalue =uploadFile/>
< / td>
< / tr>
< / TABLE>
< / form>
正如我在同一个问题的评论中所说的,你之前发布的,这很可能是因为你已经解析过请求。
这些文件是请求主体的一部分,您只能分析一次。
更新:
我通常用这种方式使用commons-upload:
pre $
if(ServletFileUpload.isMultipartContent (request)){
ServletFileUpload fileUpload = new ServletFileUpload();
FileItemIterator items = fileUpload.getItemIterator(request);
//迭代项目
while(items.hasNext()){
FileItemStream item = items.next();
if(!item.isFormField()){
is = item.openStream();
}
}
}
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
-> files attribute
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;
}
EDIT:
Here is JSP part:
<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.
Update:
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进行文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!