SpringBoot文件下载的时候文件名出现问题

1.一开始文件名全是下横线__

经查询之后,添加了这行代码

				response.setHeader("Content-Disposition",
						"attachment;filename=" + URLEncoder.encode(homeworkname, "utf-8"))

2.下横线的问题解决了,但出现中文乱码,出现%%字符

再次查找之类,将上述的代码改成以下,解决乱码

response.setHeader("Content-Disposition", "attachment;filename="+  homeworkname +";filename*=utf-8''"+URLEncoder.encode(homeworkname,"UTF-8"));

除此之外,发现也可以用这种方法

				response.setHeader("Content-Disposition",
						"attachment;filename=" + new String(homeworkname.getBytes("utf-8"), "ISO8859-1"));

参考博客:
https://blog.csdn.net/erf_1012/article/details/101026526

你可能感兴趣的:(java,SpringBoot,乱码)