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();
}
}
使用Apache http的get方法,分片下载文件到本地
于 2025-12-31 10:23:00 首次发布
2740

被折叠的 条评论
为什么被折叠?



