POI导出excel表时文件名变成乱码怎么办

在用POI进行excel表导出时,遇到中文文件名乱码问题,用下面的方法得到了解决。

转载自:ment" style="">// 判断浏览器类型,firefox浏览器做特殊处理,否则下载文件名乱码</span>

<span class="hljs-keyword" style="">public</span> <span class="hljs-keyword" style="">static</span> <span class="hljs-built_in" style="">void</span> compatibleFileName(HttpServletRequest request, HttpServletResponse response, <span class="hljs-built_in" style="">String</span> excelname) throws UnsupportedEncodingException {

<span class="hljs-built_in" style="">String</span> agent = request.getHeader(<span class="hljs-string" style="">"USER-AGENT"</span>).toLowerCase();

response.setContentType(<span class="hljs-string" style="">"application/vnd.ms-excel"</span>);

<span class="hljs-built_in" style="">String</span> fileName = excelname;

<span class="hljs-built_in" style="">String</span> codedFileName = java.net.URLEncoder.encode(fileName, <span class="hljs-string" style="">"UTF-8"</span>);

<span class="hljs-keyword" style="">if</span> (agent.contains(<span class="hljs-string" style="">"firefox"</span>)) {

response.setCharacterEncoding(<span class="hljs-string" style="">"utf-8"</span>);

response.setHeader(<span class="hljs-string" style="">"content-disposition"</span>, <span class="hljs-string" style="">"attachment;filename="</span> + <span class="hljs-keyword" style="">new</span> <span class="hljs-built_in" style="">String</span>(fileName.getBytes(), <span class="hljs-string" style="">"ISO8859-1"</span>) + <span class="hljs-string" style="">".xls"</span>);

} <span class="hljs-keyword" style="">else</span> {

response.setHeader(<span class="hljs-string" style="">"content-disposition"</span>, <span class="hljs-string" style="">"attachment;filename="</span> + codedFileName + <span class="hljs-string" style="">".xls"</span>);

}

}</code>