Issue
I am uploading a file in the Servlet
, doing the necessary operations on this file, and then downloading this processed file to the client.
I have two forms multipart / form-data
. The first one uses the post method to upload files. The second one is using the get method to download the file.
DoPost ()
-> The user selects and uploads the file. This file is processed and stored in the specified location. And this file is kept as a global variable of type File
.
DoGet ()
-> Downloading the global File
.
When a user uploads a file from Web browser, another user can download it from another Web browser.
I suppose this may be the reason that File is global. I tried ThreadLocal
, but it did not work because doPost
and doGet
are not in the same thread.
Is there a way you can suggest?
Solution
If the user will upload and download the file in the same session, then you can save the filename in HttpSession during the POST operation. During the GET, you check for the existence in HttpSession. Let the user download the file only if it exists in HttpSession.
If the user could have uploaded the file, then logged out and logged back in and download the file, then you will need to save the file information in some persistence storage like a database.
Answered By - sudhir shakya
Answer Checked By - Senaida (JavaFixing Volunteer)