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?
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.
You are correct each request is a different thread, however they should be linked to the same session.Store your file in the session and all will be sorted.
Related
I have a web application where users can upload files. So I want to know if it's possible if a user picks a file to upload then closes the browser, to get back this same file and upload it directly without user action or just a dialog box to confirm, after the user comes back to the application.
I already thought about local storage but i can't store images usually exceeding the limit of 10 MB. But i don't know if storing the path of the file with local storage will enable me to direct upload it after by using the path stored.
If storing the path of the file with local storage will enable me to
direct upload it after by using the path stored
You cant use the path in the local storage to upload the file.You can't do this without user intervention,which would be a huge security hole. Think about visiting a web page and it being able to grab and upload any of your files without you doing a thing.
Please consider this scenario: User should send some files to server and every user has a id. Every user must send the files once, not twice. If a user sends the files and he want to send them again, the file uploading page won't be shown to him. But there is a problem: the uploading process takes time and when the files are being uploaded, user can refresh the page and the page is shown to him (because the files has been not uploaded yet) and then he can upload files again and again!
The client side code using jsp and I cant change it. Now I want to prevent refreshing page in server side. For this purpose, I used a middle page: when a user want to see the file uploading page he goes to this middle page (servlet), this middle servlet puts a flag into user's session and redirect him to file uploading servlet. The file uploading servlet checks the flag and if it exist, the page is shown to him and then flag will be removed. Whenever a user try to refresh the file uploading page, the flag doesn't exist and he will be redirected to an error page.
This was my solution, but it has a serious problem: when user is redirected from middle servlet to file uploading servlet, its request and response will be destroyed and I cant access his request (The user send me some information with his request and I need those in file uploading servlet).
1 - Is there a way to clone request object? (so that I can put in session and after redirect, I can retrieve that)
2- If the answer is no, Is there a better solution for my problem?
Any suggestions would be gratefully appreciated.
Edit:
Please note that I don't want to save the request parameters/attributes into session.
Use window.stop(); in your code.
I have following scenario:
number of user can request download is 100
I am creating excel file in server by name "temp.xlsx" containing user specific information and then sending file in response.
what will happen if 10 user request file ?
server will process request synchronously one after other ? or
server will process request paralleled
but in 2nd case what will happen as server will try to create and read file "temp.xlsx" for all user request, please note file is expected to have user specific information not common for all user.
Above code is already in DEV environment ,I just want to understand how it will behave in PROD for multiple user, Thanks in Advance.
Technologies: Java REST services, Apache POI, Server Karaf container
Requests will most probably be processed in parallel. This depends on your container configuration. (See for example this question about Jetty thread pool size in Karaf)
You should
create file with unique name like temp-$username-$timestamp.xlsx
send the file to the user with desired name in the HTTP header
I am using icefaces, ace:fileEntry component for uploading files from client and those files are being saved in file system, also I have my managedBean marked as sessionScope and it make sense that when user logs out and then log in back, he/she will not see their uploaded files.
I want to do two things:
When user uploads file then I want to save that file to database also along with user Id or user session information.
When user logs out and logs back in again then he/she should be able to see his/her uploaded files, currently files are not saved after user logs out?
User can only see his or her uploaded files and not any random files.
My approach:
I am thinking of creating new table in database with file id, user id and file and so when file gets uploaded from client using ace fileEntry component then via my Hibernate, I would save file and user related information that I get from session to database, now when user logs out and logins back, I make query to database table with userId to get all his or her files.
Any thoughts or better suggestions for implementing this feature?
Create a separate subfolder for every user based on user ID.
/var/webapp/uploads/[userid]
You can use File#mkdir() to create a subfolder if necessary.
File uploads = new File("/var/webapp/uploads");
File userUploads = new File(uploads, String.valueOf(user.getId()));
userUploads.mkdir();
// ...
Then to get all uploaded files afterwards, just do
File[] allUserUploads = userUploads.listFiles();
// ...
This way you don't need another DB table and the users will always only see their own files.
i have got a situation now.
I need to develop a webpage where user can select a file to upload and before uploading the file to server i need to check first few lines of the file whether the data is valid or not and if the data is valid then upload the file, if not through an error message.
the file will be text file.
thanks,
Sandeep
HTML/Javascript does not offer a way of reading the contents of a local file. You must either upload it and check it in the server.
If you really want a client side check, you then must build a signed applet(or even ActiveX) to run in your webpage and handle the upload instead of using plain HTML.
You should perform your validation on the server side, right before you perform the upload.