1

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Harry
  • 4,705
  • 17
  • 73
  • 101

2 Answers2

4

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

Community
  • 1
  • 1
Solubris
  • 3,603
  • 2
  • 22
  • 37
  • Thanks Lithium, yes I can do this but when I'll create my war file for production all the local images will be added in my production war and I need to take the backup of all my images on production. I haven't tried this yet but what you say don't I have to do the above steps? – Harry Sep 02 '12 at 07:32
  • Yeah. This dir is now a living dir. So needs to maintained like a db. I would not add it to the build process though, as in the future you might not want to copy all you testing images to production. Also, in the future, you will prolly want to copy images down from prod to your test env. In this case, you could have 2 dirs configured: location="file:/home/images/prod,file:/home/images/test" and then use spring profiles to point file uploads to the appropriate place. And manually copy images from /home/images/test to /home/images/prod. – Solubris Sep 02 '12 at 07:40
  • Hi Lithium, I was trying to implement above solution In my spring-servlet.xml I have so while saving my file my path will be /home/images/ right?. It saving the images at /home/images/ folder but I am not able to access it by above path i.e – Harry Sep 03 '12 at 14:19
  • sorry my code in spring-servlet is – Harry Sep 03 '12 at 14:45
  • Try relative path to image: , or prefix the path with the web application context. – Solubris Sep 03 '12 at 15:06
  • Its not working even. So What I am doing is right? My images are uploaded in /home/images/ folder and my mapping in spring-servlet.xml is and in jsp I am access it like image thats it right? or I am missing some thing? – Harry Sep 03 '12 at 15:19
  • You might need to add this immediately after the resources tag: Will edit post with this info. – Solubris Sep 04 '12 at 13:37
  • I am really sorry to bother you but it still not working. Now I am just trying to display image my image is inside the home/images/ folder and trying to display it in jsp like image – Harry Sep 04 '12 at 15:34
  • In My spring-servlet I have done – Harry Sep 04 '12 at 15:43
2

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.

lost
  • 1,449
  • 1
  • 13
  • 17
  • Hi Lost thanks for your response. I have uploaded the files in myProject/upload/ folder and its saved there but when I try to access using http://localhost:8080/myProject/upload/image.jpg. It throws Http status 404 error. Do I need to do some setting to access this. What setting I need to do you said "Make the upload folder accessible to the application container by means of symbolic links" what does that mean? – Harry Sep 02 '12 at 06:50
  • I have just created myProject and uplaod folders in tomcat/webapps – Harry Sep 02 '12 at 06:52
  • In `tomcat/webapps` what's the folder of your webapp? (I. e. the context your webapp is deployed in) Is it `myProject`? – lost Sep 02 '12 at 06:57
  • My webapp folder is not there I am running my project using eclipse. I have just created two folders in webapps folder – Harry Sep 02 '12 at 07:02
  • sorry I am not getting you completly. I am new to this – Harry Sep 02 '12 at 07:03
  • Sorry, just realized the eclipse integration. Check the other answer, it sounds more promising. – lost Sep 02 '12 at 07:20