<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>文件下载</h1>
<!--后端不能接收到,fileName为非中文能接收-->
<a th:href="@{/file/download/{fileName}(fileName='简历.docx')}">简历.docx</a>
</body>
</html>
public class FileController {
private static Logger log= LoggerFactory.getLogger(FileController.class);
@Value("${file.upload.dir}")
private String realPath;
@RequestMapping("/toDownload")
public String toDownload(){
return "views/download";
}
@GetMapping("/download/{fileName}")
public void download(@PathVariable("fileName") String fileName, HttpServletResponse httpServletResponse) throws IOException {
log.info("当前下载文件名:"+fileName);
log.info("当前下载文件目录:"+realPath);
if ("1.docx".equals(fileName)) fileName="简历.docx";
//1.指定目录读取文件
File file = new File(realPath, fileName);
//2.将文件读取为文件输入流
FileInputStream fileInputStream = new FileInputStream(file);
//设置附件形式
httpServletResponse.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
//3.获取响应输出流
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
//4.输入流复制给输出流
int len=0;
byte[] b=new byte[1024];
while (true){
len = fileInputStream.read(b);
if (len==-1) break;
outputStream.write(b,0,len);
}
outputStream.close();
//fileInputStream.close();
}
当把前端参数改为"1.docx"时,能正常接收
简历.docx
当参数改为上面的"简历.docx"时,后端接收不到
当不使用restful时,能够后端能正常接收请求
希望使用restful也能接收中文参数,或者明白为什么会不能使用中文参数
到Tomcat的server.xml里面配置一下编码,
uriencode=utf-8