简体   繁体   中英

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.

These all work when I access the jsp file, I can see the image I have

<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. The browser won't let an HTML page do that: it would represent a security hole.

Image can't be access with real path of location by Server, because html page will be run User browser. Image should be access with server URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM