23

I need to map an directory containing images which resides outside tomcat webapps folder, so that application can serve those images.

I am making a J2EE Web application running under tomcat 6. User can upload/delete images in the application. Currenly I store these images to a directory under application's WebContent folder, but I want to take it outside the tomcat (e.g. C:/test/images).

I need to know how to I configure tomcat so that if I access URL http://.com/images/abc.jpg , it serves the image from directory C:/test/images

Thanks,

3 Answers3

35

Add a <Context> tag in server.xml, inside the <Host> tag:

<Context path="/images" docBase="C:/test/images/" />

Docs will be accessible at http://localhost:8080/images

MonoThreaded
  • 11,429
  • 12
  • 71
  • 102
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
  • This is not worked... Can you please be more specific?? I can not access via this trick. I am using Liferay – Pradip Bhatt May 27 '13 at 13:07
  • I don't know liferay. Is it based on tomcat? – Maurice Perry May 27 '13 at 13:20
  • Yes, it is based on Tomcat. I am using Liferay 6.2.0 m2 version which used tomcat 7. As per my knowledge this is the same normal tomcat related scenario. Help me please – Pradip Bhatt May 28 '13 at 13:23
  • 2
    I follow the same. But at the calling time like http://localhost:8080/images............ It will give Error 404 The requested resource (/images/) is not available. Is it liferay issuue??? – Pradip Bhatt May 30 '13 at 13:16
  • 1
    OK, I've also tested on liferay 6.1.1: it works like a charm; however, you need to specify the file you want to display, not the directory. – Maurice Perry May 30 '13 at 13:19
  • It works for accessing, but i need upload too. Is that work for upload? I tried in my php script, got path error. – Ulfsark Oct 22 '15 at 11:42
  • @Lars well the question is about tomcat, not php. Are you using both tomcat and apache? – Maurice Perry Oct 22 '15 at 17:05
  • @MauricePerry Nope, just tomcat. – Ulfsark Oct 23 '15 at 08:14
  • @Lars then you must write an upload servlet in java to deal with uploads – Maurice Perry Oct 23 '15 at 12:36
  • This works but how can i provide security for accessing resource – Viswa Apr 19 '16 at 11:16
  • @MauricePerry can you please provide more details, i am just having context tag with doc base pointed to external location. any reference link pls. – Viswa Apr 19 '16 at 12:40
  • When I use absolute path (path="d:/Programs/Tomcat/webapps/images/"), it show error: javax.management.MalformedObjectNameException: Invalid character ':' in value part of property Then I change to use relate path (path="../images/"), it works ! Hope my experience will help – nguyen Dec 12 '18 at 04:59
5

in Tomcat8 you can also add PotsResources to you META-INF/context.xml as follow :

<Context>
    <Resources allowLinking="false">
        <PostResources readOnly="false"
                       className="org.apache.catalina.webresources.DirResourceSet"
                       base="path-to-your-local-folder"
                       webAppMount="/images"/>
    </Resources>

    ...
</Context>
4

I had the same issue but found a solution.

If you are using Eclipse and a Tomcat plugin then please note that the Eclipse Tomcat plugin creates a separate CATALINA_BASE under the Eclipse workspace directory.

You can go to this location and you will find server.xml. Use that server.xml and it will work.

My actual tomcat directory is:

C:\apache-tomcat-7.0.62x64\apache-tomcat-7.0.62\conf

and my Eclipse Tomcat server uses:

C:\workspace\JSF\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf 

Use the workspace path and server.xml from this location.

Add this in server.xml inside the host tag:

< Context docBase="D:/personal"  path="/images" />

and it will work if D:/personal has 1.png, and then the url http://localhost:8080/images/1.png will load the image.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Brainwash
  • 188
  • 2
  • 10