Java implementation of file breakpoint continuation

From , 2 Years ago, written in Java, viewed 176 times.
URL https://pastebin.vip/view/10fa5eb8
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.RandomAccessFile;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6.  
  7. public class DownloadFile {
  8.  
  9.     /**
  10.      * @param args
  11.      */
  12.     static int len;//线程平均下载文件长度
  13.     static int bn ;//每个线程写入文件的字节数
  14.     static int tn; //线程数
  15.     static String urlt;//下载地址
  16.     static String fileName;
  17.     static RandomAccessFile osf; //文件操作
  18.     public static void main(String[] args) {
  19.         // TODO Auto-generated method stub
  20.          
  21.         try {
  22.             urlt = "http://192.168.16.77:8080/Stargreat/html/baidu player";
  23.             fileName = "C:\" + urlt.split("//")[1].split("/")[urlt.split("//")[1].split("/").length-1];
  24.             System.out.println(fileName);
  25.             URL url = new URL(urlt);
  26.             HttpURLConnection http = (HttpURLConnection) url.openConnection();
  27.  
  28.             /**
  29.              * 此处设定5个线程下载一个文件tn = 5;
  30.              * 判断平均每个线程需下载文件长度:
  31.              */
  32.             System.out.println("file size:" + http.getContentLength());
  33.             tn = 3;        
  34.             len = http.getContentLength() / tn ;//舍去余数(余数自动舍去)计算每个线程应下载平均长度,最后一个线程再加上余数,则是整个文件的长度,
  35.             File f = new File(fileName);
  36.             if (f.exists()){
  37.                 f.delete();
  38.                 osf = new RandomAccessFile(f, "rw");
  39.                 osf.seek(http.getContentLength()-1);
  40.                 osf.write(0);
  41.             }else{
  42.                 osf = new RandomAccessFile(f, "rw");
  43.                 osf.seek(http.getContentLength()-1);
  44.                 osf.write(0);
  45.             }
  46.             System.out.println("temp 文件长度:" + f.length());
  47.             Thread t;//下载子线程,
  48.             for (int j = 0; j < tn; j++) {
  49.                 if(j == tn - 1){//如果最后一个线程则加上余数长度字节
  50.                     bn = len + (http.getContentLength() % tn);
  51.                 }else{
  52.                     bn = len;
  53.                 }
  54.                 System.out.println("t"+ j + "线程下载长度:" + bn + "起始字节:" + len*j);
  55.                 t = new DT(
  56.                         j,
  57.                         urlt,
  58.                         fileName,
  59.                         len*j,
  60.                         bn
  61.                          
  62.                 );
  63.                 t.start();
  64.             }
  65.  
  66.         } catch (IOException e) {
  67.             // TODO Auto-generated catch block
  68.             e.printStackTrace();
  69.         }
  70.     }
  71.  
  72. }
  73.  
  74.  
  75. //java/6863

Reply to "Java implementation of file breakpoint continuation"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website