java汉字变下划线_Spring boot中下载文件名称中文变___________下划线了,请问如何处理?...

在Spring Boot应用中,当处理文件下载时,遇到中文文件名被转换为下划线的情况。通过在代码中加入`new String(filename.getBytes(), "utf-8")`进行UTF-8编码转换,可以解决文件名乱码问题。具体实现是在`Content-Disposition`头中设置正确的文件名,并确保文件路径正确。" 100767893,8076086,Python操作Redis详解,"['Python编程', 'Redis数据库', '数据存储', 'NoSQL', '键值对']
摘要由CSDN通过智能技术生成

@GetMapping("/download")

@ResponseBody

public ResponseEntity serveFile(@RequestParam String filename) throws IOException{

filename=new String(filename.getBytes(),"utf-8");//有没有这句话

Resource file=new FileSystemResource("D:\\workspace\\idea_me\\demo\\upload-dir"+File.separator+ filename);

HttpHeaders headers = new HttpHeaders();

headers.add("Cache-Control", "no-cache, no-store, must-revalidate");

headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getFilename()));

headers.add("Pragma", "no-cache");

headers.add("Expires", "0");

return ResponseEntity

.ok()

.headers(headers)

.contentLength(file.contentLength())

.contentType(MediaType.parseMediaType("application/octet-stream;charset=utf-8"))

.body(new InputStreamResource(file.getInputStream()));

}

43c363214b743ef792de76bff2258ee6.png

5ae5af644e34b1116fe935024d173363.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值