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.
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
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.
Related
I'm trying to add a robots.txt file to my Spring Java web application, but when I put the file in the web app folder, the text is only visible through localhost:8000/context/robots.txt instead of localhost:8000/robots.txt (the URL that google crawler would check), where I receive a 404 error. I've tried putting the robots.txt file at src, WEB-INF, the project's root folder, etc. with the same results.
I've also tried changing my application-context.xml redirecting "/robots.txt" to my file with <mvc:resources mapping="/robots.txt" location="/robots.txt" order="0"/> for all the previous locations and still doesn't work. The index page of my app would be domain.com/context/firstpage.ac so every page is accessible under that context and as I said also the robots.txt file is showed there when I put it on the web app folder.
Is there any way to separate my robots file from the others and put it at localhost:8000/robots.txt or it isn't possible since my app is deployed under that specific context?
Folder-structure:
Is it possible to serve static resources from within a compressed file using Spring MVC? Something like this.
I have some data packaged into individual JSON files (e.g. 123.json, 1634.json, etc.) and and currently serving them via
<mvc:resources mapping="/resources/**" location="/resources/" />
The files are under .../resources/dataFiles/. So a user can go to http://mywebsite.com/resources/dataFiles/123.json to retrieve the data for entity 123.
However, I have ~10,000 JSON files. It would be great if I could compress them under one file (.../resources/dataFiles/entities.zip) and tell Spring to serve the individual JSON files from within the compressed file.
So the user would still go to http://mywebsite.com/resources/dataFiles/123.json but the only file under .../resources/dataFiles/ is entities.zip.
I'm using Tomcat 7.0 if this question is outside the scope of the MVC framework.
I'm not sure if there's a Spring out-of-the-box component that does that, but you can create an independent Servlet to handle the incoming requests for static resources, then this servlet would parse the file name, and dynamically read from the zipped file the correct Zip Entry and return the content to the OutputStream of the Response. Take a look at:
sample code
I am running running a webapp in tomcat 7, and I am trying to upload images into the webapps/{webappname}/images folder via a grails controller (similar to a java servlet). How do I get the path of this images folder in the servlet so I can store the uploaded files there?
you can use:
servletContext().getRealPath("/"); to get the physical path to your webapp base directory
However, be careful uploading anything into it as that area can usually get wiped out by re-deployment. It is generally advisable to upload data to a separate data-specific directory outside your webapp or to a persistent store (DB)
How is this achieved, I have everything mapped from / to the dispatcher Servlet and the css is in the webapp folder. when clicking on the css link href in the source I get a tomcat error.
Do I need to create a #RequestMapping for css?
Css are served without the need of dispatcher (unless you configure it to serve css). Just put them in some folder in your webapp dir and reference them as /YourAppContext/path-to-your-css in your pages.
For example in typical (non-maven) webapp it's inside WebContent/css
Assuming your app's context name is MyApp and you want to reference WebContent/css/main.css
you should put something like this in jsp:
<c:url value="/css/main.css" />
or uglier using
/MyApp/css/main.css
as your css url
CSS are client files, not server files. All client files (CSS, Javascript, etc.) have nothing to do with Spring MVC, which acts on servlets (on the server). SO you add your CSS exactly the same way you would if you weren't using Spring MVC.
In my web application one of my pages is uploading a photo to the path
/usr/local/rac/picture-name-goes-here
The photo is uploading fine, but I need to access it in another page and when I try to access it from my JSP, it will not show up, I am guessing my path to the photo is incorrect
The code in my JSP to access the photo looks like the following.
<tr>
<td>
<img src="/usr/local/agent/photo-name-here.jpg"/>
</td>
</tr>
Am I incorrect with this path to the photo?
If it helps, I am running my web application from Tomcat which is in the directory
C:\Tomcat6
I will eventually be moving this over to a linux machine and expect to share the same path to the photo.
There is one major misconception here. HTML is executed by the webbrowser, not by the webserver. The webbrowser downloads HTML, scans for any resources which needs to be downloaded as well (CSS, scripts, images, etc) and fires a new HTTP request for each of them. All resources should point to a valid URL, not to some local disk file system path which the client machine has no notion of.
There are basically two ways to solve this "problem":
Add a new Context to Tomcat's /conf/server.xml:
<Context docBase="/usr/local/agent" path="/images" />
This way they'll be accessible through http://example.com/images/... and you'll be able to use the following <img>
<img src="/images/photo-name-here.jpg"/>
Create a Servlet which basically gets an InputStream of the image and writes it to the OutputStream of the response along a correct set of headers. You can find here a basic example of such a servlet and here a more advanced example. When the Servlet is mapped on /images/* in web.xml, the images are accessible by http://example.com/contextname/images/... and you'll be able to use it as follows (assuming that the JSP/HTML file is located in the context root):
<img src="images/photo-name-here.jpg"/>
src="/usr/local/agent/photo-name-here.jpg" <- this URL is a local address in your server, to show up your images you have to set a valid HTTP address like:
http://www.yourdomain.com/images/photo-name-here.jpg
To accomplish that you will need to upload the foto to a localpath that is inside in your www root folder.
If your webapp is installed in
/home/apache/www/website/
you will upload your images to a folder like:
/home/apache/www/website/images/
and then your HTTP address will be
http://www.yourdomain.com/images/photo-name-here.jpg
I got a little confuse with your two paths in /usr/ and C:\Tomcat
I encourage you to put the upload localpath folder parametrized, so you will be only modifying the config file instead of every function or method that access to that local path.