About tomcat file download configuration, tomcat file download configuration
Preface
- Tomcat File Download
- Disable tomcat directory list browsing
- Tomcat cannot download attachments with Chinese names
File downloading is a common feature in Java Web projects. Recently, we found that the file downloading feature can be completed through the built-in features of tomcat.
Tomcat File Download
- In eclipseWebContentCreate a folder named download_file. This folder is used to download files and then start the tomcat server,
In the browser, the URL: http: // localhost: 8080/project name/download_file/file name can be used to download the file. (Note that the file name must contain a suffix.) 1
Figure 1
Disable tomcat directory list browsing
- In fact, the above has completed a simple file download function, but there is a problem, if the url is followed by download_file without any file name, tomcat will traverse the download_file file directory by default (2 ),
Figure 2
In this way, it is not safe enough for others to download other files at will. Tomcat configuration is involved here. Open/conf/web. xml in the tomcat directory.
<servlet> <servlet-name>default</servlet-name> <servlet-class> org.apache.catalina.servlets.DefaultServlet </servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
Change the value of listings to false to disable tomcat from directly accessing the file directory.
Tomcat cannot download attachments with Chinese names
- There is still a flaw in downloading the above file. When the file name contains Chinese characters, the file in the directory cannot be found. This is the/conf/server under the tomcat directory. add the above Code to the xml file and add it to the <Connector> \ tag.DisableUploadTimeout = "true" URIEncoding = "UTF-8"You can recognize Chinese file names.
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" disableUploadTimeout="true" URIEncoding="UTF-8"/>
Finally, if you modify the configuration file and restart the eclipse tomcat service, you only need to delete the tomcat service and create a new one.