2

I've got Spring MVC application, and in my JSP I've got form which uploads files to my local Tomcat Server to directory "C:/apache-tomcat-7.0.56/photos".

Here is my Tomcat files structure:

C:\
  apache-tomcat-7.0.56\
    bin\
    conf\
    lib\
    logs\
    photos\  <-- I WANT TO READ FILES FROM THAT FOLDER
       photo1.jpg
       photo2.jpg
    temp\
    webapps\
       myapp\  <-- MY SPRING APPLICATION
         resources\
         WEB-INF\

How to do it? I can't access them through url 'localhost:8080/photos'. What's the url path to that files? I know I can do that with absolute path to my local drive like this:

<img src='C:\apache-tomcat-7.0.56\photos\photo1.jpg' />

but I want to read it through server url.

EDIT: I can't upload files into my webapp/resources directory from which I can easily read files, but when I redeploy app, files are deleted.

zeus
  • 51
  • 1
  • 5
  • You could create a separate web app that you could use for your photos. If necessary your context definition could use the crossContext attribute. Look at http://tomcat.apache.org/tomcat-7.0-doc/config/context.html – rickz Jan 09 '15 at 16:59
  • Alternatively, you could use a Servlet to serve the images. Look at http://stackoverflow.com/questions/4903762/serve-a-static-image-file-from-the-filesystem-in-a-servlet and http://stackoverflow.com/questions/8623709/output-an-image-file-from-a-servlet and http://balusc.blogspot.com/2007/04/imageservlet.html – rickz Jan 09 '15 at 17:10

3 Answers3

0

In order for the Tomcat to serve your images in a browser it needs to know that the files in this directory can be served. You would need to update web server configuration. There are many ways to solve it. Some of them are:

  1. Create photos directory in webapps directory
  2. Create directory somewhere on a server and add configuration file to /conf/Catalina/localhost directory
  3. Create a Controller which would read file and display it in a browser

Also see this: New uploaded files and Tomcat?

Community
  • 1
  • 1
jny
  • 8,007
  • 3
  • 37
  • 56
0

You can access your static resources as if they belonged to the same web application. All you need is to configure the access to your static resources and use a relative path in the img tag:

  • XML-based configuration

    <mvc:resources mapping="/photos/**" location="file:///c:/apache-tomcat-7.0.56/photos/" />
    
  • Java based Configuration

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/photos/**").addResourceLocations("file:///C:/apache-tomcat-7.0.56/photos/");
    }
    

And in your img tag:

  • <img src="<c:url value="/photos/photo1.jpg" />">
  • <img src="${pageContext.request.contextPath}/photos/photo1.jpg">
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
0

Easy and fast way to do it.

System.getenv("CATALINA_HOME") + "/webapps/your project name/images

Tomcat can't work without CATALINA_HOME, so we can use it without any additional config file or something else.

Raymond Reddington
  • 1,709
  • 1
  • 13
  • 21