java file HTTP implementation principle

Source:  the Java HTTP The principle is very simple

 

Keywords:  RandomAccessFile

 

The principle:

  1) Download the off time, file recording position position breakpoint;

  2) continue the download time by RandomAccessFile position to find a position before the start the download

 

Actual operation:

  We create a file called "test.txt" in the root directory of the D drive, the file is very simple, as shown: 

 

 Yes, what we input is a simple six English letters. Then we right → Properties: 

  The effect we want to achieve is very simple: Write to the E drive in which the disk D "test.txt" file, but the way we will simulate a "break" behavior, then resume uploading, the final completion of the entire process.
  In other words, here we will put "D drive" viewed as a computer, and directly to the "E drive" treated as a single server. So we no longer even have something to do with the http protocol and a half dime, and (of course, the actual development we definitely still have something to do with it of ^ <^), which concerned only the most basic reading and writing files "off principle "and" renewal "is kind of how.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Test {
    private static int position = -1;

    public static void main(String[] args) {
        // 源文件与目标文件
        File sourceFile = new File("D:/", "test.txt");
        File targetFile = new File("E:/", "test.txt");
         // input and output streams 
        the FileInputStream FIS = null ; 
        a FileOutputStream fos = null ;
         // data buffer 
        byte [] = buf new new  byte [. 1 ]; 

        the try { 
            FIS = new new the FileInputStream (a sourceFile); 
            fos = new new a FileOutputStream (targetfile) ;
             // data read and write 
            the while (fis.read (buf) = -1! ) { 
                fos.write (buf); 
                // when the content of the document has been uploaded 3 bytes, the analog network interrupt, an exception is thrown 
                if(targetFile.length () ==. 3 ) { 
                    position =. 3 ;
                     the throw  new new FileAccessException (); 
                } 
            } 

        } the catch (FileAccessException E) { 
            keepGoing (a sourceFile, targetfile, position); 
        } the catch (a FileNotFoundException E) { 
            the System.out. the println ( "the specified file does not exist" ); 
        } the catch (IOException E) { 

        } the finally {
             the try {
                 // Close the input stream output 
                IF (FIS =!null)
                    fis.close();

                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void keepGoing(File source, File target, int position) {
        try {
            Thread.sleep(10000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            RandomAccessFile readFile = new RandomAccessFile(source, "rw");
            RandomAccessFile writeFile = new RandomAccessFile(target, "rw");

            readFile.seek(position);
            writeFile.seek(position);

            // 数据缓冲区
            byte[] buf = new byte[1];
            // 数据读写
            while (readFile.read(buf) != -1) {
                writeFile.write(buf);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

class FileAccessException extends Exception {
}

In summary, we have them in this change what has been done:

  1. First, we define a variable position, recorded at the time of interruption occurs, Position read and write. (This is to facilitate, in practical terms this value should definitely speak to a file or database for persistence)
  2. Then the file read and write while loop, we went to simulate an interrupt occurs behavior. Here when targetFile file length of 3 bytes is an analog thrown our custom. (We can imagine the actual download has been uploaded (download) the content of bytes of "x", this time to network outages, and then we will throw in a network outage exception "x" record).
  3. The rest is the same as if to say before we in the "Resume" behavior after the start, to wrap our files RandomAccessFile class, and then specify the location of the pointer before the interruption occurred to read and write to get through seek. (Actual download file upload, we certainly need to save the value of the interrupt uploaded to the server, this approach is usually httpConnection.setRequestProperty ( "RANGE", "bytes = x");)

In our code, on "Resume" behavior, that keepGoing method: We start thread to sleep for 10 seconds, which is running the program in order for us to see the effect.
Now we run the program, then the file will open the "Upload from the D drive to E drive process," we first opening the E drive, you will find really more of a test.txt file, open it found as follows:

 

 Yes, this time we find content that only "abc". This is less than we expected, because our program simulates a three-byte file upload time was interrupted.

Ok, we quietly wait 10 seconds have passed, and then point to open the file and see if you can successfully:

 

 We have found through Screenshot content has indeed become "abc", thus also completed resume.

 

 

Guess you like

Origin www.cnblogs.com/myseries/p/12554084.html