网站首页 > 技术文章 正文
一、创建下载工具类
@Slf4j
public class Down {
/**
*
* @param response
* @param downLoadPath 下载文件的路径
* @param realName 文件名字
*/
public static void downLoad(HttpServletResponse response,String downLoadPath,String realName) {
File file=new File(downLoadPath);
try {
common(response, realName, file);
} catch (IOException e) {
log.error("IO异常",e);
} finally {
file.delete();
}
}
private static void common(HttpServletResponse response, String realName, File file) throws IOException {
InputStream ins = new FileInputStream(file);
response.setCharacterEncoding("utf-8");
response.setHeader("pragma", "No-Cache");
response.setHeader("Cache-Control", "No-Cache");
response.setDateHeader("Expires", 0);
//multipart/form-data
response.setContentType("multipart/form-data;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+new String(realName.getBytes("GB2312"), "ISO8859-1"));
Cookie fileDownload=new Cookie("fileDownload", "true");
fileDownload.setPath("/");
response.addCookie(fileDownload);
OutputStream os=response.getOutputStream();
byte[] buff = new byte[2048];
int len;
while ((len=ins.read(buff))>0) {
os.write(buff, 0, len);
}
os.flush();
os.close();
ins.close();
}
}
猜你喜欢
- 2024-11-08 加班用了2天,结果同事30分钟就搞定了?你和别人的差距在哪里
- 2024-11-08 SMTP发送邮件 smtp发送邮件过程
- 2024-11-08 NPM 使用介绍 npm .staging
- 2024-11-08 java servlet笔记:设置编码集、文件下载和两种服务器跳转
- 2024-11-08 还不懂 HTTP 协议的吗?一篇文章讲透
- 2024-11-08 JavaScript包管理工具pnpm介绍 js importpackage
- 2024-11-08 Ajax请求时,请求类型,常用的几种 Content-Type json form-data xml
- 2024-11-08 Python教程:报表和日志精讲 python自动生成日报
- 2024-11-08 安全RCE之未授权访问分析 未授权的访问路径
- 2024-11-08 JMeter的这个功能,你了解么? jmeter详解
- 标签列表
-
- content-disposition (47)
- nth-child (56)
- math.pow (44)
- 原型和原型链 (63)
- canvas mdn (36)
- css @media (49)
- promise mdn (39)
- readasdataurl (52)
- if-modified-since (49)
- css ::after (50)
- border-image-slice (40)
- flex mdn (37)
- .join (41)
- function.apply (60)
- input type number (64)
- weakmap (62)
- js arguments (45)
- js delete方法 (61)
- blob type (44)
- math.max.apply (51)
- js (44)
- firefox 3 (47)
- cssbox-sizing (52)
- js删除 (49)
- js for continue (56)
- 最新留言
-