首页 > 编程语言 > 详细

java下载附件(并防止中文乱码)

时间:2021-04-10 00:20:28      阅读:33      评论:0      收藏:0      [点我收藏+]
 // filePath是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
// 用available()方法来获取文件大小,进而用来定义缓冲数组的长度
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
// response.reset();
response.setCharacterEncoding(CharEncoding.UTF_8);
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"),"ISO-8859-1"));//防止中文乱码
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();

java下载附件(并防止中文乱码)

原文:https://www.cnblogs.com/wangSoft/p/14637108.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!