将文件打成压缩包下载(OSS或FASTDFS)

/**
 * 将文件打成压缩包下载
 *
 * @param zipModelDTOList 文件集合
 * @param uploadType 文件类型(OSS或FASTDFS)
 * @param response
 * @return void
 */
public static void downloadZipFiles(HttpServletResponse response, List<ZipModelDTO> zipModelDTOList, String uploadType) {
    try {
        response.reset();
        response.setCharacterEncoding("UTF-8");
        // 不同类型的文件对应不同的MIME类型
        response.setContentType("application/json;charset=utf-8");
        // --设置成这样可以不用保存在本地,再输出, 通过response流输出,直接输出到客户端浏览器中。
        ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
// 设置读取数据缓存大小
byte[] buffer = null;
try {
    // 循环读取文件路径集合,获取每一个文件的路径
    for (ZipModelDTO zipModelDTO : zipModelDTOList) {
        // 阿里oss 方式文件存储
        if (uploadType.equals(CommonConstant.UPLOAD_TYPE_FASTDFS)) {
            // 创建输入流读取文件
            buffer = FastdfsUtil.getFileStream(zipModelDTO.getFilePath());
            zipWrite(response, zipModelDTO, buffer, zos);
        }
        if (uploadType.equals(CommonConstant.UPLOAD_TYPE_OSS)) {
            // 创建输入流读取文件
            buffer = OssUtil.getFileStream(zipModelDTO.getFilePath());
            zipWrite(response, zipModelDTO, buffer, zos);
        }
    }
} catch (

IOException e) {
    throw new PdException(e.getMessage());
} finally {
    if (null != zos) {
        try {
            zos.close();
        } catch (IOException e) {
            throw new PdException(e.getMessage());
        }
    }
}

    } catch (IOException e) {
        e.printStackTrace();
    }
}
public static void zipWrite(HttpServletResponse response, ZipModelDTO zipModelDTO, byte[] buffer, ZipOutputStream zos) throws IOException {
    // 对文件名进行编码处理中文问题
    String zipBimName = new String(zipModelDTO.getFileName().getBytes(), StandardCharsets.UTF_8);
    // inline在浏览器中直接显示,不提示用户下载
    // attachment弹出对话框,提示用户进行下载保存本地
    // 默认为inline方式
    response.setHeader("Content-Disposition", "attachment;filename=" + zipBimName);
    response.setHeader("Access-Control-Allow-Origin", "*");
    // 将文件写入zip内,即将文件进行打包
    zos.putNextEntry(new ZipEntry(zipBimName + zipModelDTO.getFileExtension()));
    // 写入文件的方法,同上
    int size = 0;
    // 设置读取数据缓存大小
    zos.write(buffer, 0, buffer.length);
    // 关闭输入输出流
    // zos.closeEntry();
}

用到的工具包

 


 /**
     * FastdfsUtil获取文件缓存流
     */

    @Value("${fdfs.groupName}")
    private String fdfsGroupName;
    @Autowired
    private FastFileStorageClient client;
    private static FastdfsUtil fastdfsUtil;

    @PostConstruct
    public void init() {
        fastdfsUtil = this;
    }
    public static byte[] getFileStream(String fileUrl) {

        // 分离文件分组
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        // 分离文件路径
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        // 进行文件下载
        byte[] buffer = fastdfsUtil.client.downloadFile(group, path, new DownloadByteArray());

        return buffer;
    }

   /**
     * OssUtil获取文件缓存流
     */
  /**
     * ---------变量----------
     */
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    // 文件存储目录
    private static String filedir = "workbook/";

    private static OssUtil ossUtil;
    private static OSS ossClient;

    @PostConstruct
    public void init() {
        ossUtil = this;
        ossClient = new OSSClientBuilder().build(ossUtil.endpoint, ossUtil.accessKeyId, ossUtil.accessKeySecret);
    }
    public static byte[] getFileStream(String fileUrl) throws IOException {
        // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
        // OSS ossClient = new OSSClientBuilder().build(ossUtil.endpoint, ossUtil.accessKeyId, ossUtil.accessKeySecret);
        OSSObject ossObject = ossClient.getObject(ossUtil.bucketName, fileUrl);
        // 读取文件内容。
        InputStream inputStream = ossObject.getObjectContent();
        // 把输入流放入缓存流
        BufferedInputStream in = new BufferedInputStream(inputStream);

        // byte[] buffer = inputStream.readAllBytes(); //java 11
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        int rc = 0;
        while ((rc = inputStream.read(buff, 0, 1024)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in_b = swapStream.toByteArray();
        return in_b;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值