commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Thomas Neidhart (JIRA)" <>
Subject [jira] [Resolved] (FILEUPLOAD-258) Empty files in mutipart requests aren't saved to disk
Date Tue, 11 Nov 2014 20:43:33 GMT


Thomas Neidhart resolved FILEUPLOAD-258.
       Resolution: Fixed
    Fix Version/s: 1.4

The getStoreLocation() method wrongly returned a File object in case the FileItem was stored
in memory. The javadoc was stating that it should return null in this case.

Fixed this in r1638377.

You can also use the method isInMemory() to determine if the file is stored on disk or solely
in memory.

btw. the problem you describe can also happen for file smaller than the sizeThreshold for
the DiskFileItemFactory.

> Empty files in mutipart requests aren't saved to disk
> -----------------------------------------------------
>                 Key: FILEUPLOAD-258
>                 URL:
>             Project: Commons FileUpload
>          Issue Type: Bug
>    Affects Versions: 1.3, 1.3.1
>            Reporter: Peter Cowan
>            Priority: Minor
>             Fix For: 1.4
> 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:
> {|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

View raw message