I have made a folder outside the webcontent to save the uploaded images by user. Initially i tried to access those files directly 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 feild 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
答案 0 :(得分:3)
将<context>
添加到tomcat/conf/server.xml
文件。
<Context docBase="c:\images" path="/project/images" />
通过这种方式,您应该能够在以下位置找到文件(例如c:/images/NameOfImage.jpg):
答案 1 :(得分:1)
解决方法是编写一个Servlet,它将从外部文件夹中读取文件并将它们传输到客户端:基本上它就是客户端和外部文件系统之间的代理。这可能是类似下面的内容,你可以简单地使用:
<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);
}
}
答案 2 :(得分:0)
根据我的理解,你的项目应该在tomcat webapps文件夹中。图像应该像
webapps/YourProject/resources/images/something.jpg
或
webapps/YourProject/WEB-Content/images/something.jpg
根据我的经验,我认为你不必在任何xml中设置路径。只需直接访问您将能够访问它的图像。容器限制访问放在WEB-INF文件夹中的任何内容。