JavaWeb----文件的下载
1.问题说明:
实现文件下载,且下载的文件名不会出现乱码现象(主流浏览器:Google、IE、火狐、360)
2.页面预览
代码布局
3.代码实现
3.1 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>下载文件资源</title>
</head>
<body>
<h1>文件资源的下载</h1>
<a href="./img/2.jpg">图片预览</a>
<hr>
<a href="./downloadServlet?filename=长春工业大学校徽.jpg">图片下载链接</a> |
<a href="./downloadServlet?filename=长春工业大学宣传片.avi">视频下载链接</a>
</body>
</html>
3.2 downLinkServlet.java
package com.ccut.servlet;
import com.ccut.util.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;
/**
* @ClssName:downLinkServlet
* @Author:Mr.Tony
* @Description:
* @Create: 2020-05-12 16:18
* @Version: v1.0
*/
@WebServlet("/downloadServlet")
public class downLinkServlet extends HttpServlet {
@Override
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.设置response的响应头
//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.使用工具类方法编码文件名即可
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();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
3.3-1 DownLoadUtils.java(我用jdk 11,jdk9以上含jdk9)
package com.ccut.util;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.Base64.Encoder;
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")) {
// 火狐浏览器
Encoder encoder = Base64.getEncoder();
filename = "=?utf-8?B?" + encoder.encodeToString(filename.getBytes("utf-8")) + "?=";
} else {
// 其它浏览器
//filename = URLEncoder.encode(filename, "utf-8");
//苹果Safari浏览器也支持
filename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
}
return filename;
}
}
3.3-2 DownLoadUtils.java(jdk 8以下包含jdk8)
package com.ccut.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");
//苹果Safari浏览器也支持
filename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
}
return filename;
}
}
说明:
原因是/lib/tool.jar和/lib/rt.jar已经从Java SE 9中删除
测试火狐浏览器
测试Google浏览器
MD5的加密现方法(jdk9.0 含9.0)
import java.util.Base64.Encoder
import java.util.Base64.Decoder
Encoder encoder = Base64.getEncoder();
String result = encoder.encodeToString(byteArray);
Decoder decoder = Base64.getDecoder();
byte[] result = decoder.decode(str);
原方法(jdk8.0 含8.0)
BASE64Encoder encoder = new BASE64Encoder();
String imagestr = encoder.encode(captcha);
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = decoder.decodeBuffer(imagestr);
不停的思考,就会不停的进步