/ *
** SitefileFetch.java
* /
Package Netfox;
Import java.io. *;
Import java.net. *;
Public class sitefilefetch extends thread {
SiteInfobean SiteInfobean = null; // File Information Bean
Long [] nStartPos; // Start location
Long [] nendpos; // end position
FILESPLITTERFETCH [] FILESPLITTERFETCH; / / Sub-threaded object
Long nfilelength; // file length
Boolean bfirst = true; // Whether to take a document for the first time
Boolean bstop = false; // stop sign
File TmpFile; // Temporary information downloaded
DataOutputStream output; // Output to the file output stream
/ / Responsible for the capture of the entire file, control internal thread (FileSplitterFetch class)
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;
Read_npos ();
}
Else
{
NStartPOS = New long [bean.getnsplitter ()];
Nendpos = new long [bean.getnsplitter ()];
}
}
Public void Run ()
{
// Get the file length
// Split file
// Example FileSplitterFetch
// Start FileSplitterFetch thread
// Waiting for the sub-thread to return
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 { NStartPOS [I] = (long) (i * (nfilelength / nstartpos.length); } For (int i = 0; I { Nendpos [i] = nStartPOS [i 1]; } Nendpos [NendPos.length-1] = NFileLength; } } // Introduction FileSplitterFetch = new filesplitterfetch [nstartpos.length]; For (int i = 0; i { FilesplitterFetch [I] = new filesplitterFetch (SiteInfobean.getssiteURL (), SiteInfobean.getsFilePath () file.separator siteInfobean.getsfilename (), NStartPOS [I], NendPos [i], i); Utility.log ("Thread" i ", NStartPOS =" NStartPOS [i] ", Nendpos =" NendPos [i]); FileSplitterFetch [i] .start (); } // filesplitterfetch [npos.length-1] = new filesplitterfetch (siteInfobean.getssiteURL (), SiteInfobean.getsfilepath () file.separator siteInfobean.getsfilename (), NPOS [NPOS.LENGTH-1], NFileLength, Npos.length-1); // utility.log ("thread" (npos.length-1) ", nStartPOS =" NPOS [NPOS.LENGTH-1] ", Nendpos = " nfilelength); // filesplitterfetch [npos.length-1] .Start (); // Waiting for the end of the sub-thread // INT count = 0; / / End WHILE loop Boolean Breakwhile = false; While (! BSTOP) { Write_npos (); Utility.sleep (500); Breakwhile = true; For (int i = 0; i { IF (! filesplitterfetch [i] .bdownover) { Breakwhile = false; Break; } } IF (BreakWhile) Break; // COUNT ; // if (count> 4) // SiteStop (); } System.err.println ("File Downloads!"); } Catch (Exception E) {E.PrintStackTrace (); } // Get the file length Public long getFilesize () { INT nfileLength = -1; Try { URL URL = New URL (SiteInfoBean.getssiteURL ()); HTTPURLCONNECTION httpConnection = (httpurlconnection) URL.OpenConnection (); HttpConnection.seueStProperty ("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; } } Else Break; } } Catch (IOException E) {E.PrintStackTrace (); Catch (Exception E) {E.PrintStackTrace (); Utility.log (nfileLength); Return nfileLength; } / / Save download information (file pointer position) Private void write_npos () { Try { Output = New DataOutputStream (New FileoutputStream (TMPFILE)); Output.writeint (NStartPos.Length); For (int i = 0; i { // output.writelong (NPOS [i]); Output.writelong (filesplitterfetch [i] .nstartpos); Output.writelong (filesplitterfetch [i] .nendpos); } Output.close (); } Catch (IOException E) {E.PrintStackTrace (); Catch (Exception E) {E.PrintStackTrace (); } // Read save 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 { NSTARTPOS [I] = INPUT.READLONG (); Nendpos [I] = INPUT.READLONG (); } INPUT.CLOSE (); } Catch (IOException E) {E.PrintStackTrace (); Catch (Exception E) {E.PrintStackTrace (); } Private Void ProcesserRrorcode (int NerrorCode) { System.err.Println ("Error Code:" NerrorCode); } // Stop file download Public void sitestop () { BSTOP = TRUE; For (int i = 0; i FILESPLITTERFETCH [I] .splitterstop (); } } / / Responsible for the capture of some files ** FileSplitterFetch.java * / Package Netfox; Import java.io. *; Import java.net. *; Public class filesplitterfetch eXtends thread { String surl; // file URL Long nStartPos; // file snippet start position Long Nendpos; // File Snippet End Position INT NTHREADID; // Thread's ID Boolean bdownover = false; // downing is over Boolean bstop = false; // stop identical FileAccessi FileAccessi = NULL; / / File Access Interface Public FileSplitterFetch (String Surl, String Sname, Long Nstart, Long Nend, INT ID) THROWS IOEXCEPTION { THIS.SURL = SURL; THIS.NSTARTPOS = NSTART; THIS.NENDPOS = NEND; nthreadid = ID; FileAccessi = New FileAccessi (SNAME, NSTARTPOS); // Positioning } Public void Run () { While (NStartPOS { Try { URL URL = New URL (SURL); HTTPURLCONNECTION httpConnection = (httpurlconnection) URL.OpenConnection (); HttpConnection.seueStProperty ("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 { NStartPOS = FileAccessi.write (B, 0, NREAD); // if (nthreadid == 1) // utility.log ("NStartPos =" NStartPOS ", Nendpos =" Nendpos); } Utility.log ("Thread" NTHREADID "Is over!"); BDOWNOVER = TRUE; // npos = fileaccessi.write (B, 0, NREAD); } Catch (Exception E) {E.PrintStackTrace ();}} } // Print the header information Public void logresponsehead (httpurlconnection con) { For (int i = 1 ;; i ) { String header = con.getHeaderfieldKey (i); IF (header! = null) //Responseheaders.put (HEADER ,HTTPCONNECTION.GetHeaderfield (HEADER)); Utility.log (Header ":" con.getHeaderfield (header); Else Break; } } Public void splitterstop () { BSTOP = TRUE; } } / * ** FileAccess.java * // File Access (positioning, write) Package Netfox; Import java.io. *; Public class fileaccessi imports serializable { // randomaccessFile class read / write bytes at a specific location of any file Randomaccessfile OsavedFile; Long NPOS; Public FileAccessi () THROWS IOEXCEPTION { THIS ("", 0); } Public FileAccessi (String Sname, long npos) throws oException { 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 (IOException E) { E.PrintStackTrace (); } Return n; } } / * ** SiteInfoBean.java * / Package Netfox; Public class siteInfobean { Private string ssiteurl; // site's url Private string sfilepath; // Saved File's Path Private string sfilename; // Saved File's Name Private int nsplitter; // count of splited downloading file Public siteInfobean () {// NSPLitter's default value is 5 // default value of nsplitter is 5 THIS (",", ",", 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 (String value) { SsiteURL = Value; } Public String getsfilepath () { Return sfilepath; } Public void setsfilepath (String Value) { 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 () { } // Thread sleep Public Static Void Sleep (Int Nsecond) { Try { Thread.sleep (NSECOND); } Catch (Exception E) { E.PrintStackTrace (); } } // log 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.exe Try { SiteInfobean 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 (); } }