用ServletFileUpload的parseRequest进行file upload?

我上传我的web应用程序中input type="file"浏览的input type="file" 。 问题是我得到FileItem列表大小为0,虽然我可以看到所有上传的文件信息

request – > JakartaMutltiPartRequest – >文件属性

这里是读取文件的Java代码

 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> 

正如我在同一个问题的评论中所说,你之前发布的,这很可能是因为你之前已经parsing了请求。 这些文件是请求正文的一部分,您只能parsing它一次。

更新:

我通常以这种方式使用commons-upload:

 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(); } } } 

你应该检查多部分的内容

 boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List items = upload.parseRequest(request); Iterator iterator = items.iterator(); while (iterator.hasNext()) { FileItem item = (FileItem) iterator.next(); if (!item.isFormField()) { String fileName = item.getName(); String root = getServletContext().getRealPath("/"); File path = new File(root + "/fileuploads"); if (!path.exists()) { boolean status = path.mkdirs(); } File uploadedFile = new File(path + "/" + fileName); item.write(uploadedFile); } } } catch (Exception e) { 

你上传的文件有多大? 您可能会超过默认阈值。 我认为默认是10K

 factory.setSizeThreshold(maxSizeYouWantToHandle); 

如果您使用的是weblogic 12,那么请检查patch_wls1211是否被insatlled。 我遇到同样的问题,并在应用patch_wls1211后解决。