Harry
Harry

Reputation: 4773

Download file from physical path in java

I have uploaded files using spring MVC file upload functionality and files are getting uploaded in /home/myfolder/images/ folder. Now I want to download these file from this physical path. For test in my jsp I have written the following line

   <a href="<%=request.getSession().getServletContext().getRealPath("/home/images/image.jpg")%>" >download </a>

but when I click on this link it redirects me to the URL

    http://localhost:8080/home/myfolder/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/myproject/home/images/image.jpg.

How can I download the image saved. Please let me know if there is anything else you need from me to resolve this.

Upvotes: 1

Views: 3709

Answers (2)

Solubris
Solubris

Reputation: 3753

Try using this:

<!-- Handles HTTP GET requests for /resources/ ** by efficiently serving up static resources -->
<mvc:resources mapping="/images/**" location="file:/home/images"/>
<!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource requests to the container's default Servlet -->
<mvc:default-servlet-handler/>

In the jsp:

<img src="/images/image.jpg" />

Here is the thread: Spring : serving static resources outside context root

Upvotes: 4

lost
lost

Reputation: 1459

Files outside your Webapps folder can't be served by the application container.

Some possibilities:

1) Upload to a folder that is below your Webapps folder in your application container. E. g.

Given your Webapps folder is /home/myfolder/Tomcat/webapps/myApp/ You could upload to /home/myfolder/Tomcate/webapps/myApp/upload/

This way the uploaded files can be served by the application container.

2) Make the upload folder accessible to the application container by means of symbolic links. You might need to change your container's config in order to make this work.

3) If you use a webserver in front of your application container (like httpd) let the webserver serve the static files.

4) Write your own servlet that reads from an arbitrary file and serves the contents to the client.

Upvotes: 2

Related Questions