文件下载案例
1. 要求
- 页面显示超链接
- 点击超链接后弹出下载提示框
- 完成图片/视频文件下载
2. 效果
3. 分析
1. 超链接指向的资源如果能够被浏览器解析,则在浏览器中展示,如果不能解析,则弹出下载提示框。不满足需求因为任何资源都必须弹出下载提示框
2. 使用响应头设置资源的打开方式:
* content-disposition:attachment;filename=xxx
3. 文件中文名乱码
3. 步骤
1. 定义页面,编辑超链接href属性,指向Servlet,传递资源名称filename
2. 定义Servlet
1. 获取文件名称
2. 使用字节输入流加载文件进内存
3. 指定response的响应头: content-disposition:attachment;filename=xxx
4. 设置传输的数据是什么类型
5. 将数据写出到response输出流
4. 代码
(1)downLoad.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="/day12/downLoadServlet?fileName=2.jpg">下载图片</a>
<br />
<a href="/day12/downLoadServlet?fileName=1.avi">下载视频</a>
</body>
</html>
(2)DownLoadServlet.java文件
@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 = request.getServletContext();
String path = servletContext.getRealPath("/resource/" + fileName);
//2.2 加载资源
FileInputStream inputStream = new FileInputStream(path);
//3.设置响应头-以附件形式发送文件
response.setHeader("content-disposition", "attachment;filename="+fileName);
//4.设置传输的数据是什么类型
String mimeType = servletContext.getMimeType(fileName);
response.setContentType(mimeType);
//5.数据传输
ServletOutputStream outputStream = response.getOutputStream();
byte[] buff = new byte[1024 * 8]; //字节缓冲数组
int len = 0;
while ((len = inputStream.read(buff)) != -1) {
outputStream.write(buff,0,len);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
5. 文件名为中文时乱码的问题
解决方法:
建立下面文件
package cn.wanghao.web.util;
import sun.misc.BASE64Encoder;
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")) {
// 火狐浏览器
BASE64Encoder base64Encoder = new BASE64Encoder();
filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
} else {
// 其它浏览器
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
}
在以附件形式发送消息之前,用DownLoadUtils.getFileName()将文件名转换一下。
代码如下:
@WebServlet("/downLoadServlet")
public class DownLoadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取参数---文件名称
String fileName = request.getParameter("fileName");
//request.setCharacterEncoding()方法对get请求不起作用
//将乱码转回用ISO8859-1编码前的二进制位
byte[] bytes = fileName.getBytes("ISO8859-1");
//再使用UTF-8进行编码
fileName = new String(bytes,"UTF-8");
//2.将资源加载进内存
//2.1 获取资源的真实路径
ServletContext servletContext = request.getServletContext();
String path = servletContext.getRealPath("/resource/" + fileName);
//2.2 加载资源
FileInputStream inputStream = new FileInputStream(path);
//3.设置传输的数据是什么类型
String mimeType = servletContext.getMimeType(fileName);
response.setContentType(mimeType);
//4.设置响应头-以附件形式发送文件
String ua = request.getHeader("user-agent"); //获取浏览器的用户代理
System.out.println(ua);
fileName = DownLoadUtils.getFileName(ua, fileName);
response.setHeader("content-disposition", "attachment;fileName="+fileName);
//5.数据传输
ServletOutputStream outputStream = response.getOutputStream();
byte[] buff = new byte[1024 * 8]; //字节缓冲数组
int len = 0;
while ((len = inputStream.read(buff)) != -1) {
outputStream.write(buff,0,len);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}