Renewal (HTTP) content: (1) The principle of breakpoint renewal (II) Java renewed the key point of the breakpoint resumed (3) Renewal of the renewal of the burst (1) Renewal The principle of biography is that the principle of complex resume is very simple, which is different from the request and general downloads in HTTP. When you make a bit, the request is as follows: assuming that the server domain is www.sjtu.edu.cn, the file name is Down.zip. Get /down.zip http / 1.1accept: image / gif, image / x-xbitmap, image / jpeg, image / pjpeg, application / vnd.ms-excel, application / msword, Application / VND.MS-PowerPoint, * / * Accept-language: zh-cnaccept-encoding: Gzip, deflateUser-agent: mozilla / 4.0 (compatible; msie 5.01; windows nt 5.0) Connection: Keep-Alive
After receiving the request, the server is required to find the requested file, extract the file information, and then return to the browser, return information as follows:
200Content-Length = 106786028Accept-Ranges = bytesDate = Mon, 30 Apr 2001 12:56:11 GMTETag = W / "02ca57e173c11: 95b" Content-Type = application / octet-streamServer = Microsoft-IIS / 5.0Last-Modified = Mon, 30 apr 2001 12:56:11 GMT
The so-called breakpoint continues to continue downloading from the place where the file has been downloaded. So add more information when the client browser is passed to the web server - where to start. Below is a "browser" compiled by yourself to pass the request information to the web server, requiring starting from 200,00070 bytes. Get /down.zip http / 1.0User-agent: Netfoxrange: Bytes = 2000070-Accept: Text / HTML, Image / GIF, Image / JPEG, *; Q = .2, * / *; q=.2
Take a closer look, you will find more than a row: bytes = 2000070 - The meaning of this is to tell the server down.zip This file starts from 200,000 bytes, and the previous byte is not available. After receiving this request, the return information is as follows: 206Content-length = 106786028content-range = bytes 2000070-106786027 / 106786028Date = mon, 30 apr 2001 12:55:20 gmtetag = W / "02ca57e173c11: 95b" content-type = Application / OCTET-Streamserver = Microsoft-IIS / 5.0last-Modified = Mon, 30 APR 2001 12:55:20 GMT
Comparison with the information returned by the previous server, it will find a line: Content-Range = bytes 2000070-106786027 / 106786028 The return code is also changed to 206, and no longer 200.
If you know the above principles, you can make a programming of the breakpoint.
(2) Java renewed key points
(1) What method is used to implement the submission: BYTES = 2000070-. Of course, the most primitive socket is certain, but it is too much, in fact, this function is provided in the Java NET package. The code is as follows: URL URL = New URL ("http://www.sjtu.edu.cn/down.zip"); httpurlconnection httpConnection = (httpurlconnection) URL.OpenConnection ();
// Set user-agentproperty ("user-agent", "netfox"); // Set the start position of the breakpoint HTTPConnection.seuestProperty ("Range", "Bytes = 2000070); // Get the input stream InputStream INPUT = httpConnection.getinputStream ();
The byte stream taken from the input stream is the byte stream from the Down.zip file from 2000070. Let's see that it is still very simple to achieve Java. The next thing to do is how to save the resulting stream to the file.
The method of saving the file. I use the RandaccessFile class in the IO package. The operation is quite simple, assuming that the file is saved from 2000070, the code is as follows: randomaccess osavedfile = new randomaccessfile ("Down.zip", "RW"); long npos = 2000070; // Location file pointer to NPOS location OsavedFile.seek (NPOS ); byte [] b = new byte [1024]; int nread; // read the byte stream from the input stream, then Write While in the file ((NREAD = INPUT.READ (B, 0, 1024))> 0) {OSAVEDFILE.WRITE (B, 0, NREAD);
How is it, it is also very simple. The next thing to do is to integrate into a complete program. Includes a series of thread control, and the like.
(3) The implementation of the breakpoint renewal kernel has maintained 6 classes, including a test class. SiteFileFetch.java is responsible for the capture of the entire file, controlling internal threads (FILESPLITTERFETCH). FILESPLITTERFETCH.JAVA is responsible for the capture of some files. FileAccess.java is responsible for the storage of documents. SiteInfoBean.java to grab the information, such as the directory, name, and the URL of the file, and the URL of the file. Utility.java tool class, put some simple way. TestMethod.java test class.
Below is the source program:
/***Sitefilefetch.java*/package netfox; import java.io. *; Import java.net. *;
Public class sitefilefetch extends thread {
SiteInfobean SiteInfobean = null; // File Information Beanlong [] nStartPos; // Start position long [] Nendpos; // End position filesplitterFetch; // Subload object long nfilelength; // file length Boolean bfirst = true; / / Whether to take the file boolean bstop = false; // stop sign file tmpfile; // file download temporary information DataOutputStream output; // Output to the file output stream
public SiteFileFetch (SiteInfoBean bean) throws IOException {siteInfoBean = bean; // tmpFile = File.createTempFile ( "zhong", "1111", new File (bean.getSFilePath ())); tmpFile = new File (bean.getSFilePath () File.separator bean.getsFileName () ". Info"); if (tmpfile.exists ()) {bfirst = false;} else {nStartPos = new long [bean.getnsplitter ()]; Nendpos = New long [bean.getnsplitter ()];}}
Public void Run () {// Get file length // Split file // Instance fileplitterFetch // Start FileSplitterFetch thread // Wait for sub-thread Returns Try {if (bfirst) {nfilelength = getFileSize (); if (nfilelength == -1 ) {System.err.Println ("File Length Is Not Known!");} Else if (nfileLength == -2) {system.err.println ("File Is Not Access!");} Else {for (int i = 0; i // promoter thread fileSplitterFetch = new FileSplitterFetch [nStartPos.length]; for (int i = 0; i // get file length public long getFileSize () {int nFileLength = -1; try {URL url = new URL (siteInfoBean.getSSiteURL ()); HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection (); httpConnection.setRequestProperty ( "User -Agent "," Netfox "); Int responsecode = httpConnection.getResponsecode (); if (responsecode> = 400) {processerrorcode (responsecode); return -2; // - 2 represent access is error} string sheader; for (int i = 1 ;; i ) {// DataInputStream in = new DataInputStream (httpConnection.getInputStream ()); // Utility.log (in.readLine ()); sHeader = httpConnection.getHeaderFieldKey (i); if ( ! sHeader = null) {if (sHeader.equals ( "Content-Length")) {nFileLength = Integer.parseInt (httpConnection.getHeaderField (sHeader)); break;}} elsebreak;}} catch (IOException e) {e. PRINTSTACKTRACE ();} catch (exception e) {E.PrintStackTrace (); Utility.log (nfileLength); Return nfileLength;} // Save download information (file pointer location) private void write_npos () {Try {Output = New DataOutputStream (New FileoutputStream (TMPFILE)); Output.writeInt (NStartPos.Length); for (int i = 0; I // read the saved download information (file pointer position) private void read_nPos () {try {DataInputStream input = new DataInputStream (new FileInputStream (tmpFile)); int nCount = input.readInt (); nStartPos = new long [nCount] Nendpos = new long [ncount]; for (int i = 0; i Private void processerrorcode (int Nerror) {system.err.println ("Error Code:" NerrorCode); // Stop file download public void suit () {bstop = true; for (int i = 0; i Import java.io. *; import java.net. *; Public class filesplitterfetch eXtends thread { String sURL; // File URLlong nStartPos; // File Snippet Start Positionlong nEndPos; // File Snippet End Positionint nThreadID; // Thread's IDboolean bDownOver = false; // Downing is overboolean bStop = false; // Stop identicalFileAccessI fileAccessI = null; // File Access Interface public FileSplitterFetch (String sURL, String sName, long nStart, long nEnd, int id) throws IOException {this.sURL = sURL; this.nEndPos = nEnd;; nThreadID = id; this.nStartPos = nStart fileAccessI = new FileAccessI (sName, NStartPOS); Public void run () {while (NStartPOS try {URL url = new URL (sURL); HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection (); httpConnection.setRequestProperty ( "User-Agent", "NetFox"); String sProperty = "bytes =" nStartPos "-" ; httpConnection.SetRequestProperty ("Range", Sproperty; Utility.log (Sproperty); InputStream INPUT = httpConnection.getinputStream (); // logresponsehead (httpConnection); Byte [] b = new byte [1024]; int Nread; while ((NREAD = INPUT.READ (B, 0, 1024))> 0 && NStartPOS Public void splitterstop () {bstop = true; } /***FileAccess.java*/package netfox; import java.io. *; Public class fileaccessi imports serializable { Randomaccessfile OsavedFile; long npos; Public FileAccessi () throws oException {this ("", 0); Public FileAccessi (String Sname, Long Npos) throws ioException {osavedfile = new randomaccessfile (SNAME, "RW"); this.npos = npos; osavedfile.seek (npos);} Public synchronized int Write (byte [] b, int nstart, int Nlen) {int n = -1; try {osavedfile.write (b, nStart, nlen); n = nlen;} catch (ooException e) {E.PrintStackTrace (); Return n; } /***SiteInfobean.java*/package netfox; Public class siteInfobean { Private string ssiteurl; // site's urlprivate string sfilepath; // saved file's pathprivate string sfilename; // saved file's nameprivate int nsplitter; // count of splited downloading file Public siteInfobean () {// default value of nsplitter is 5th (",", ",", 5);} Public siteInfobean (String SURL, String Spath, String Sname, Int nspiltter) {ssiteURL = surl; sfilepath = spath; sfilename = sname; this.nsplitter = nspiltter; } Public string getssiteURL () {return ssiteURL; Public void setssiteurl (SSITEURL) {ssiteURL = value; Public string getsfilepath () {return sfilepath;} public void setsfilepath (SFILEPATH = VALUE) Public string getsfilename () {return sfilename;} Public void setsfilename (string value) {sfilename = value; Public int getnsplitter () {return nsplitter;} Public void setnsplitter (int ncount) {nsplitter = ncount;}} /***Utility.java*/package netfox; Public class utility { Public utility () { } Public Static Void Sleep (INT NSECOND) {Try {thread.sleep (NSECOND);} catch (exception e) {E.PrintStackTrace ();}} Public static void log (string smsg) {system.err.println (SMSG); Public Static Void log (int smsg) {system.err.println (smsg);}} /***Testmethod.java*/package netfox; Public class testmethod { Public TestMethod () {///xx/weblogic60b2_win.exetry ssffobean bean = new siteInfobean ("http://localhost/xx/weblogic60b2_win.exe", "l: // temp", "WebLogic60B2_Win.exe", 5) // siteInfobean bean = new siteInfobean ("http: // localhost: 8080 / down.zip", "l: // Temp", "WebLogic60B2_WIN.EXE", 5); SiteFileFetch filefetch = new sitefileFetch (bean); filefetch .start ();} catch (exception e) {E.PrintStackTrace (); } Public static void main (String [] args) {new testmethod ();}}