Java -- & gt; implements resumable upload (download) and java -- resumable upload

Source: Internet
Author: User

Java --> implements resumable upload (download), java -- resumable upload

--> Resumable upload: stops or closes the program just like thunder downloads a file. The next download starts from the last download, rather than starting with another download...

--> RandomAccessFile --> pointer (File pointer) --> seek (move file pointer) --> resumable upload

Package com. dragon. java. downloadfile; import java. io. file; import java. io. IOException; import java. io. randomAccessFile;/* resumable upload: This is very important for downloading software! --> The first download of 100 bytes --> the second download of 101 bytes... find a way to know where the last download was broken. The location where the last download has been made. Write down the breakpoint location ------> A third-party file is required to remember the breakpoint location */public class Test {public static void main (String args []) {File srcFile = new File ("D:/Java4Android/01_Java archaeology/01_java .mp4"); File desDir = new File ("f:/vidio"); copyFileToDir (srcFile, desDir);} public static void copyFileToDir (File srcFile, File desDir) {desDir. mkdirs (); // create the configuration File configFile = new File (desDir, srcFile. getName (). split ("\\. ") [0] + ". c Onfig "); // create the target File desFile = new File (desDir, srcFile. getName (); if (! ConfigFile. exists () & desFile. exists () {System. out. println ("this file has been downloaded! "); Return;} RandomAccessFile rafSrc = null; RandomAccessFile rafDes = null; RandomAccessFile rafConfig = null; try {rafSrc = new RandomAccessFile (srcFile," r "); rafDes = new RandomAccessFile (desFile, "rw"); rafConfig = new RandomAccessFile (configFile, "rw"); // set the target file to be as long as the source file. setLength (srcFile. length (); // set the configuration file length to 8 to prevent the first download from an EOF exception rafConfig. setLength (8); // download continues from the last download location! Long pointer = rafConfig. readLong (); System. out. println ("downloaded:" + (float) pointer/srcFile. length () * 100 + "%"); rafSrc. seek (pointer); rafDes. seek (pointer); // set a small point for the length of a single transmission, so that you can check whether the resumable data transfer byte [] buffer = new byte [32]; int len =-1; // at the beginning of each copy, the pointer of the source file and the pointer of the target file must be read from the last disconnected position while (len = rafSrc. read (buffer ))! =-1) {rafDes. write (buffer, 0, len); // when writing the configuration file, every time the file pointer is moved to the original position --> this way, the first eight bytes of rafConfig will be stored forever. seek (0); // The sum of each copy. record the position of the file pointer for resumable data transfer. RafConfig. writeLong (rafSrc. getFilePointer ();} catch (IOException e) {System. out. println (e);} finally {try {rafSrc. close (); rafDes. close (); rafConfig. close ();} catch (IOException e) {System. out. println (e);} // Delete the configuration file System after the stream is closed. out. println ("Download successful! "); ConfigFile. delete ();}}}

--> Simulate simple resumable upload through replication...

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.