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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
2018-09-30 层序创建二叉树
2018-09-30 求二叉树的高度
2018-09-30 输出二叉树中的所有叶子节点
2018-09-30 Brute-Force算法
2018-09-30 循环队列(数组实现)
2018-09-30 中缀表达式转换成后缀表达式(只适用于加减乘除运算)
2018-09-30 顺序栈