Reputation: 85
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
Upvotes: 2
Views: 4934
Reputation: 56
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
Upvotes: 4
Reputation: 23246
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);
}
}
Upvotes: 1
Reputation: 4516
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.
Upvotes: 0