首页 技术笔记正文

Java下载文件,中文文件名乱码问题解决

拾柴者 技术笔记 2019-11-03 22:09:54 147 1

在项目中经常会做下载文件、导出Excel 等问题,经常会遇到乱码,本次从设置UTf_8编码格式和检测浏览器,并设置相关参数的方式解决下载中文乱码问题。直接上代码:

Controller层
@RequestMapping("getExcel")
public void getUserExcel(HttpServletResponse response, HttpServletRequest request){
    try{
        UserService.getUserExcel(response,  request);
    }catch(Exception e){
        e.printStackTrace();
    }
}
ServerImpl层
public void getUserExcel(HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
        int i=2;
//        response.setHeader("Content-disposition",
//                "attachment;filename=" + URLEncoder.encode("员工表.xls","UTF-8"));
//        response.setContentType("application/msexcel;charset=UTF-8");
//        response.setCharacterEncoding("UTF-8");
        String userAgent = request.getHeader("User-Agent");
        String formFileName = "员工表.xls";

        // 针对IE或者以IE为内核的浏览器:
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            formFileName = java.net.URLEncoder.encode(formFileName, "UTF-8");
        } else {
            // 非IE浏览器的处理:
            formFileName = new String(formFileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        response.setHeader("Content-disposition",String.format("attachment; filename=\"%s\"", formFileName));
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("UTF-8");
        List<User> userlist=UserMapper.getUserExcel();
        List<Userinfo> userinfoList=new ArrayList<>();
        for(User user:userlist){
            Userinfo userinfo= new Userinfo ();
            String workno=User.getWorkno();
            if(workno == null || "".equals(workno)) continue;
            User users=userMapper.getEmployById(Integer.valueOf(workno));
            if(users== null|| "".equals(users)) continue;
            userinfo.setExtention(user.getExtention());
            userinfo.setName(users.getName());
            userinfo.setPart(users.getDepName());
            userinfo.setAccount(users.getAccount());
            userinfo.setWorkno(calluser.getWorkno());
            userTelnoList.add(userinfo);
        }
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet=wb.createSheet("员工表");
        HSSFRow row1=sheet.createRow(0);
        HSSFCell cell=row1.createCell(0);
        cell.setCellValue("员工一览表");
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,5));
        HSSFRow row2=sheet.createRow(1);
        row2.createCell(0).setCellValue("姓名");
        row2.createCell(1).setCellValue("账号");
        row2.createCell(2).setCellValue("部门");
        row2.createCell(3).setCellValue("职位");
        row2.createCell(4).setCellValue("座机号");
        for(Userinfo userinfo:userinfoList){
            HSSFRow row=sheet.createRow(i);
            row.createCell(0).setCellValue(userinfo.getName());
            row.createCell(1).setCellValue(userinfo.getAccount());
            row.createCell(2).setCellValue(userinfo.getPart());
            row.createCell(3).setCellValue(userinfo.getPosition());
            row.createCell(4).setCellValue(userinfo.getTelno());
            i++;
        }
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            wb.write(outputStream);
            wb.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            if(outputStream != null){
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
关键代码
String userAgent = request.getHeader("User-Agent");
String formFileName = "员工表.xls";

// 针对IE或者以IE为内核的浏览器:
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
    formFileName = java.net.URLEncoder.encode(formFileName, "UTF-8");
} else {
    // 非IE浏览器的处理:
    formFileName = new String(formFileName.getBytes("UTF-8"), "ISO-8859-1");
}
response.setHeader("Content-disposition",String.format("attachment; filename=\"%s\"", formFileName));
response.setContentType("multipart/form-data");
response.setCharacterEncoding("UTF-8");


打赏 支付宝打赏 微信打赏

评论

精彩评论

双十一优惠

点击关闭
  • 在线客服1