简体   繁体   English

为什么我不能使用realpath访问JSP文件中的图像?

[英]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.

相关问题 在jsp中上传文件后,为什么无法从请求中访问参数? - Why I can't get access to parameters from a request after upload a file in jsp? 为什么我不能访问jsp中的字段值(它返回null)? - Why can't I access the value of the field in jsp (it returns null)? 我遇到错误。(图像文件的真实路径) - I'm getting an error.( Realpath to Image file) 即使使用请求调度程序,我也无法访问 WEB-INF/file.jsp 我收到错误 404 - i can't access to WEB-INF/file.jsp even with using request dispatcher i got error 404 为什么我不能使用<jsp:getproperty>没有<jsp:usebean> ?</jsp:usebean></jsp:getproperty> - Why I can't use <jsp:getProperty> without <jsp:useBean>? 我无法在JSP中导入CSS文件 - I can't import a CSS File in a JSP 我无法使用Spring MVC和JSP访问Webapp中的js和CSS资源 - i can't access js and css ressources in my webapp using spring mvc and jsp 无法从控制器中的src/main/webapp/WEB-INF/resources/img访问图像文件以在jsp中显示 - Can't access image file from src/main/webapp/WEB-INF/resources/img in controller to display in jsp 在JSP中无法上传图像文件的完整路径 - Can't take uploaded image file full path in JSP 为什么我无法使用反射访问字段类? - Why I can't access to field class using reflection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM