线上文件打包成zip下载

 当前项目一般都是有一个独立的文件系统,前端先将文件上传至文件系统,获取到一个fileKey保存到数据库即可;再需要下载时,只需要对应的系统url+fileKey即可获取下载文件;我这里代码使用filePath代替fileKey

如何批量打包下载呢?

 先获取到批量文件的list对象,对象包含两个字段fileName和filePath即可;转换为json,我这里的格式:
      [

       {"fileName":"我的文件1","filePath":"246542134.jpg"},
       {"fileName":"我的文件2","filePath":"546542135.doc"},
       {"fileName":"我的文件3","filePath":"578532136.text"},

       ...
       ]

     工具类:
     *fileName为文件原名,filePath为实际地址fileKey
     *urlPrefix 为文件系统的url前缀,如http://xxx.com/file

package com.youdingte.saas.ewechat.biz.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.compress.utils.IOUtils;

import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 文件系统oss获取文件,打包
 * zip 打包 返回byte[]
 *
 * @author yrl
 * @date 2022/2/24 11:22
 */
public class ZipFileUtil {


    /**
     *json为查询文件list,我这里的格式
      [{"fileName":"我的头像1","filePath":"/xxxx/2134.jpg"},
       {"fileName":"我的头像2","filePath":"/xxxx/2135.jpg"},
       {"fileName":"我的头像3","filePath":"/xxxx/2136.jpg"}
       ]
     *fileName为文件原名,filePath为实际地址fileKey
     *urlPrefix 为文件系统的url前缀,如http://xxx.com/file
     *
     */
    public static byte[] doItReport4Attachments(String json,String urlPrefix) {
        ObjectMapper mapper = new ObjectMapper();
        TypeReference<List<HashMap<String,String>>> typeRef
                = new TypeReference<List<HashMap<String,String>>>() {};

        List<HashMap<String,String>> o = null;
        try {
            o = mapper.readValue(json, typeRef);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        Map<String, File> srcFiles = new HashMap<>();
        assert o != null;
        int ix = 1;
        for (HashMap<String,String> e : o) {
            if (e.get("filePath") == null) {
                continue;
            }

            File file = null;
            try {
                file = File.createTempFile("report4-attach-", ".tmp");
                //拼接系统url+具体文件【可直接访问】如:http://xxxx.com/file/xxx/2134.jpg
                copyURLToFile(new URL(urlPrefix + e.get("filePath")), file);

                String key = ix + "." + e.get("fileName");

                srcFiles.put(key, file);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            ix++;
        }
        Path zipPath = zip(srcFiles);
        if (zipPath != null) {
            try {
                return Files.readAllBytes(zipPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new byte[]{};
    }

    public static Path zip(Map<String, File> srcFiles) {
        try {
            File file = File.createTempFile("multiCompressed", ".zip");
            FileOutputStream fos = new FileOutputStream(file);
            ZipOutputStream zipOut = new ZipOutputStream(fos);
            for (Map.Entry<String, File> srcFile : srcFiles.entrySet()) {
                File fileToZip = srcFile.getValue();
                FileInputStream fis = new FileInputStream(fileToZip);
                ZipEntry zipEntry = new ZipEntry(srcFile.getKey());
                zipOut.putNextEntry(zipEntry);

                byte[] bytes = new byte[1024];
                int length;
                while((length = fis.read(bytes)) >= 0) {
                    zipOut.write(bytes, 0, length);
                }
                fis.close();
            }
            zipOut.close();
            fos.close();
            return file.toPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void copyURLToFile(URL source, File destination) throws IOException {
        InputStream input = source.openStream();

        try {
            FileOutputStream output = openOutputStream(destination);

            try {
                IOUtils.copy(input, output);
            } finally {
                IOUtils.closeQuietly(output);
            }
        } finally {
            IOUtils.closeQuietly(input);
        }

    }

    public static FileOutputStream openOutputStream(File file) throws IOException {
        if (file.exists()) {
            if (file.isDirectory()) {
                throw new IOException("File '" + file + "' exists but is a directory");
            }

            if (!file.canWrite()) {
                throw new IOException("File '" + file + "' cannot be written to");
            }
        } else {
            File parent = file.getParentFile();
            if (parent != null && !parent.exists() && !parent.mkdirs()) {
                throw new IOException("File '" + file + "' could not be created");
            }
        }

        return new FileOutputStream(file);
    }

}

工具类返回byte[]即可,controller返回ResponseEntity<byte[]>到前端即可

我这边的测试controller如下:

    @ApiOperation("批量下载")
    @GetMapping("/download")
    public ResponseEntity<byte[]> download(List<Long> ids) throws UnsupportedEncodingException {
        //文件系统的url
        String ossUrl = "http://xxxx.com/file";
        
        byte[] bytes = null;
        //ids为文件ids,查询到需要下载的文件地址
        if (ids != null && ids.size() > 0) {
            //SysFile测试,只有fileName和FilePath两个字段
            List<SysFile> sysFiles =  fileService.getByIds(ids);
          
            if (sysFiles .size() > 0) {
                //转化为json
                String jsonString = JsonUtil.toJson(sysFiles);
                //调用工具类
                bytes = ZipFileUtil.doItReport4Attachments(jsonString, ossUrl);
            }
        }
        if (bytes != null) {
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/octet-stream");
            headers.add("Connection", "close");
            headers.add("Accept-Ranges", "bytes");
            headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode("attachments.zip", "UTF-8"));
            return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
        } else {
            return null;
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用`java.util.zip`包来实现根据URL将多个文件打包zip并进行下载的功能。下面是一个示例代码: ```java import java.io.*; import java.net.URL; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class FileZipDownloader { public static void main(String[] args) { String zipUrl = "http://example.com/files.zip"; String[] fileUrls = {"http://example.com/file1.txt", "http://example.com/file2.txt"}; try { // 创建ZipOutputStream FileOutputStream fos = new FileOutputStream("downloaded_files.zip"); ZipOutputStream zos = new ZipOutputStream(fos); // 从URL下载并添加文件zip中 for (String fileUrl : fileUrls) { URL url = new URL(fileUrl); InputStream is = url.openStream(); zos.putNextEntry(new ZipEntry(url.getFile())); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) > 0) { zos.write(buffer, 0, length); } is.close(); zos.closeEntry(); } // 关闭流 zos.close(); fos.close(); // 下载zip文件 downloadZip(zipUrl); } catch (IOException e) { e.printStackTrace(); } } private static void downloadZip(String url) throws IOException { URL zipUrl = new URL(url); InputStream is = zipUrl.openStream(); FileOutputStream fos = new FileOutputStream("downloaded_files.zip"); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) > 0) { fos.write(buffer, 0, length); } is.close(); fos.close(); } } ``` 上述代码首先创建了一个`ZipOutputStream`对象,然后依次遍历多个文件的URL,将每个文件下载后添加到zip中,并关闭流。最后调用`downloadZip`方法根据zip文件的URL进行下载。请确保提供的URL和文件格式的可用性和正确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值