Github:file_download
一. 需求
- 页面显示超链接
- 点击超链接后弹出下载提示框(Chrome 浏览器是直接下载)
- 完成图片文件下载
二. 分析
- 点击超链接,超链接指向的资源如果能够被浏览器解析,则直接在浏览器中展示;如果不能,则弹出下载提示框。–> 不满足需求,需求为任何资源都要弹出下载提示框。
- 设置响应头改变资源的的打开方式:
Content-dispostion:attachment;filename=xxx
,以附件形式打开。
三. 开发步骤
-
定义 download.html 页面,编辑超链接的 href 属性指向 Servlet,并传递资源名称 filename 作为参数。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>download</title> </head> <body> <a href="/file_download/img/九尾.jpg">图片1</a> <a href="/file_download/img/2.jpg">图片2</a> <hr /> <a href="/file_download/downloadServlet?filename=九尾.jpg">图片1</a> <a href="/file_download/downloadServlet?filename=2.jpg">图片2</a> </body> </html>
-
定义 Servlet
- 获取资源名称
- 使用字节输入流加载文件进内存
- 找到文件的真实(服务器)路径
- 用字节流关联文件
- 设置响应头:
setHeader("Content-dispostion", "attachment;filename=xxx")
- 将数据写出到 response 输出流
-
解决中文文件名问题:导入 DownLoadUtils 工具类
- 获取客户端使用的浏览器版本信息
- 根据不同的版本信息,响应不同的数据(设置 filename 的编码方式)。
DownloadServlet.java
package com.hjplz.web.download;
import com.hjplz.web.utils.DownLoadUtils;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
@WebServlet("/downloadServlet")
public class DownloadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 获取请求参数(文件名称)
String filename = request.getParameter("filename");
// 2. 使用字节输入流加载文件进内存
// 2.1 找到文件的真实(服务器)路径
ServletContext servletContext = this.getServletContext();
String realPath = servletContext.getRealPath("/img/" + filename);
// 2.2 用字节流关联文件
FileInputStream fis = new FileInputStream(realPath);
// 3. 设置响应头
// 3.1 设置响应头类型:Content-Type
String mimeType = servletContext.getMimeType(filename); // 获取文件的 MIME 类型
response.setHeader("Content-Type", mimeType);
// 3.2 设置响应头打开方式:Content-disposition
// 解决中文文件名问题
// 1. 获取 User-Agent 请求头
String agent = request.getHeader("User-Agent");
// 2. 使用 DownLoadUtils 工具类方法编码文件名
filename = DownLoadUtils.getFileName(agent, filename);
response.setHeader("Content-disposition", "attachment;filename=" + filename);
// 4. 将输入流的数据写出到输出流中
ServletOutputStream sos = response.getOutputStream();
byte[] buff = new byte[1024 * 8];
int len = 0;
while((len = fis.read(buff)) != -1) {
sos.write(buff, 0, len);
}
fis.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
DownLoadUtils.java
package com.hjplz.web.utils;
import java.util.Base64;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class DownLoadUtils {
public static String getFileName(String agent, String filename) throws UnsupportedEncodingException {
if (agent.contains("MSIE")) {
// IE浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
// 火狐浏览器
Base64.Encoder encoder = Base64.getEncoder();
filename = "=?utf-8?B?" + encoder.encodeToString(filename.getBytes("utf-8")) + "?=";
} else {
// 其它浏览器
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
}
四. 项目结构
五. 效果演示
在 Firefox 浏览器中访问: