Two ways to set Tomcat virtual path using IDEA

Foreword:

In the project development process, there will be image upload and echo functions. If the path of the image upload exists in the path of the project service (for example: under the WEB-INF or webapp folder), the attachment will be compiled and released when the project is released In the jar package or war package, the published file is too large. Therefore, the file attachment will be placed on the designated directory or server, but sometimes after uploading to the designated directory, it cannot be accessed through the service request normally, and the common problem of 404 path not found during access will occur.
The following content introduces two solutions for tomcat virtual path setting.

Method 1: Use Tomcat's own virtual path

Modify the content of server.xml in the conf folder under the Tomcat installation directory of the service deployment
Insert picture description here
. Add the following content to the <Host> note in the server.xml file:

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
            
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

		<!--
			下方的Context内容为新增内容:
			path="/images"  虚拟路径
			docBase="D:\images"  图片存放的真实路径
		 -->
		  <Context path="/images" docBase="D:\images" debug="0" reloadable="true" crossContext="true" />

</Host>

The effect of server.xml is as follows:
Insert picture description here
When Tomcat starts the project on IDEA, check "Deploy applications configured in Tomcat instance" to restart the Tomcat startup project. As shown below:
Insert picture description here

Method 2: Do not change the Tomcat configuration, directly use IDEA to set the virtual path

When Tomcat starts the project on IDEA, do not check "Deploy applications configured in Tomcat instance", as shown in the following figure:
Insert picture description here
Open the "Deployment" in the Tomcat configuration in IDEA to set the virtual path, the path is as follows:
Insert picture description here
Insert picture description here
Insert picture description here
follow the above steps to restart after setting Tomcat is fine.

Either one of the two methods can achieve the function. The following is the effect diagram of the function I tested:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_33369215/article/details/109138165