Tomcat Downloads Auto Rename file via file path URL

Source: Internet
Author: User
Tags md5

The latest in the writing of graduation design, involves the function of the name of the load, but there is no javaweb implementation of the program, after many searches, only found the Nginx scheme. I had to study and share this blog post. Add a file download feature for Tomcat

Create a new XML file in the Conf\catalina\localhost under the Tomcat directory, where I build the download.xml.
The contents are as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <context path= "  /download" docbase= "F://filetemp" Crosscontext= "true" >  

Where Docbase is the real directory of the files.
Path is the URL access path. provides automatic renaming for tomcat file downloads

Because we usually store files in the server is stored with MD5 or other policies, on the one hand, to prevent duplicate names, on the other hand, the network disk and other file servers require a second pass function, usually using the MD5 named file. When the user accesses the download, the filename is MD5, so it needs to be renamed. Here I am using the Filter+http header implementation of the rename. How to implement a rename
A content-disposition header exists in the Http header. This header enables you to specify the file name when downloading files. How to add this header message to the response header for a file download
Because our file downloads are implemented by Tomcat, you can use the filter to intercept the request and add a header to the Dofilter method. How to achieve

Write a global filter. Make a jar package, throw it into the Tomcat directory, and configure filter in the Web.xml in the Conf folder in the Tomcat directory. Nonsense not to say, directly on the code:

Package com.lzp.filter;

Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse; 

    public class Filedownloadrenamefilter implements filter {/** * filter processing rule */String urlstartswith = ""; Public Filedownloadrenamefilter () {} public void Destroy () {} public void Dofilter (servletreques T request, servletresponse response, Filterchain chain) throws IOException, Servletexception {if ((H ttpservletrequest) request). Getrequesturi (). StartsWith (Urlstartswith)) {String filename = Request.getparamete
            R ("filename"); if (filename!= null && filename.length ()!= 0) {((HttpServletResponse) response). SetHeader ("Cont Ent-disposition "," attachment;filename=\ "" + filename + "" "");

    } chain.dofilter (request, response); } public void init (Filterconfig fconfig) throws servletexception {This.urlstartswith = Fconfig.getinitparame
    ter ("Urlstartswith"); }

Add to the Conf/web.xml <web-app> node:

        <filter-class> com.lzp.filter.filedownloadrenamefilter</filter-class>
            <param-name> urlstartswith</param-name>
        <filter-name>filedownloadrenamefilter</ filter-name>

Where the value of the Param-value is the path set in the Download.xml (remember the previous one/)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.