Search code examples
servletsfile-uploadmultipart

When exactly are the files transmitted to the server?


If I have a servlet that handles file uploads, when does the server actually receive the files?

Are they already available when I invoke HttpServletRequest::getParts or will the individual files only be fetched when I actually call the corresponding Part::write?

Asked differently, is it my servlet's responsibility to implement a parallel upstream of all files or do I just need to worry about writing them all to disk?


Solution

  • Are they already available when I invoke HttpServletRequest::getParts

    Yes.

    or will the individual files only be fetched when I actually call the corresponding Part::write?

    Technically, files are not fetched. The server doesn't actually "download" files from the client. It's the client who sends the files as part of the request body to the server and the server has just to listen for these files and write them to a temporary storage before invoking the service method. The server will only invoke the service method when the request body is fully read. This is regardless of the request body content type. Hence the "Yes" on the previous question.

    Asked differently, is it my servlet's responsibility to implement a parallel upstream of all files

    Absolutely not. You don't at all need to synchronize anything on the HttpServletRequest. At most only on the HttpSession, but even this doesn't have a role here.

    or do I just need to worry about writing them all to disk?

    Yes, exactly that. Simply grab the Part, validate it and finally write it.

    See also: