Skip to content

Instantly share code, notes, and snippets.

@xcaspar
Created July 21, 2015 07:32
Show Gist options
  • Save xcaspar/b600629ec7d75e500e0d to your computer and use it in GitHub Desktop.
Save xcaspar/b600629ec7d75e500e0d to your computer and use it in GitHub Desktop.

解决下载文件名乱码问题

在做文件下载中,发现只要文件中含有中文,就会乱码,即使设置了以下内容,也会出现此情况,

response.setContentType("application/octet-stream; charset=utf-8");

解决此问题的方法是设置响应头,将文件名字符串按ISO8859-1进行编码,代码如下:

response.setHeader("Content-Disposition", "attachment; filename=" +  new String(fileName.getBytes("UTF-8"),"ISO8859-1"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment