Respond to the prompt to download a multi-page file before reading and saving the file
Is it possible to respond to a multipart / form-data request after reading the headers, but before reading the file data from the request body and saving it to a temporary file?
I have a file upload servlet that accepts large files and I would prefer that I be able to handle any pre-processing checks from the request header before uploading the entire file. For example, I need to check if the user is authenticated from the session before processing and storing the file. It seems like I have to call request.getPart()
or any method that parses the request body before sending the response, because otherwise the browser doesn't process the response. The problem is that with multipart / form-data requests it request.getPart()
parses the body and also reads the file data and stores it in a temporary file on disk.
How can I only read the headers and send a response before loading the file from the request body
Here's an example:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// check session (header cookie)
if (!isAuthenticated(request)) {
// This does not work before reading body
forwardLogin(request, response);
return;
}
// Actually parse request body and save the data to a temporary file on disk
Part filePart = request.getPart("file");
// process file
}
source to share
No one has answered this question yet
Check out similar questions: