下载单个文件和压缩包下载demo对比

下载单个文件和压缩包下载demo对比

下载压缩包:

 public String downZip(List<String> fileUrls, HttpServletResponse response) {
        logger.info("批量下载的地址有:{}",JSON.toJSONString(fileUrls));
        ZipOutputStream zipStream = null;
        response.setContentType("application/force-download");
        response.addHeader("Content-Disposition","attachment;filename=file.zip");
        OutputStream os = null;
        InputStream fis = null;
        HttpURLConnection conn = null;
        try{
            os = response.getOutputStream();
            zipStream = new ZipOutputStream(os);

            for(String filePath : fileUrls){

                URL path = new URL(filePath);
                conn = (HttpURLConnection) path.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5 * 1000);
                fis = conn.getInputStream();// 通过输入流获取数据

                ZipEntry zipEntry = new ZipEntry(filePath.substring(filePath.lastIndexOf("/")));
                zipStream.putNextEntry(zipEntry);
                int read = 0;
                byte[] buf = new byte[1024 * 10];
                while((read = fis.read(buf)) != -1)
                {
                    zipStream.write(buf, 0, read);
                }

            }

        }catch (Exception e){
            logger.info("批量下载失败,异常:{}",e.getMessage());
            e.printStackTrace();
        }finally {
            streamClose(zipStream);
            streamClose(bufferedInputStream);
            streamClose(os);
            streamClose(fis);
        }
        return null;
    }


    private void streamClose(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

下载单个:

 HttpURLConnection conn = null;
        try {
            File file = new File(filePath);
            // 取得文件的后缀名。
            String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1).toLowerCase();
            StringBuffer buffername = new StringBuffer(filePath.substring(filePath.lastIndexOf("/")+1));

            String filename = buffername.toString();

            URL path = new URL(filePath);
            conn = (HttpURLConnection) path.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            InputStream fis = conn.getInputStream();// 通过输入流获取数据

            byte[] buffer = readInputStream(fis);
            if (null != buffer && buffer.length > 0) {
                // 清空response
                response.reset();
                // 设置response的Header
                response.addHeader("Content-Disposition", "attachment;filename=" + new String((filename).getBytes("GBK"), "ISO8859_1"));
                response.addHeader("Content-Length", "" + buffer.length);
                OutputStream toClient = response.getOutputStream();
                response.setContentType("application/octet-stream");
                toClient.write(buffer);
                toClient.flush();
                toClient.close();
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        return null;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值