You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Peter Cowan (JIRA)" <ji...@apache.org> on 2014/09/15 13:26:33 UTC

[jira] [Created] (FILEUPLOAD-258) Empty files in mutipart requests aren't saved to disk

Peter Cowan created FILEUPLOAD-258:
--------------------------------------

             Summary: Empty files in mutipart requests aren't saved to disk
                 Key: FILEUPLOAD-258
                 URL: https://issues.apache.org/jira/browse/FILEUPLOAD-258
             Project: Commons FileUpload
          Issue Type: Bug
    Affects Versions: 1.3.1, 1.3
            Reporter: Peter Cowan
            Priority: Minor


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:

{code:title=Snippet.java|borderStyle=solid}
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);
{code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)