Spring boot 文件下载乱码问题
Spring Boot 文件下载:乱码问题
第一次使用Spring boot 时下载文件保存时,英文名称是没有问题的,但是,有中英混血的话就会出现乱码问题
在本人电脑D盘目录下有一个PDF文件:Java调用POI组件操作Excel.pdf
控制器代码:
@RequestMapping("/download")public String downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException{String fileName = "Java调用POI组件操作Excel.pdf";if (fileName != null) {File file = new File("D:\\"+fileName);if (file.exists()) {response.setContentType("application/force-download");response.addHeader("Content-Disposition","attachment;fileName=" +fileName);byte[] buffer = new byte[1024];FileInputStream fis = null;BufferedInputStream bis = null;try {fis = new FileInputStream(file);bis = new BufferedInputStream(fis);OutputStream os = response.getOutputStream();int i = bis.read(buffer);while (i != -1) {os.write(buffer, 0, i);i = bis.read(buffer);}System.out.println("下载成功...");} catch (Exception e) {e.printStackTrace();} finally {if (bis != null) bis.close();if (fis != null) fis.close();}}}return null;}
Java调用POI组件操作Excel.pdf";if (fileName != null) {File file = new File("D:\\"+fileName);if (file.exists()) {response.setContentType("application/force-download");response.addHeader("Content-Disposition","attachment;fileName=" +fileName);byte[] buffer = new byte[1024];FileInputStream fis = null;BufferedInputStream bis = null;try {fis = new FileInputStream(file);bis = new BufferedInputStream(fis);OutputStream os = response.getOutputStream();int i = bis.read(buffer);while (i != -1) {os.write(buffer, 0, i);i = bis.read(buffer);}System.out.println("下载成功...");} catch (Exception e) {e.printStackTrace();} finally {if (bis != null) bis.close();if (fis != null) fis.close();}}}return null;}
HTML代码:
<!DOCTYPE html>
<html lang="en">
<head><title>文件下载</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<a href="/download">下载</a>
</body>
</html>
咿呀呀,可以下载但是:
百度了半天,折腾死自己:
这样改点击下载没反应:
//初始代码:String fileName = "Java调用POI组件操作Excel.pdf";
//修改后代码:问题没解决
String fileName = new String("Java调用POI组件操作Excel.pdf".getBytes("UTF-8"),"iso-8859-1");
Java调用POI组件操作Excel.pdf";
//修改后代码:问题没解决
String fileName = new String("Java调用POI组件操作Excel.pdf".getBytes("UTF-8"),"iso-8859-1");
关键点在这里:
//初始代码:response.addHeader("Content-Disposition","attachment;fileName=" +fileName);
//修改后代码:问题解决
response.addHeader("Content-Disposition","attachment;fileName=" +new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
response.addHeader("Content-Disposition","attachment;fileName=" +new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
看图片:
Spring boot 文件下载乱码问题相关推荐
- Spring Boot文件下载断点续传
Spring Boot文件下载断点续传 正文 MimeType映射关系 正文 @RequestMapping(value = { "/downloading" }, method ...
- spring boot 文件下载接口
文章目录 spring boot 文件下载接口 通过ResponseEntity实现 通过写HttpServletResponse的OutputStream实现 spring boot 文件下载接口 ...
- spring boot html乱码,Spring-boot 字符集设置 解决乱码方案
使用spring-boot开发时候,有时候程序没事,往往不经意会造成中文到前端变成乱码(????这样情况) 下面给出spring-boot项目统一字符集设置方案: 1.Spring Boot修改编码方 ...
- Spring Boot 中文乱码问题解决方案汇总
使用 Spring Boot 开发,对外开发接口供调用,传入参数中有中文,出现中文乱码,查了好多资料,总结解决方法如下: 第一步,约定传参编码格式 不管是使用httpclient,还是okhttp,都 ...
- spring boot 日志乱码_3. Spring Boot日志
3.1 日志框架分类和选择 市面上的日志框架 JCL.SLF4J,Jboss-logging.logback.log4j.log4j2.JUL .... 左边选一个门面(抽象层),右边来选一个实现 ? ...
- Spring Boot中文乱码问题怎么解决(按步骤做)
在我们切换浏览器的中英文的时候,会出现中文乱码的问题,这样的话我们要进行以下操作 1.点击File,点击Settings- 2.在输入框中输入File Encodings,找到File Encodin ...
- Spring Boot 中文乱码问题解决方法
1.在运行项目成功到网页上面验证,出现的是乱码 2.在idea中file找到setting --> Editor --> Code Style --> File Encodings ...
- 【Spring】Spring boot 中文乱码
1.背景 写了一个Sparing程序,传入的是一个字符串字符串是一个Json {"id":1,"name":"梁川川lcc_1qazxsw2#$%&q ...
- spring boot文件下载加水印(pdf,word,pdf,照片,excel)
<repositories><repository><id>com.e-iceblue</id><url>https://repo.e-ic ...
最新文章
- Rancher 2.2.2 发布,优化 Kubernetes 集群运维
- 从给定的N个正数中选取若干个数之和最接近M
- boost::range模块reversed相关的测试程序
- lvs-dr模式原理详解和可能存在的“假负载均衡”
- oracle与db2 市场占有率,oracle 与 DB2 的区别
- Android TextView跑马灯效果与设置文字阴影
- openssl学习笔记--CA及https网站证书配置
- iOS:ODRefreshControl
- 4.TCP/IP 详解卷1 --- ARP:地址解析协议
- LeetCode Student Attendance Record I
- php 61850,IEC61850客户端
- PR转场 700+抖音视频转场素材包含PR调色预设和音效素材
- APP安全——抓包代理工具的设置
- 【PTA】中M22春C、Java入门练习7-138 质因子分解
- ugp和千幻魔镜买哪个好_2020年买VR盒子十大品牌推荐 VR手机盒子如何使用
- 负载均衡器之F5和Nginx
- python模拟实现QQ邮箱登录
- win10投影无法正常使用:我们正在确认此功能 解决方法
- certbot泛域名证书申请
- Advantech PCI card 驱动注册 --W T