Spring MVC file upload via MultipartFile gives sometimes(!) empty InputSteam

we encountered a strange problem with our simple file upload system.

The setup is: Spring 3.2.2, commons fileupload 1.3, commons io 2.4. We're actually running straight from Eclipse with an external Tomcat 7.0.40. It's been tested on Mac OS X 10.8 and 10.6.

Here's the code:

public void saveFile(MultipartFile file, String description) {
    System.out.println(file.getOriginalFilename());
    System.out.println(file.getSize());
    OutputStream out = new FileOutputStream("someFileName");
    IOUtils.copy(file.getInputStream(), out);
    out.flush();
    out.close();
}

It outputs the correct file name and also the correct file size!

Now when it comes to writing the file to disk, it produces a 0 byte file. This especially happens with .docx files (in about 95% of the cases). It seems to depend on the file. Images and PDF documents seem to work always.

There's no exception or any other hint. The debugger says that the InputStream is empty (not null , just empty).

Any explanation for this behaviour?


I would check the following:

  • Does your server has any antivirus / malware software that prevents unknown program to write docx into the filesystem.
  • Does the OS user that runs your server (hence writes the file to FS) has sufficient permission to do so?
  • InputStream empty could means the uploaded document starts with EOF. Also check the content of the docx itself maybe it's corrupted / some file encoding problem causes multibyte character interpreted as EOF. I would probably debug the docx by printing first few bytes in hex
  • 链接地址: http://www.djcxy.com/p/48774.html

    上一篇: okhttp登录拦截器在多部分

    下一篇: 通过MultipartFile上传Spring MVC文件有时会产生(!)空的InputSteam