iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >手把手教你用SpringBoot将文件打包成zip存放或导出
  • 832
分享到

手把手教你用SpringBoot将文件打包成zip存放或导出

2024-04-02 19:04:59 832人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

环境准备 其实也没什么准备,准备好SpringBoot就行,还有几张图片: 将文件打包成Zip存放 代码 Controller代码: @RequestMapping("/zip

环境准备

其实也没什么准备,准备好SpringBoot就行,还有几张图片:

在这里插入图片描述

将文件打包成Zip存放

代码

Controller代码:


@RequestMapping("/zip")
@RestController
public class ZipController {

    
    @PostMapping("package")
    public void packageFileToZip() throws ioException {
        // 为了方便我直接将文件地址写好了,各位可以根据自己的情况修改
        String[] filePath = new String[]{"E:\\ykds\\1068128498917799516.jpg", "E:\\ykds\\1068128498917917980.jpg", "E:\\ykds\\1068128498917807874.jpg"};
        // 将需要打包的文件都放在一个集合中
        List<File> fileList = new ArrayList<>();
        for (String s : filePath) {
            File file = new File(s);
            fileList.add(file);
        }
        // 先在D盘创建一个压缩包
        File zipFile = new File("D:\\package.zip");
        if(!zipFile.exists())
            zipFile.createNewFile();
        // 将package.zip的File对象传到toZip对象中
        ZipUtils.toZip(fileList, zipFile);
    }
}

ZipUTils工具类代码


public class ZipUtils {

    
    public static void toZip(List<File> srcFiles, File zipFile) throws IOException {
        if(zipFile == null){
            return;
        }
        if(!zipFile.getName().endsWith(".zip")){
            return;
        }
        ZipOutputStream zos = null;
        FileOutputStream out = new FileOutputStream(zipFile);
        try {
            zos = new ZipOutputStream(out);
            for (File srcFile : srcFiles) {
                byte[] buf = new byte[BUFFER_SIZE];
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                int len;
                // 读取文件并写入到zip中
                FileInputStream in = new FileInputStream(srcFile);
                while ((len = in.read(buf)) != -1) {
                    zos.write(buf, 0, len);
                    zos.flush();
                }
                in.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (zos != null) {
                zos.close();
            }
        }
    }
}

测试

代码打好了,接下来测试下,打开熟悉的postman:

在这里插入图片描述

调用接口后就会在D盘中新建一个package.zip的压缩包:

在这里插入图片描述

可以看到,我打包的文件都在这里,再看看能不能正常显示:

在这里插入图片描述

very Good!

将文件打包成zip并导出

上面的方法只是将压缩包保存在本地,如果需要导出的话代码有点不一样。

代码

Controller代码:



    @PostMapping("download")
    public void download(httpservletResponse response) throws IOException {
        // 这里还是和上面一样
        String[] filePath = new String[]{"E:\\ykds\\1068128498917799516.jpg", "E:\\ykds\\1068128498917917980.jpg", "E:\\ykds\\1068128498917807874.jpg"};
        List<File> fileList = new ArrayList<>();
        for (String s : filePath) {
            File file = new File(s);
            fileList.add(file);
        }
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=download.zip");
        ZipUtils.downloadZip(response.getOutputStream(), fileList);
    }

ZipUtils工具类代码


public static void downloadZip(OutputStream outputStream, List<File> fileList){
        BufferedInputStream bufferedInputStream = null;
        ZipOutputStream zipOutputStream = null;
        try {
            zipOutputStream = new ZipOutputStream(outputStream);
            for (File file : fileList) {
                ZipEntry zipEntry = new ZipEntry(file.getName());
                zipOutputStream.putNextEntry(zipEntry);
                byte[] buf = new byte[BUFFER_SIZE];
                int len;
                FileInputStream in = new FileInputStream(file);
                while ((len = in.read(buf)) != -1) {
                    zipOutputStream.write(buf, 0, len);
                    zipOutputStream.flush();
                }
            }
            zipOutputStream.flush();
            zipOutputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }
                if (zipOutputStream != null ) {
                    zipOutputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

测试

还是用postman:

在这里插入图片描述
在这里插入图片描述

下载完成后打开看看

在这里插入图片描述

到此这篇关于手把手教你用springBoot将文件打包成zip存放或导出的文章就介绍到这了,更多相关SpringBoot将文件打包成zip内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 手把手教你用SpringBoot将文件打包成zip存放或导出

本文链接: https://www.lsjlt.com/news/128117.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作