The scenario is like : I have a page on which when we submit a file number, the client is given a link to download the file. But I don't know how to write one servlet which will decide which file to send to the client. This has to happen on clicking of a link, so I can't send parameters which can help me determine which file to download.
Please help.
you submit a file number;
you build a link based on that file number (it's a link for a GET. GET can have parameters, so you can put parameters in the URL path or in the query string);
user clicks the link;
the link is handled by a servlet;
the servlet uses the parameters from the GET request to determine what file to send;
you send the content of that file in the response.
Those are the steps.
Here is an example on BalusC's blog: http://balusc.blogspot.ro/2007/07/fileservlet.html
What you need to take care of is security:
don't expose the file directly as path on the server otherwise users can navigate the path to access other files on your application;
if users of your application have different rights, make sure you check those rights before returning the file, so that you a user can't access somebody else's files.
Related
I know solution of the above problem using HTML form in which user chooses a file from computer and upload it and then download it but how to do it without using HTML with the already given path of file.
Thank you For your concern in Advance.
I assume the scenario is that the user accesses your server from a browser.
In this case you will not be able to access the file system given file paths. Only the user can grant access to certain files by selecting them in a file upload input.
If the user uses a mobile/desktop application that sends the file to your server, you must implement the upload in the app itself.
I'm working on a web application at the moment using Tomcat and spring framework. A CSV file is stored on the server, the path to the file and the filename is stored in the database.
A search button on a webpage would list all records in database, if user click on one listed item, a saving dialog would be displayed and the file will be saved locally.
I want a dialog like this opened when user click on one item, how can I do it with httpServlet? I can think about a solution is set content-disposition type in response header to 'attachment'. It would force the browser to download the file instead of trying to display it in browser. But I want user to be able to select the type for file download. Please take a look at the image below
Setting the Content-Type response header to something that the browser is unable to render should prompt the user to save the file upon receiving the response. A value of application/octet-stream (arbitrary binary data) should do the trick.
However, since you expect the actual file content to be in different format depending on user choice, here's what you would need to do:
create a link/form on your html page that will allow user to select a type and make a request to URL with proper extension (like download/file.xls for XLS or download/file.csv for CSV).
in your servlet that handles these URLs, check the extension requested (easy) and then convert the file to expected format within your servlet (not so easy) and send it in response.
I would like to know if it's possible to download a file from a servlet and directly save it in a specific directory on the client (without asking the user to select a directory where to save the file, without prompting anything actually)?
No, That will break the security, by the way how would server know about the client file system ?
I want to send the link which contains a path to the any file that is located in Server whenever user clicks the button. How can I do this in JSP?
For example, my file is stored in web-inf/temp0001111/JspContext.pdf.
So, whenever user click this link, then this file get downloaded into browser and allow the user to save.and also need to specify the life time of this link is to only 3 days.
How can I achieve this?
the path under WEB-INF is not publicly visible so user can't simply GET it, you need to write a custom servlet which serve your purpose
I need to display uploaded files on a web application, the flow is as follows
User uploads file through web UI
Validation on image
Makes call to imageRepository.store( uploadedImage, user.getSite() )
The user wants the image displayed which adds <img src="${anUploadedImage.getUrl()}"/>
but that is where I'm stuck, what can getUrl() do? The simple solution is to put it someplace and let Apache serve the file, but then I can't use the application to ensure that one user isn't modifying the URL to view other users files, which in this case is important
You have a couple of options if you want to manage the path to the file.
You can create a service to return a file by ID. Essentially this controller-action (or servlet) would load the image back from the repository store and serialize it back to the user. This provides the most control.
You can create a filter that performs the control you want, then delegates back to the web server to perform the actual file serving. This still gives you control, but takes the responsibility off of your app to serialize the file efficiently.
The one thing I would want to verify (since I'm not sure what type the ${anUploadedImage} is) is what URL getUrl() is returning. If it is a File object, then getUrl() provides a file URL which is useless in the HTML. Essentially the web browser would go looking on the user's drive or report some sort of fishy activity.
The URL you include in your HTML needs to point to where your service will send the desired image.