commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Peter Cowan (JIRA)" <>
Subject [jira] [Created] (FILEUPLOAD-258) Empty files in mutipart requests aren't saved to disk
Date Mon, 15 Sep 2014 11:26:33 GMT
Peter Cowan created FILEUPLOAD-258:

             Summary: Empty files in mutipart requests aren't saved to disk
                 Key: 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

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:

final DiskFileItemFactory factory = new DiskFileItemFactory() {
  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;

      protected File getTempFile() {
        File tempFile = super.getTempFile();
        try {
        catch (final IOException e) {
          //Handle appropriately
        return tempFile;

ServletFileUpload upload = new ServletFileUpload(factory);

This message was sent by Atlassian JIRA

View raw message