使用Apache http的get方法,分片下载文件到本地

    private boolean downloadRarFile(String objToken,String path,String title) {
        try {
            long i=0;    //这里必须是long,否则只能下载2GB。
            String response;
            CloseableHttpClient client = HttpClientBuilder.create().build();
            do {
                response = httpGet(client,"https://open.feishu.cn/open-apis/drive/v1/files/"+objToken+"/download",i*100*1024*1024+"",(i+1)*100*1024*1024-1+"",path+"/"+title);
                i++;
            }while(response.contains("206"));
        } catch (IOException e) {
            XxlJobHelper.log(e);
            return false;
        }
        return true;
    }

    private String httpGet(CloseableHttpClient client,String url,String start, String end,String pathfile) throws IOException {

        HttpGet httpPostRequest = new HttpGet(url);
        try {
            httpPostRequest.addHeader("Authorization", "Bearer " + getTenantAccessToken()); // 添加header
            httpPostRequest.addHeader("Content-Type", "application/json; charset=utf-8"); // 添加header
            httpPostRequest.addHeader("Range", "bytes=" + start + "-" + end); // 添加header
            CloseableHttpResponse response = client.execute(httpPostRequest);
            XxlJobHelper.log(response.getStatusLine().toString()); // 响应状态
            // 如果不是206和200就直接返回。
            if(!response.getStatusLine().toString().contains("206") && !response.getStatusLine().toString().contains("200")) {
                return response.getStatusLine().toString();
            }
            HttpEntity responseEntity = response.getEntity(); // 响应实体
            InputStream inputStream = responseEntity.getContent();
            byte[] b = new byte[8192];
            int len;
            try (OutputStream fos = new FileOutputStream("D:/"+pathfile, true)) {
                while ((len = inputStream.read(b)) != -1) {
                    fos.write(b, 0, len);
                }
                fos.flush();
            }
            inputStream.close();
            //		client.close();
            response.close();
            return response.getStatusLine().toString();
        } finally {
            httpPostRequest.releaseConnection();
        }

    }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值