post - Java request getInputStream return empty stream

问题描述:

I've got a problem with getInputStream from doPost request and setting filename before. It's giving me a file with right filename but empty file 0kB. If I'll comment setting fileName than I'll get not empty file.

Part offers a getInputStream() method, so you should use that instead of request.getInputStream(); when you're dealing with parts.

However...

Part.write() is a convenience method to write this uploaded item to disk, so you can just use part.write(SAVE_DIR + part.getSubmittedFileName()); and it's all handled for you (note that getSubmittedFileName() is available only for Servlet 3.1).