其他分享
首页 > 其他分享> > ClassPathResource 下载模板

ClassPathResource 下载模板

作者:互联网

代码

@GetMapping("/downloadExcelTemplate")
    private boolean downloadTemplate(HttpServletResponse  response) throws UnsupportedEncodingException {
        ClassPathResource classPathResource = new ClassPathResource("static/template/客户.xlsx");
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(classPathResource.getFilename(),"UTF-8"));
        try (OutputStream outputStream = response.getOutputStream()) {
            BufferedInputStream inputStream = new BufferedInputStream(classPathResource.getInputStream());
            byte[] buffer = new byte[1024];
            while((inputStream.read(buffer))>0){
                outputStream.write(buffer);
                outputStream.flush();
            }
        } catch (IOException e) {
            throw  new  RuntimeException("导出失败,失败原因"+e.getMessage());
        }
        return false;
    }

配置

file template

问题

1.出现文件无法下载
2.下载之后没有内容(最直接是0K)

解决方案:

1.检查 ClassPathResource 路径是否一致
2. 看target文件是否有,没有重新编译
target

标签:outputStream,buffer,classPathResource,模板,ClassPathResource,new,response,下载
来源: https://blog.csdn.net/WindwirdBird/article/details/115027402