java 文件下载,压缩


    public static void downloadFile(String fileUrl, String savePath) throws IOException {
        URL url = new URL(fileUrl);
        InputStream in = url.openStream();
        FileOutputStream out = new FileOutputStream(savePath);

        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }

        in.close();
        out.close();
    }
    public Map<String, Object> attachmentRecordDown(Map<String, Object> params) {
        Map<String,Object>map = new HashMap<>();
        String files = MapUtils.getString(params,"files");

        if (StringUtils.isBlank(files)){
            throw new CustomException("1","文件地址不能为空!");
        }

        String[] split = files.split(",");
        if (split.length<1){
            throw new CustomException("1","文件地址不能为空!");
        }

        File fileZip = new File(path+"/zip");
        //如果已经存在则删除
        if (fileZip.exists()) {
            fileZip.delete();
        }
        //检查是否存在
        if (!fileZip.exists())
        {
            fileZip.mkdirs();
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateStr = df.format(new Date());

        List<String> paths = new ArrayList<>();
        for (String s:split){
            s = downPath+s;
            String fileName1=s.substring(s.lastIndexOf("/")+1);
            String filePaths = fileZip+"/"+fileName1;
            try {
                URL url = new URL(s);
                InputStream fis = url.openStream();
                BufferedInputStream bis =new BufferedInputStream(fis);
                FileOutputStream fos = new FileOutputStream(filePaths);
                BufferedOutputStream bos =new BufferedOutputStream(fos);
                int read;
                while ((read = bis.read())!=-1){
                    bos.write(read);
                }
                bis.close();
                bos.close();
                fis.close();
                fos.close();
                paths.add(filePaths);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        try {
            compress(paths,fileZip+"/"+dateStr+".zip");
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("path","/localFile/zip/"+dateStr+".zip");
        return map;
    }

    public static int compress(List<String> filePaths, String zipFilePath) throws IOException{
        // 压缩下载保存的路径
        File zipFile = new File(zipFilePath);
        //设置压缩流:直接写入response,实现边压缩边下载
        ZipOutputStream zipOs = null;
        //循环将文件写入压缩流
        DataOutputStream os = null;
        //文件
        File file;
        try {
            zipOs = new ZipOutputStream(new FileOutputStream(zipFile));
            //设置压缩方法
            zipOs.setMethod(ZipOutputStream.DEFLATED);
            for (String path : filePaths) {
                String fileName=path.substring(path.lastIndexOf("."));
                fileName=path.substring(path.lastIndexOf("/")+1);
                file = new File(path);
                if (!file.exists()) {
                    throw new RuntimeException("文件不存在");
                }
                //添加ZipEntry,并将ZipEntry写入文件流
                zipOs.putNextEntry(new ZipEntry(fileName));
                os = new DataOutputStream(zipOs);
                FileInputStream fs = new FileInputStream(file);
                byte[] b = new byte[100];
                int length;
                //读入需要下载的文件的内容,打包到zip文件
                while ((length = fs.read(b)) != -1) {
                    os.write(b, 0, length);
                }

                //关闭流
                fs.close();
                zipOs.closeEntry();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭流
            for (String str:filePaths){
                File fileExcel = new File(str);
                //如果已经存在则删除
                if (fileExcel.exists()) {
                    fileExcel.delete();
                }
                String substring = str.substring(0, str.lastIndexOf("/"));
                fileExcel = new File(substring);
                // 判断该temp对象是否为目录对象
                if (fileExcel.isDirectory()) {
                    fileExcel.delete();
                }
            }
            try {
                if (os != null) {
                    os.flush();
                    os.close();
                }
                if (zipOs != null) {
                    zipOs.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值