Springboot 添加DefaultServlet实现更好的文件下载功能
defaultServlet是tomcat为我们提供的能下载或打开tomcat项目文件夹下的静态文件的servlet,他的url-pattern是 /。
但如果使用springboot,会自动注册dispatchServlet到容器中url-pattern也是 /会把defaultServlet给覆盖掉。
实现目的
- 实现文件下载和文件打开功能
- 文件存储目录在
D:/files/文件夹下
- 如果访问
localhost/download/xxx.jpg则到该d:/files/下查找xxx.jpg下载
- 如果访问
localhost/download/aa/bb/cc/xxx.jpg 则到d:/files/aa/bb/cc/下查找
- 如果访问
localhost/static/xxx.jpg 则在d:/files/xxx.jpg在浏览器打开而不是下载
过程
我们继承一个DefaultServlet并在init方法里面将文件存储的目录配置进去。然后将这个Servlet添加到Tomcat里。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| @Configuration public class ServletConfig {
@Bean public ServletRegistrationBean servletRegistrationBean() { ServletRegistrationBean<Servlet> register = new ServletRegistrationBean<>(); DefaultServlet defaultServlet = new DownloadServlet(); register.setServlet(defaultServlet); register.addUrlMappings("/static/*", "/download/*"); register.setLoadOnStartup(1); return register; }
private static class DownloadServlet extends DefaultServlet {
private static final long serialVersionUID = 1537326382082402617L;
@Override public void init() throws ServletException { WebResourceRoot attribute = (WebResourceRoot) getServletContext().getAttribute(Globals.RESOURCES_ATTR);
attribute.addPreResources(new DirResourceSet(attribute, "/static", "d:/files", "/")); attribute.addPreResources(new DirResourceSet(attribute, "/download", "d:/files", "/")); super.init(); }
@Override public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (req.getRequestURI().startsWith("/download")) { resp.setContentType("application/octet-stream"); } super.service(req, resp); } }
}
|
这里重写service方法的目的是 如果发现/download开头的请求,要设置响应头为application/octet-stream,这样浏览器就能下载该文件,而不是展示该文件。
结尾
以上就是在SpringMvc环境下,处理静态资源展示和下载的实现方式。