You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Thomas Neidhart (JIRA)" <ji...@apache.org> on 2014/11/11 21:43:33 UTC

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

     [ https://issues.apache.org/jira/browse/FILEUPLOAD-258?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

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: https://issues.apache.org/jira/browse/FILEUPLOAD-258
>             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:
> {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)