Java解决导出文件文件名乱码

版权声明: https://blog.csdn.net/Dongguabai/article/details/83308832
public static String processFileName(String fileName, HttpServletRequest request, HttpServletResponse response){
        String userAgent = request.getHeader("USER-AGENT");
        LOG.info("获取的Agent为:::::::{}",userAgent);
        try {
            if(StringUtils.contains(userAgent, "MSIE")){//IE浏览器
                fileName = URLEncoder.encode(fileName,"UTF8");
            }else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            }else{
                fileName = URLEncoder.encode(fileName,"UTF8");//其他浏览器
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return fileName;
    }
@RequestMapping("exportTagDetail")
    public void exportTagDetail(HttpServletRequest request, HttpServletResponse response, @RequestParam("area") String area, @RequestParam("orgId") String orgId, @RequestParam("tagTypeCode") String tagTypeCode) {
        String fileName = FileUtil.processFileName(FILE_NAME, request,response);
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        HSSFWorkbook wb = zJNtableExportService.getHSSFWorkbook(area, orgId, tagTypeCode);
        try {
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
            wb.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("流处理异常!");
        }
    }

猜你喜欢

转载自blog.csdn.net/Dongguabai/article/details/83308832