登录    关于
马育民老师的博客

马育民的博客

QQ:65242847

springmvc 下载文件、中文乱码

@GetMapping("/exportMaterial")
public ResponseEntity<byte[]> export(HttpServletRequest request, Dim dim ){

    try {
        String uuid = UUID.randomUUID().toString().replace("-","");
        String path = request.getServletContext().getRealPath("temp/"+uuid+".xls");
        Material material=new Material();
        material.setWmType(dim.getInventoryId());
        material.setCode(dim.getKeyword());
        List list=iMaterialService.query(material);
        POI.getExcel(list,path);

        File file = new File(path);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        String fileName = "原材料.xls";
        headers.setContentDispositionFormData("attachment",java.net.URLEncoder.encode(fileName, "UTF-8"));
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
    }catch (Exception e){
        log.error(e);
        return null;
    }
}

原文出处:https://malaoshi.top/show_1IX22YhbO5Gb.html