EasyExcel导出时文件名乱码


response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("UTF-8");
//在后端的接口中开放这个这个响应头,前端的response就可以获取到了
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");//开放权限,让前端获取到

//1、下面 浏览器下载正常,但是postman下载文件名称乱码
//response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
//2、下面这是用postman测名称正常,换到浏览器会多了filename_=utf-8等字样。。)
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")+ ".xlsx"
                  + ";filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8")+ ".xlsx");
       
 //下面都是提示                            
//提示:前端获取不到后端定义的Header的解决办法
//后端添加的Header,浏览器可以看到这个Header,但前端的response获取不到自定义的Content-Disposition,只能获取到content-type
  response.addHeader("Content-Disposition","attachment;filename=" + java.net.URLEncoder.encode(filename, "UTF-8"));

 

EasyExcel导出时文件名乱码_第1张图片

你可能感兴趣的:(java,开发语言)