zoukankan      html  css  js  c++  java
  • Java实现断点下载Demo

     1 //1、声明URL
     2         String path="http://localhost:8080/day22_DownLoad/file/a.rmvb";
     3         URL url=new URL(path);
     4         //2、设置已下载文件
     5         String savePath="d:\a.rmvb";
     6         File file=new File(savePath);
     7         long size=file.length();//文件当前大小,刚开始时返回0
     8         System.out.println(size);
     9         //3、设置连接
    10         HttpURLConnection conn=  (HttpURLConnection) url.openConnection();
    11         //4、设置访问类型
    12         conn.setRequestMethod("GET");
    13         //5、设置下载区间
    14         conn.setRequestProperty("range","bytes="+size+"-");
    15         conn.connect();
    16         //6、状态码
    17         int code=conn.getResponseCode();//断点是206
    18         if(code==206)
    19         {
    20             InputStream in=conn.getInputStream();
    21             int serviceSize=conn.getContentLength();
    22             //必须使用
    23             RandomAccessFile out=new RandomAccessFile(file, "rw");
    24             //从size字节开始写
    25             out.seek(size);
    26             byte[] b=new byte[1024];
    27             int len=-1;
    28             while((len=in.read(b))!=-1)
    29             {
    30                 out.write(b,0,len);
    31             }
    32             out.close();
    33         }
  • 相关阅读:
    CF981D
    CF883H
    Hdu 5884
    全排列
    二叉搜索树实现
    my.ini配置详解
    主元素问题
    排序算法(还需补充)
    迷宫问题(DFS,BFS)
    算法导论4--求最大和数组
  • 原文地址:https://www.cnblogs.com/liuwt365/p/4158230.html
Copyright © 2011-2022 走看看