Servlet的文件下载案例
程序员文章站
2022-06-02 13:54:37
...
案例需求分析与步骤
下载页面的代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="/08_response/downloadServlet?filename=素描1.jpg">素描风景</a>
<a href="/08_response/downloadServlet?filename=夏天的风.mp3">夏天的风</a>
</body>
</html>
文件下载的Servlet代码部分
package pers.cb.web.download;
import pers.cb.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 = null;
if (filename.endsWith(".jpg"))
{
realPath = servletContext.getRealPath("/img/" + filename);
}
if (filename.endsWith(".mp3"))
{
realPath = servletContext.getRealPath("/music/" + filename);
}
System.out.println(realPath);
//2.2用字节流关联
FileInputStream fis = new FileInputStream(realPath);
//3.设置response的响应头
//3.1设置响应头类型:content-type
String mimeType = servletContext.getMimeType(filename);//获取文件的mime类型
response.setHeader("content-type",mimeType);
//response.setContentType("text/html;charset=utf-8");
//解决中文文件名问题
//1.获取user-agent请求头
String agent = request.getHeader("user-agent");
//2.使用工具类方法编码文件名即可
filename = DownLoadUtils.getFileName(agent, filename);
//3.2设置响应头打开方式
response.setHeader("content-disposition","attachment;filename=" + filename);
//4.将输入流的数据写出到输出流中
ServletOutputStream sos = response.getOutputStream();
byte[] bytes = new byte[1024 * 8];
int len = 0;
while((len = fis.read(bytes)) != -1)
{
sos.write(bytes,0,len);
}
fis.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
中文文件名的工具类
package pers.cb.web.utils;
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;
}
}
推荐阅读
-
javascript - 像sourceforge这种无刷新的文件下载是怎么做到的?
-
php获取CSS文件中图片地址并下载到本地的方法,
-
windows无法访问指定设备路径或文件 下载文件的点击数回填
-
Spring MVC的文件上传和下载以及拦截器的使用实例
-
基于java文件上传-原始的Servlet方式
-
iOS开发网络篇—实现大文件的多线程断点下载
-
J2EE Servlet上传文件到服务器并相应显示功能的实现代码
-
Java Servlet简单实例分享(文件上传下载demo)
-
【Python】Python的urllib模块、urllib2模块批量进行网页下载文件
-
Android通过SOCKET下载文件的方法