码迷,mamicode.com
首页 > 其他好文 > 详细

文件下载

时间:2019-09-30 11:17:15      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:path   har   class   cat   puts   replace   char   tac   response   

 文件路径:

技术图片

 

 


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 }

 

文件下载

标签:path   har   class   cat   puts   replace   char   tac   response   

原文地址:https://www.cnblogs.com/FengZeng666/p/11611564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!