Java -- & gt; multi-thread resumable upload, java -- resumable upload

Source: Internet
Author: User

Java --> multi-thread resumable upload, java -- resumable upload

--> Added the resumable upload function based on multi-thread replication.

--> Test class

1 package com. dragon. java. multithreaddownload; 2 3 import java. io. file; 4 5/* 6 * multi-thread resumable upload (download) 7 */8 public class Test {9 public static void main (String [] args) {10 // @ SuppressWarnings ("resource") 11 // configure condition = new condition (System. in); 12 // System. out. println ("Enter the file path:"); 13 // write dead files and threads directly .. 14 File srcFile = new File ("F:/mp4/01_Java.mp4"); 15 // System. out. println ("Enter the number of threads:"); 16 // int n = s Canner. nextInt (); 17 int n = 4; 18 if (! SrcFile. exists () {19 System. out. println ("this file does not exist! "); 20} 21 22 File desFile = new File (srcFile. getParent (), "new" + srcFile. getName (); 23 24 // single-thread replication length: 25 long partLenghth = srcFile. length ()/n + 1; 26 for (int I = 1; I <n + 1; I ++) {27 // start thread 28 new MyThread (srcFile, desFile, partLenghth * (I-1), partLenghth29 * I ). start (); 30 31} 32} 33}

--> MyThread thread implementation class

1 package com. dragon. java. multithreaddownload; 2 3 import java. io. file; 4 import java. io. IOException; 5 import java. io. randomAccessFile; 6 7 public class MyThread extends Thread {8 private File srcFile; 9 private File desFile; 10 private long start; 11 private long end; 12 13 MyThread () {14 super (); 15} 16 17 MyThread (File srcFile, File desFile, long start, long end) {18 super (); 19 this. srcFile = srcF Ile; 20 this. desFile = desFile; 21 this. start = start; 22 this. end = end; 23} 24 25 @ Override26 public void run () {27 // File pointer 28 File configFile = new File (desFile. getParent (), Thread. currentThread () 29. getId () + ". config "); 30 if (! ConfigFile. exists () & desFile. exists () {31 32 System. out. println (Thread. currentThread (). getName () + "downloaded"); 33 return; 34} 35 // write the stream in try () you do not need to manually close the stream 36 try (RandomAccessFile rafSrc = new RandomAccessFile (srcFile, "r"); 37 RandomAccessFile rafDes = new RandomAccessFile (desFile, "rw "); 38 RandomAccessFile rafConfig = new RandomAccessFile (configFile, 39 "rw");) {40 41 rafConfig. setLength (8); 42 43 // When it is not the first download, pass the pointer in the configuration file to the starting pointer 44 long pointer = rafConfig. readLong (); 45 if (pointer! = 0) {46 start = pointer; 47} 48 rafSrc. seek (start); 49 rafDes. seek (start); 50 51 // display completion level System. out. println (Thread. currentThread (). getName () + "downloaded:" 52 + (float) pointer/srcFile. length () * 100 + "%"); 53 54 int len =-1; 55 byte [] buffer = new byte [32]; 56 while (len = rafSrc. read (buffer ))! =-1) {57 rafDes. write (buffer, 0, len); 58 pointer = rafSrc. getFilePointer (); 59 rafConfig. seek (0); 60 rafConfig. writeLong (pointer); 61 // end thread 62 if (rafSrc. getFilePointer ()> = end) {63 break; 64} 65} 66} catch (IOException e) {67 System. out. println (e); 68} 69 // Delete the configuration file 70 configFile. delete (); 71} 72}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.