使用ServletFileUpload的parseRequest上传文件?

时间:2012-10-24 12:09:46

标签: jsp servlets file-upload apache-commons-fileupload

我在我的网络应用中上传了我用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>

4 个答案:

答案 0 :(得分:12)

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

<强>更新

我通常以这种方式使用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();
        }
    }
}

答案 1 :(得分:3)

您应该检查多部分内容

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) {

答案 2 :(得分:0)

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

factory.setSizeThreshold(maxSizeYouWantToHandle);

答案 3 :(得分:0)

如果您使用的是weblogic 12,请检查是否已安装patch_wls1211。我遇到了同样的问题,并在应用patch_wls1211后解决了。