Solve the problem that the picture under the absolute path of the page does not display

Problem Description: 

I save the picture to the local disk, and then the absolute path of the picture is saved in the database.
However, the absolute path of the picture is displayed in the jsp page, but not the picture. The specific code is as follows:
<img src="E:\Cache\Img_Cache\20171213091911.jpg"/>

Cause Analysis :

It turns out that tomcat looks for pictures in the root directory of the project by default.
<img src="http://localhost:8080/znkj/images/20171213091911.jpg"/>
In fact, the picture is not saved in this location.

Solution:

        

Configure the virtual path:
Open conf/server.xml in the installation path of tomcat,
Add the following code in the middle of <Host></Host>:
<Context path="/Cache/Img_Cache/" docBase="E:\Cache\Img_Cache" reloadable="true">    </Context>

        
The front-end jsp code is as follows:
<img src=" /Cache/Img_Cache/ 20171213091911.jsp">
At this time, start tomcat, and the picture can be displayed normally.

Notice: 
The virtual path /Cache/Img_Cache should be consistent with the path configured above.
     一般来讲,图片上传不会和项目分开,将项目保存到本地磁盘的做法不建议使用,如果项目上线发布到服务器上是不存在分盘的,所以路径问题最后还是要修改。所以以上将图片保存在本地磁盘的做法不建议使用,最好还是保存在项目根目录下,不过这样做也有一点弊端,随着用户上传的文件的增多,项目所占的内存会一直增加。



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326437474&siteId=291194637