java批量下载并压缩打包(本地下载,网络Url下载,FasfDfsClient批量下载)

由于每次查找资料都需要耗费大量时间,于是写下此文章,方便日后查阅复用.

**1.适用于网络本地下载

实现思路:根据前台传过来的参数,循环将文件放置压缩流,然后创建zip输入流读取文件,最后发返回前端压缩文件.

 @RequestMapping(value = "/downloadPLFile")
    public void plPDF(HttpServletRequest  request, HttpServletResponse response) throws Exception {
        String wjlj = request.getParameter("wjlj");//文件路径  前端获取到的参数
        System.out.println(wjlj);
        String[] wjljList = wjlj.split(",");
        /*List<String> wjgsList = Arrays.asList(wjgs.split(","));*/
        try {
            String zipName =  StringUtil.getPrimaryKeyNo()  + ".zip"; //随机生成的
            File file = new File(zipName);
            if (!file.exists()){
                file.createNewFile();
            }
            FileOutputStream fileOutputStream =new FileOutputStream(file);
            ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);

            //循环读取文件路径集合,获取每一个文件路径
            for (int i = 0; i < wjljList.length; i++) {
                File file1 = new File(wjljList[i]);
                if (!file1.exists()){
                    file1.getParentFile().mkdirs();
                    file1.createNewFile();
                }
                FileInputStream fis = new FileInputStream(file1);

                zipOutputStream.putNextEntry(new ZipEntry(StringUtil.getPrimaryKeyNo()+file1.getName()));

                int len = 0;
                byte[] buffer = new byte[1024];
                while ((len = fis.read(buffer)) > 0) {
                    zipOutputStream.write(buffer, 0, len);
                }
                //关闭流
                zipOutputStream.closeEntry();
                fis.close();
            }
            zipOutputStream.close();

            InputStream ins = new FileInputStream(zipName);
            BufferedInputStream bis = new BufferedInputStream(ins);
            OutputStream outputStream = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(outputStream);
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipName.getBytes("UTF-8"), "ISO-8859-1"));
            int bytesRead = 0;
            byte[] buff = new byte[8192];
            while ((bytesRead = bis.read(buff, 0, 8192)) != -1) {
                bos.write(buff, 0, bytesRead);
            }
            bos.flush();
            ins.close();
            bis.close();
            bos.close();
            outputStream.close();


        }catch (Exception e){
            logger.error(FrameUtil.getLogInfo("business_error"), e);
            e.printStackTrace();
        }

    }

**2.根据URL批量下载

@RequestMapping(value = "/downloadCgsDzdaPLFile")

public void downloadCgsDzdaPLFile(HttpServletRequest  request, HttpServletResponse response) throws Exception {
  String wjlj = request.getParameter("wjlj");//文件路径  group1/M00/00/58/RAy5W1_q1Z2AfL7_AAAoGMuKQCo260.pdf
        System.out.println(wjlj);
        String wjgs = request.getParameter("wjgs");//文件格式
        System.out.println(wjgs);
        String urlPath = request.getParameter("urlPath");
        String[] wjljList = wjlj.split(",");
        try {
            int lengh ;
            String downloadName = StringUtil.getPrimaryKeyNo() + ".zip";
            File zipName = new File(downloadName);
            if(zipName.exists()){
                zipName.createNewFile();
            }
            FileOutputStream fileOutputStream =new FileOutputStream(zipName);
            ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
            File file = null;
            for (int i = 0; i < wjljList.length; i++) {
                String name = wjljList[i];
                name = name.trim();
                String suffix = wjljList[i].substring((wjljList[i]).lastIndexOf("."));
                System.err.println(suffix);
                String fileUrl = urlPath+"/"+name;
                //System.err.println(fileUrl);
                URL url = new URL(fileUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setReadTimeout(50000);
                conn.setConnectTimeout(50000);
                conn.setDoInput(true);
                conn.setRequestProperty("Charset","UTF-8");
                conn.setRequestProperty("User-Agent","Mozilla/4.0(compatible;MSIE 5.0;Windows NT;DigExt)");
                conn.setRequestProperty("Accept-Encoding","identity");
                lengh = conn.getContentLength();
                System.err.println(lengh);
                conn.connect();
                try{
                    int responseCode = conn.getResponseCode();
                    InputStream inputStream = null;
                    if(responseCode ==200){
                        inputStream = new BufferedInputStream(conn.getInputStream());
                    }else {
                        inputStream = new BufferedInputStream(conn.getErrorStream());

                    }
    
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    byte [] bytes = new byte[lengh];
                    int num = 0;
                    while((num = inputStream.read(bytes)) !=-1){
                        byteArrayOutputStream.write(bytes,0,num);
                    }
                    file = File.createTempFile("file",suffix);
                    if (file.exists()){
                        file.getParentFile().mkdirs();
                        file.createNewFile();
                    }
                    System.err.println(file.getName());
    
                    FileOutputStream fos =new FileOutputStream(file);
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fos);
                    bufferedOutputStream.write(byteArrayOutputStream.toByteArray());
                    fos.flush();
                }catch (Exception e){
                    e.printStackTrace();
                }
    
                FileInputStream fis = new FileInputStream(file);
                zipOutputStream.putNextEntry(new ZipEntry(StringUtil.getPrimaryKeyNo()+file.getName()+File.separator));
                int len = 0;
                byte[] buffer = new byte[1024];
                while ((len = fis.read(buffer)) > 0) {
                    zipOutputStream.write(buffer, 0, len);
                }
                zipOutputStream.flush();
                //关闭流
                zipOutputStream.closeEntry();
                fis.close();
            }
            zipOutputStream.close();
    
            InputStream ins = new FileInputStream(zipName);
            BufferedInputStream bis = new BufferedInputStream(ins);
            OutputStream outputStream = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(outputStream);
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(downloadName.getBytes("UTF-8"), "ISO-8859-1"));
            int bytesRead = 0;
            byte[] buff = new byte[8192];
            while ((bytesRead = bis.read(buff, 0, 8192)) != -1) {
                bos.write(buff, 0, bytesRead);
            }
            bos.flush();
            ins.close();
            bis.close();
            bos.close();
            outputStream.close();

}

**3.1使用FastDfsClient批量下载(此方式需下载到本地)

  @RequestMapping(value = "/downloadCgsDzdaPLFile")
    public void downloadCgsDzdaPLFile(HttpServletRequest  request, HttpServletResponse response) throws Exception {
String wjlj = request.getParameter("wjlj");//文件路径  group1/M00/00/58/RAy5W1_q1Z2AfL7_AAAoGMuKQCo260.pdf
    wjlj = wjlj.substring(0,wjlj.lastIndexOf(","));
    System.out.println(wjlj);
    String wjgs = request.getParameter("wjgs");//文件格式
    System.out.println(wjgs);
    String urlPath = request.getParameter("urlPath");
    List<String> wjljList = Arrays.asList(wjlj.split(","));
    File file = new File(rootPath);
    if (!file.exists()){
        file.mkdirs();        }
    try{
        File f = null;
        FileOutputStream fos =null;
        /*ZipOutputStream zos = null;*/
        List<String> fileList = new ArrayList<>();
        for (int i = 0; i <wjljList.size() ; i++) {
            String fileName =  wjljList.get(i);
            byte [] bytes = FastDFSClient.downloadFile(fileName.substring(0, 6), fileName.substring(7));
            f = new File(fileName);
            if (!f.exists()){
                f.getParentFile().mkdir();
                f.createNewFile();
            }
            fileList.add(fileName);
            fos = new FileOutputStream(f);
            fos.write(bytes,0,bytes.length);
        }
        zipFile(fileList,response);
        fos.flush();
        fos.close();
           }catch (Exception e){
        logger.error(FrameUtil.getLogInfo("business_error"), e);
        e.printStackTrace();
    }
}
public void zipFile(List<String> fileUrl,HttpServletResponse response){
    try {
        String downloadName = StringUtil.getPrimaryKeyNo() + ".zip";
        File zipName = new File(downloadName);
        if (!zipName.exists()) {
            zipName.createNewFile();
        }
        BufferedOutputStream bos = null;
        FileInputStream fis = null;
        ZipOutputStream out = null;

        bos = new BufferedOutputStream(response.getOutputStream());
        response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment;filename=" + new
                String(downloadName.getBytes("UTF-8"), "ISO-8859-1"));

        out = new ZipOutputStream(bos);
        for (int i = 0; i < fileUrl.size() ; i++) {
            File file = new File(fileUrl.get(i));
            if(!file.exists()){
                continue;
            }
            ZipEntry zipEntry = new ZipEntry(file.getName());
            out.putNextEntry(zipEntry);
            fis = new FileInputStream(file);
            byte [] buffer = new byte[1024];
            int read = 0;
            while ((read = fis.read(buffer))!=-1){
                out.write(buffer,0,read);
            }
        }
        out.close();
        bos.close();
    }catch (Exception e){
        e.printStackTrace();
    }
}

**3.2使用FastDfsClient批量下载(不需要存入本地直接通过输入流存入到压缩流)

 @RequestMapping(value = "/downloadCgsDzdaPLFile")
    public void downloadCgsDzdaPLFile(HttpServletRequest  request, HttpServletResponse response) throws Exception {
String wjlj = request.getParameter("wjlj");//文件路径  group1/M00/00/58/RAy5W1_q1Z2AfL7_AAAoGMuKQCo260.pdf
    wjlj = wjlj.substring(0,wjlj.lastIndexOf(","));
    System.out.println(wjlj);
    String wjgs = request.getParameter("wjgs");//文件格式
    System.out.println(wjgs);
    String urlPath = request.getParameter("urlPath");
    List<String> wjljList = Arrays.asList(wjlj.split(","));
    InputStream in = null;
    ZipOutputStream out = null;
    try{
     	response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment;filename=" + new
                String(downloadName.getBytes("UTF-8"), "ISO-8859-1"));
       	out = new ZipOutputStream(response.getOutputStream);
		 for (int i = 0; i <wjljList.size() ; i++) {
            String fileName =  wjljList.get(i);
            byte [] bytes = FastDFSClient.downloadFile(fileName.substring(0, 6), fileName.substring(7));
            in = new ByteArrayInputStream(bytes);
            ZipEntry zipEntry = new ZipEntry(file.getName());
            out.putNextEntry(new zipEntry(StringUtil.getPrimaryKeyNo()+fileName.substring(fileName.lastIndexOf(".")));
            byte [] byteStr = new byte[1024];
            while((n = in.read(byteStr) ! = -1)){
            	out.writer(byteStr,0,n);
        	}
        }catch (Exception e){
        logger.error(FrameUtil.getLogInfo("business_error"), e);
        e.printStackTrace();
    }
}

新手一枚,在成长的道路不断踩坑,如有不妥,欢迎指出!
生活不易,但也要长存理想!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王哈哈今日不举铁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值