通过url下载文件,中间服务器可能中断,需要断点续传
private void copyUrlPartly( URL url) throws IOException {
List<PartETag> partETags = new ArrayList<PartETag>();
long totalUplod = 0L;
int index = 1;
long remain = fileSize;
int retry = 0;
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
long fileSize = httpURLConnection.getContentLengthLong();
/*如果下载的size小于size那么,就重新请求连接*/
while (totalUplod < fileSize && retry < 5) {
byte readBuffer[] = new byte[1024];
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("RANGE", "bytes="+totalUplod+"-");
httpURLConnection.connect();
InputStream stream = httpURLConnection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(stream);
if(remain <= 0) {
break; }
while (remain > 0) {
long partSize = 0L;
File tmpFile = null;
try {
/* 使用临时文件作为中转 */
tmpFile = File.createTempFile( "test", null);
/* 将part拷贝到临时文件夹下面的临时文件 */
partSize = downloadPart(bis, readBuffer, tmpFile);
// 在这可以对tmpFile处理你 省略不写
remain -= partSize;
totalUplod += partSize;
retry = 0;
} catch (Exception e) {
retry++;
logger.error("copyUrlPartly failed. ", e);
break;
} finally {
if (null != tmpFile) {
tmpFile.delete();
}
}
}
}
}
//将网上下载部分保存到临时文件tmpFile中
private long downloadPart(BufferedInputStream bis, byte[] buffer, File tmpFile) throws Exception {
BufferedOutputStream bos = null;
long total = 0;
try {
bos = new BufferedOutputStream(new FileOutputStream(tmpFile));
int count;
while ((count = bis.read(buffer)) > 0) {
total += count;
bos.write(buffer, 0, count);
}
} finally {
try {
if (null != bos) {
bos.close();
}
} catch (Exception e) {
logger.debug("IoCloseException", e);
}
}
return total;
}
Range头域
Range头域可以请求实体的一个或者多个子范围。例如,
表示头500个字节:bytes=0-499
表示第二个500字节:bytes=500-999
表示最后500个字节:bytes=-500
表示500字节以后的范围:bytes=500-
第一个和最后一个字节:bytes=0-0,-1
同时指定几个范围:bytes=500-600,601-999
转载请注明原文地址: https://ju.6miu.com/read-664869.html