url 断点续传

通过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 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值