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;
    }

配置

ClassPathResource 下载模板

问题

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

解决方案:

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

上一篇:安卓assets处理


下一篇:Android Assets文件夹下的文件无法获取输出流Outputstream