Uploaded image for project: 'Commons FileUpload'
  1. Commons FileUpload
  2. FILEUPLOAD-258

Empty files in mutipart requests aren't saved to disk

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.3, 1.3.1
    • Fix Version/s: 1.4
    • Labels:
      None

      Description

      If a multipart request contains an empty file, ServletFileUpload#parseRequest does not create a corresponding temporary file on the file system. If any operations are performed on the corresponding file object from DiskFileItem#getStoreLocation() a FileNotFoundException is thrown.

      FileUploadBase#parseRequest() copies the multipart file input stream to the temporary file output stream with Streams#copy(). This method only creates the temporary file on the file system if data is written to the file's output stream. Since the input stream is empty, write is never called on the output stream and the temporary file is not created.

      I've used the following snippet to overcome this issue. This ensures the temporary file is created on the file system prior to writing:

      Snippet.java
      final DiskFileItemFactory factory = new DiskFileItemFactory() {
        @Override
        public FileItem createItem(final String fieldName, final String contentType, final boolean isFormField, final String fileName) {
          return new DiskFileItem(fieldName, contentType, isFormField, fileName, getSizeThreshold(), getRepository()) {
            private static final long serialVersionUID = 1L;
      
            @Override
            protected File getTempFile() {
              File tempFile = super.getTempFile();
              try {
                tempFile.createNewFile();
              }
              catch (final IOException e) {
                //Handle appropriately
                ...
              }
              return tempFile;
            }
          };
        }
      };
      
      ServletFileUpload upload = new ServletFileUpload(factory);
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              prcjac Peter Cowan
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: