2

I have made a folder outside the webcontent to save the uploaded images by user. Initially i tried to access those files directly by passing the location in the "src" tag, but unable to fetch it. After researching i found that i have to set the path using in "conf/server.xml" file inside the tag. Although i have made all these changes i am unable to access the file.

1)My Tomcat is Installed at E:\my work\Tomcat

2)I am having my webroot at E:\my work\Project

3)Image folder is at E:\my work\images

Path i am setting in "conf\server.xml" is

    <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true"
        xmlValidation="false" xmlNamespaceAware="false">
   <Context docBase="/my work/images/" path="/images"  />
   </Host>

But still when i tried to access the file using the following url

           http://localhost:8080/images/paper.jpg

I am unable to fetch it and getting the "HTTP Status 404" and request resource not found error.

Please help me in this i am using the Blob field to store the image and storing the image inside this folder when user request for a particuler image. I don't want to use specific servlet to write the image into the browser rather i want direct access to the user.

Please help me to solve this problem. Thanks Regard

Ankit Sahu
  • 85
  • 4
  • 17

3 Answers3

4

Add a <context> to the tomcat/conf/server.xml file.

<Context docBase="c:\images" path="/project/images" />

In this way, you should be able to find the files (e.g. c:/images/NameOfImage.jpg) under:

http://localhost:8080/project/images/NameOfImage.jpg

1

The solution to this is to write a Servlet which will read the files from the external folder and stream them to the client: essentially then it acts as a proxy between the client and the external file system. This would like something like the below and which you can call simply using:

<img src="/pathToMyImageServlet?imageId=123"/>

Servlet:

public class ImageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String imageId = request.getParameter("imageId");

        /*
        File file = new File("E:/my work/images/" + imageId);
        FileInputStream in = new FileInputStream(file);
        OutputStream out = response.getOutputStream();

        byte[] buf = new byte[1024];
        int count = 0;

        while ((count = in.read(buf)) >= 0) {
            out.write(buf, 0, count);
        }
        */

        byte[] imageData = ....// data from db for specified imageId
        OutputStream out = response.getOutputStream();
        out.write(imageData);
        out.flush();
        out.close();
        //in.close();

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • I don't want to use servlet because i am processing the image from the database while the user request for it and stored it inside the webcontent, which i want to take outside. If i will use another servlet to again process it and making it available for the browser, is it going to be a good approach with regard to the runtime efficiency. – Ankit Sahu Apr 23 '15 at 09:01
  • Can't you use this Servlet to stream directly from the database then? Why are you actually creating a image on disk? – Alan Hay Apr 23 '15 at 09:06
  • 1
    All you need to do is write the bytes from the database to the output stream out.write(bytesFromDatabase); See update. – Alan Hay Apr 23 '15 at 09:14
  • Yes, i got the basic idea, I am writing the code for it if i will need some help i will get in touch again. Thank for the response. – Ankit Sahu Apr 23 '15 at 09:17
0

As per my understanding your project should be inside the tomcat webapps folder & the images should be like

webapps/YourProject/resources/images/something.jpg

or

webapps/YourProject/WEB-Content/images/something.jpg

In my experience I don't think you have to set the path in any xml. Just directly access the image you will be able to access it. The container restricts access to anything which is placed inside WEB-INF folder.

underdog
  • 4,447
  • 9
  • 44
  • 89
  • no its in the hierarchy as i have mentioned above E:\my work\Project\WebContent – Ankit Sahu Apr 23 '15 at 05:54
  • That's what you need to change the hierarchy. Have you kept your web project in Tomcat's webapps folder? Are you using any IDE? you have provided the image url http://localhost:8080/images/paper.jpg after localhost:8080 your project name should be there. You have directly specified the image folder. That is not the standard way of accessing resources from your webapp. – underdog Apr 23 '15 at 05:55
  • Yes i am using Eclipse IDE. and i have also gone through the answer of [this question](http://stackoverflow.com/questions/1812244/simplest-way-to-serve-static-data-from-outside-the-application-server-in-a-java-w) and then i have implemented this. – Ankit Sahu Apr 23 '15 at 06:01
  • 1
    They cannot be stored in the webapps folder as these are images uploaded by the user images and will be lost on next deployment. – Alan Hay Apr 23 '15 at 08:42