文件打包并下载(代码)

    private byte[] downloadImages(HttpServletResponse response, String filePath) {
        File file = new File(filePath);
        response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
        byte[] bytes = null;
        try{
            bytes = FileUtils.readFileToByteArray(file);
        }catch (IOException e){
            e.printStackTrace();
        }
        return bytes;
    }

    private String compressProcess(Long parkingId, List<String> imageNames){
        String path = parkingImagePath;
        String parkingDir = "/" + parkingId;
        createDirectories(path + parkingDir);
        imageNames.forEach(imageName -> copyFiles(path + "/" + imageName, path + parkingDir));
        zip(path + parkingDir);
        deleteDir(path + parkingDir);
        return path + parkingDir + ".zip";
    }

    private static void createDirectories(String path){
        File dateDirectory = new File(path);
        dateDirectory.mkdir();
    }

    private static void copyFiles(String sourceFile, String targetPath){
        File source = new File(sourceFile);
        File target = new File(targetPath);
        try {
            FileUtils.copyFileToDirectory(source, target);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    private static void zip(String zipPath){
        FileOutputStream fos = null;
        ZipOutputStream zipOut = null;
        try {
            fos = new FileOutputStream(zipPath  + ".zip");
            zipOut = new ZipOutputStream(fos);
            File fileToZip = new File(zipPath);
            zipFile(fileToZip, fileToZip.getName(), zipOut);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                zipOut.close();
                fos.close();
            }catch (IOException e){
                e.printStackTrace();
            }

        }


    }
    private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut){

        if (fileToZip.isHidden()) {
            return;
        }
        if (fileToZip.isDirectory()) {
            File[] children = fileToZip.listFiles();
            for (File childFile : children) {
                zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
            }
            return;
        }
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(fileToZip);
            ZipEntry zipEntry = new ZipEntry(fileName);
            zipOut.putNextEntry(zipEntry);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = fis.read(bytes)) >= 0) {
                zipOut.write(bytes, 0, length);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                fis.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    private static void deleteDir(String path){
        try {
            FileUtils.deleteDirectory(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

注意:Controller方法上注解添加produces属性

produces = MediaType.APPLICATION_OCTET_STREAM_VALUE
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值