在我的网络应用程序中,我的一个页面正在将照片上传到路径
/usr/local/rac/picture-name-goes-here
The photo is uploading fine, but I need to access it in another page and when I try to access it from my JSP, it will not show up, I am guessing my path to the photo is incorrect
The code in my JSP to access the photo looks like the following.
<tr>
<td>
<img src="/usr/local/agent/photo-name-here.jpg"/>
</td>
</tr>
我的照片路径有误吗? 如果有帮助,我正在从目录中的 Tomcat 运行我的 Web 应用程序
C:\Tomcat6
我最终会将其转移到 Linux 机器上,并希望共享相同的照片路径。
最佳答案
这里有一个重大误解。 HTML 由网络浏览器执行,而不是由网络服务器执行。网络浏览器下载 HTML,扫描任何需要下载的资源(CSS、脚本、图像等),并为每个资源触发一个新的 HTTP 请求。所有资源都应该指向有效的 URL,而不是指向客户端计算机不知道的某些本地磁盘文件系统路径。
基本上有两种方法可以解决这个“问题”:
添加新的
Context
到 Tomcat 的/conf/server.xml
:<Context docBase="/usr/local/agent" path="/images" />
这样就可以通过 http://example.com/images/ 访问它们...您将能够使用以下
<img>
<img src="/images/photo-name-here.jpg"/>
创建
Servlet
基本上得到InputStream
图像并将其写入OutputStream
沿着一组正确的 header 的响应。您可以找到here此类 servlet 的基本示例和 here一个更高级的例子。当Servlet
映射到/images/*
在web.xml
,图像可通过 http://example.com/contextname/images/ 访问...您将能够按如下方式使用它(假设 JSP/HTML 文件位于上下文根中):<img src="images/photo-name-here.jpg"/>
关于java - 我需要查看上传的图片的正确路径是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3723718/