Loading

response - 文件下载

## 案例:
    * 文件下载需求:
        1. 页面显示超链接
        2. 点击超链接后弹出下载提示框
        3. 完成图片文件下载


    * 分析:
        1. 超链接指向的资源如果能够被浏览器解析,则默认会在浏览器中直接展示,不会弹出下载框。如果浏览器不能解析,才会弹出下载提示框。这样不满足我们的需求
        2. 需求:任何资源都必须弹出下载提示框
        3. 解决方法:使用响应头设置资源的打开方式:
            * content-disposition:attachment;filename=xxx


    * 步骤:
        1. 定义页面,编辑超链接href属性,指向Servlet,传递资源名称filename
        2. 定义Servlet
            1. 获取文件名称
            2. 使用字节输入流加载文件进内存
            3. 指定response的响应头: content-disposition:attachment;filename=xxx
            4. 将数据写出到response输出流


    * 问题:
        * 中文文件名问题
            * 解决思路:
                1. 获取客户端使用的浏览器版本信息
                2. 根据不同的版本信息,设置filename的编码方式不同
 

 

文件路径:

 

 


download.html
 1 <!DOCTYPE html>
 2 <html lang="en">
 3     <head>
 4         <meta charset="UTF-8">
 5         <title>Title</title>
 6     </head>
 7     <body>
 8         <a href="/day13_tomcat/downloadServlet?filename=皮卡丘.jpg">图片</a>
 9         <a href="/day13_tomcat/downloadServlet?filename=duanwu.mp3">音乐</a>
10         <a href="/day13_tomcat/downloadServlet?filename=huli.avi">视频</a>
11     </body>
12 </html>




DownloadServlet类
 1 package cn.itcast.web.servlet;
 2 
 3 import cn.itcast.utils.DownloadUtils;
 4 
 5 import javax.servlet.ServletContext;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.ServletOutputStream;
 8 import javax.servlet.annotation.WebServlet;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 
15 @WebServlet(urlPatterns = "/downloadServlet")
16 public class DownloadServlet extends HttpServlet {
17     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
18         // 1.获取请求参数,文件名称
19         String filename = request.getParameter("filename");
20         // 2.使用字节输入流加载文件进内存
21         // 2.1找到文件服务器路径
22         ServletContext context = request.getServletContext();
23         String realPath = context.getRealPath("/img/" + filename);
24         // 2.2用字节流关联
25         FileInputStream fis = new FileInputStream(realPath);
26 
27         // 3.设置response的响应头
28         // 3.1设置响应头类型:content-type
29         String mimeType = context.getMimeType(filename);
30         response.setHeader("content-type", mimeType);
31 
32         // 解决中文文件名问题
33         // 获取user-agent请求头
34         String agent = request.getHeader("user-agent");
35         // 使用DownloadUtils工具类方法编码文件名
36         filename = DownloadUtils.getFileName(agent, filename);
37 
38         // 3.2设置响应头打开方式:content-disposition
39         response.setHeader("content-disposition", "attachment; filename="+filename);
40 
41         // 4.将输入流的数据写到输出流中
42         ServletOutputStream sos = response.getOutputStream();
43         byte[] b = new byte[1024 * 8];
44         int len = 0;
45         while((len = fis.read(b)) != -1){
46             sos.write(b, 0, len);
47         }
48     }
49 
50     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
51         this.doPost(request, response);
52     }
53 }

 

DownloadUtils工具类

 1 package cn.itcast.utils;
 2 
 3 import sun.misc.BASE64Encoder;
 4 import java.io.UnsupportedEncodingException;
 5 import java.net.URLEncoder;
 6 
 7 
 8 public class DownloadUtils {
 9 
10     public static String getFileName(String agent, String filename) throws UnsupportedEncodingException {
11         if (agent.contains("MSIE")) {
12             // IE浏览器
13             filename = URLEncoder.encode(filename, "utf-8");
14             filename = filename.replace("+", " ");
15         } else if (agent.contains("Firefox")) {
16             // 火狐浏览器
17             BASE64Encoder base64Encoder = new BASE64Encoder();
18             filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
19         } else {
20             // 其它浏览器
21             filename = URLEncoder.encode(filename, "utf-8");
22         }
23         return filename;
24     }
25 }

 

posted @ 2019-09-30 10:59  拾月凄辰  阅读(566)  评论(0编辑  收藏  举报