springboot 文件下载

简介: springboot 文件下载

1.原始的方式

@ApiOperation(value = "下载参数导入模板")
    @GetMapping("/template/parameters/excel/download")
    public void   downloadExcelTemplate(HttpServletResponse response, HttpServletRequest request) throws Exception {
        ClassPathResource resource = new ClassPathResource("templates/parameterTemplate.xls");
        InputStream in =resource.getInputStream();
        String fileName = "parameterTemplate.xls";

        int len ;
        byte[] buffer = new byte[1024];
        response.setHeader("content-disposition", String.format("attachment; filename=%s", fileName));
        response.setCharacterEncoding("UTF-8");
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);

        OutputStream out = response.getOutputStream();
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        out.flush();
        in.close();

    }


    @ApiOperation(value = "下载参数导入模板")
    @GetMapping(value = "/template/parameters/excel/download123",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void   downloadExcelTemplate22(HttpServletResponse response, HttpServletRequest request) throws Exception {


        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");


        // 设置要导出的文件的名字
        String fileName = "users"  + new Date() + ".xls";

        // 新增数据行,并且设置单元格数据
        int rowNum = 1;

        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "id", "uid", "地址", "城市"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        for (int i = 0; i <10 ; i++) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(00);
            row1.createCell(1).setCellValue(01);
            row1.createCell(2).setCellValue(02);
            row1.createCell(3).setCellValue(03);
            rowNum++;
        }
        

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());

    }

2.新的方式

 @ApiOperation(value = "下载参数导入模板")
    @GetMapping(value = "/template/parameters/excel/download2",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<byte[]> downloadExcelTemplate2() throws Exception {
        String resourceLocation = "classpath:templates/1.jpg";
        File downloadFile = ResourceUtils.getFile(resourceLocation);
        return getDownloadHttpHeaders(downloadFile);
    }




    private ResponseEntity getDownloadHttpHeaders(File downloadFile) throws Exception {
        byte[] output = new byte[0];
        try {
            output = FileUtils.readFileToByteArray(downloadFile);
        } catch (IOException e) {
            GlobalException.throwException("下载失败!");
        }
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.valueOf(MediaType.APPLICATION_OCTET_STREAM_VALUE));
        responseHeaders.setContentLength(output.length);
        responseHeaders.set("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(downloadFile.getName(), "UTF-8")+"\"");
        return new ResponseEntity<>(output, responseHeaders, HttpStatus.OK);
    }

3. BufferedInputStream读取的流下载

 private ResponseEntity getDownloadHttpHeaders() throws Exception {
        AmazonS3 s3Client=S3Utils.getS3Client("XXXX","XXXX","http://1XXX");
        S3Object s3Object = s3Client.getObject(
                "s3_container",
                "test.tar.gz"
        );
        BufferedInputStream bufferedInputStream = new BufferedInputStream(
                s3Object.getObjectContent()
        );
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();

        int len;
        byte []buf=new byte[2048];
        while((len=bufferedInputStream.read(buf))!=-1){
            byteArrayOutputStream.write(buf, 0, len);
        }
        byteArrayOutputStream.flush();
        byte[]  output=byteArrayOutputStream.toByteArray();


        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.valueOf(MediaType.APPLICATION_OCTET_STREAM_VALUE));
        responseHeaders.setContentLength(output.length);
        responseHeaders.set("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode("test.tar.gz", "UTF-8")+"\"");
        return new ResponseEntity<>(output, responseHeaders, HttpStatus.OK);
    }
相关文章
|
6月前
|
Java
Springboot文件下载跨域问题解决方案
Springboot文件下载跨域问题解决方案
|
Java 测试技术 Spring
Spring Boot入门(11)实现文件下载功能
  在这篇博客中,我们将展示如何在Spring Boot中实现文件的下载功能。   还是遵循笔者写博客的一贯风格,简单又不失详细,实用又能让你学会。
2209 0
|
4月前
|
Java
软件开发常用之SpringBoot文件下载接口编写(下),Vue+SpringBoot文件上传下载预览,服务器默认上传是1M,可以调节,调节文件上传大小写法,图片预览,如何预览后下次还能看到,预览写法
软件开发常用之SpringBoot文件下载接口编写(下),Vue+SpringBoot文件上传下载预览,服务器默认上传是1M,可以调节,调节文件上传大小写法,图片预览,如何预览后下次还能看到,预览写法
|
4月前
|
存储 安全 Java
Spring Boot中的文件下载实现
Spring Boot中的文件下载实现
|
6月前
|
XML Java 数据格式
SpringBoot文件下载(Zip & Xml)
SpringBoot文件下载(Zip & Xml)
390 0
|
6月前
|
前端开发
SpringBoot+vue实现文件下载
SpringBoot+vue实现文件下载
393 0
|
Java API Spring
spring boot中Excel文件下载踩坑大全
spring boot中Excel文件下载踩坑大全
1729 2
spring boot中Excel文件下载踩坑大全
springboot 各种文件下载方式(最全)
springboot 各种文件下载方式(最全)
3300 0
Java小白翻身-基于SpringBoot的文件下载系统 3
Java小白翻身-基于SpringBoot的文件下载系统 3
126 0
|
Java Spring
spring boot 实现文件下载
主要介绍了spring boot 实现文件下载