[英]Why can't I access the image in JSP file using realpath?
I have a sample index.jsp file and in the same directory I also have 1.png. 我有一个示例index.jsp文件,并且在同一目录中也有1.png。
These all work when I access the jsp file, I can see the image I have 当我访问jsp文件时,所有这些都能正常工作,我可以看到我拥有的图像
<img src="1.png"/>
<img src="<%= request.getContextPath()%>/1.png"/>
But this does not work fine: 但这不能正常工作:
<img src="<%= request.getServletContext().getRealPath(File.separator)%>1.png"/>
When I inspect the source I see: 当我检查源时,我看到:
<img src="C:\tomcat\webapps\multi-web\1.png"/>
and this is where the image really is. 这就是图像的真实位置。 But in the browser I can not see the image.
但是在浏览器中我看不到图像。 Why is that?
这是为什么?
Because that would open a file located on the end-user machine, and not on the web server. 因为那样会打开位于最终用户计算机上而不是Web服务器上的文件。 The browser won't let an HTML page do that: it would represent a security hole.
浏览器不会允许HTML页面执行此操作:它将代表一个安全漏洞。
Image can't be access with real path of location by Server, because html page will be run User browser. 服务器无法使用实际位置路径访问图像,因为html页面将运行用户浏览器。 Image should be access with server URL.
图片应使用服务器URL访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.