2018-10-18 SpringMvc下载中文件时,中文文件名显示空白
1. 问题:获得Excel文件流并下载的过程中,如果文件名称包含中文,下载的文件中文显示空白 ( ).xls
public static void download(HttpServletRequest request, HttpServletResponse response) throws Exception {String fileName = "下载文件测试.xls";String filePath = "D:\\";response.setCharacterEncoding("UTF-8");response.setContentType("multipart/form-data");response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);try {InputStream inputStream = new FileInputStream(filePath);OutputStream os = response.getOutputStream();byte[] b = new byte[2048];int length;while ((length = inputStream.read(b)) > 0) {os.write(b, 0, length);}os.close();inputStream.close();} catch (Exception e){throw e;}
}
2. 解决方案:在设置fileName的时候进行转码
fileName = URLEncoder.encode(fileName,"utf-8");