如何通过spring @RestController提供压缩下载的文件? [英] How to offer a file compressed as download via spring @RestController?

查看:1738
本文介绍了如何通过spring @RestController提供压缩下载的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个servlet,提供 CSV 文件供下载:

I have a servlet that offers a CSV file for download:

@RestController 
@RequestMapping("/")
public class FileController {

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public FileSystemResource getFile() {
        return new FileSystemResource("c:\file.csv"); 
    }
}

这很好用。

问题:如何将此文件作为压缩文件提供? (zip,gzip,tar没关系)?

Question: how can I offer this file as compressed file? (zip, gzip, tar doesn't matter)?

推荐答案

基于解决方案 where (对于普通的 Servlet ),你也可以这样做使用基于Spring MVC的控制器。

Based on the solution here (for a plain Servlet), you can also do the same with a Spring MVC based controller.

@RequestMapping(value = "/export", method = RequestMethod.GET)
public void getFile(OutputStream out) {
    FileSystemResource resource = new FileSystemResource("c:\file.csv"); 
    try (ZipOutputStream zippedOUt = new ZipOutputStream(out)) {
        ZipEntry e = new ZipEntry(resource.getName());
        // Configure the zip entry, the properties of the file
        e.setSize(resource.contentLength());
        e.setTime(System.currentTimeMillis());
        // etc.
        zippedOUt.putNextEntry(e);
        // And the content of the resource:
        StreamUtils.copy(resource.getInputStream(), zippedOut);
        zippedOUt.closeEntry();
        zippedOUt.finish();
    } catch (Exception e) {
        // Do something with Exception
    }        
}

您根据回复 OutputStream 创建了 ZipOutputStream (您可以简单地注入了方法)。然后为压缩流创建一个条目并写入。

You created a ZipOutputStream based on the responses OutputStream (which you can simply have injected into the method). Then create an entry for the zipped out stream and write it.

您还可以连接 HttpServletResponse OutputStream >这样您就可以设置文件名和内容类型。

Instead of the OutputStream you could also wire the HttpServletResponse so that you would be able to set the name of the file and the content type.

@RequestMapping(value = "/export", method = RequestMethod.GET)
public void getFile(HttpServletResponse response) {
    FileSystemResource resource = new FileSystemResource("c:\file.csv"); 
    response.setContentType("application/zip");
    response.setHeader("Content-Disposition", "attachment; filename=file.zip");

    try (ZipOutputStream zippedOUt = new ZipOutputStream(response.getOutputStream())) {
        ZipEntry e = new ZipEntry(resource.getName());
        // Configure the zip entry, the properties of the file
        e.setSize(resource.contentLength());
        e.setTime(System.currentTimeMillis());
        // etc.
        zippedOUt.putNextEntry(e);
        // And the content of the resource:
        StreamUtils.copy(resource.getInputStream(), zippedOut);
        zippedOUt.closeEntry();
        zippedOUt.finish();
    } catch (Exception e) {
        // Do something with Exception
    }        
}

这篇关于如何通过spring @RestController提供压缩下载的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆