How does the jsp page display the picture of the local disk path?

I encountered this problem when I was working on a project today, and it took me a long time to solve it.

Sometimes if the project involves a large number of pictures and videos, we store the database to the local disk path, or the server disk path.

This problem may be encountered. In the project, the resources that can be directly accessed can only be in this project. When you want to access the disk file, you will not be able to access it, which does not mean that there is no way. You can do it in the following way.

That is to configure the virtual path:

That is to say, map a directory on the hard disk to the working directory of tomcat, and then tomcat can find the files in the hard disk according to a mapping relationship.

1. We find the server in Eclipse, there is a server.xml file below, click to open



2. Add the following code to the <Host> tag:

<Context crossContext="true" debug="0" docBase="D:\imooc\images" path="/imgUrl" reloadable="true"/> 

Note: This sentence must be added in the <Host> tag, otherwise it will be useless


3. Description

Where docBase is the absolute path of the local computer

path is the virtual path set

After the modification, you need to restart the tomcat server


4. The virtual path configuration is completed, and the next step is the display of the jsp page.

<img src="/imgUrl/header1.png" alt="用户头像" width="40" height="40">


At this time, the path in the img tag src " /imgUrl/header1.png "------- will be mapped to "D:\imooc\images\ header1.png "


5. We can test access directly on the page

http://localhost:8080/imgUrl/header1.png

As shown in the figure below, you can directly access the


Guess you like

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