How to download files through tomcat download mapping

1.1 Find the server.xml file in the tomcat server

Insert image description here

!--doBase是静态资源路径位置,  path作用相当于设置的key,   doBase作用相当于value -->
<Context path="/download" docBase="E:\testBackData"></Context>

1.2 Find the web.xml file in the tomcat server

<servlet> 
	<init-param>
           	 	<param-name>listings</param-name>
           	 	<!--设置为true -->
            		<param-value>true</param-value><!--此处的false改为true-->
        	</init-param>
  </servlet>

2. Start tomcat to download the files in this folder – you need to bring the file name

For example, download the test document
Insert image description here

http://localhost:8082/download/测试文档.xlsx

For example, download sql file

http://localhost:8082/download/test1.sql

3. Configure cross-domain (optional operation)

Make some configurations in conf/web.xml: markdown

在 conf/web.xml 文件的最后面加上以下代码:app

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
  <init-param>
    <param-name>cors.allowed.origins</param-name>
    <param-value>*</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
复制代码
重启 tomcat 后生效。cors

注意: 这是一个统一的容许跨域设置,tomcat下的全部请求都将放开

4. Improvement steps

1. Add to server.xml in tomcat file
Insert image description here

<Context docBase="E:\testBackData" path="/testBackData" reloadable="false"/>	

2. Accessed address
http://localhost:8087/testBackData/
Insert image description here

5. The location of the file can also be placed in the webapp within the tomcat file.

For example, configure the offline version of element-plus

Guess you like

Origin blog.csdn.net/beiback/article/details/132473540