Java Breakpoint Continuation function instance (Java Fetch remote file) _java

Source: Internet
Author: User

Copy Code code as follows:

Import Java.io.BufferedInputStream;
Import Java.io.DataOutputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;

Import java.io.IOException;

Import Java.io.RandomAccessFile;

Import java.net. * ;

/**
* File transfer client: Get remote Files
*/
public class Getremotefile_client_goon {public Getremotefile_client_goon ()
{
}

Private Boolean fileexist (String pathandfile)//Determine if the file has been downloaded but not downloaded
{
File File = new file (pathandfile);
if (File.exists ())
return true;
Else
return false;
}

Private Long FileSize (String pathandfile)//Determine the size of the file that has been downloaded
{
File File = new file (pathandfile);
return File.length ();
}
private void Filerename (String fname,string nname)//Rename the downloaded full file and remove the. TP Name
{
File File = new file (fName);
File.renameto (New File (Nname));
File.delete ();
}
public static void Main (string[] args)
{
URL url = null;
HttpURLConnection URLC = null;
DataOutputStream dos = null;
Bufferedinputstream bis = null;
FileOutputStream fos = null;
String LocalFile = "d:////x.x"; The location of the file and file name, the specific circumstances can be changed
String Localfile_bak = LocalFile + ". TP"; The file PLUS. tp extension was not downloaded to make the difference
Getremotefile_client_goon GCO = new Getremotefile_client_goon ();
Long fileSize = 0;
Long start = System.currenttimemillis ();
int len = 0;
byte [] bt = new byte [1024];
Byte[] Buffer=new byte[50*1024];
Randomaccessfile rafile = null;
Long totalsize = 0; Total size of files to download
Try
{
url = new URL ("Http://www.netbox.cn/download/nbsetup.EXE");
URLC = (httpurlconnection) url.openconnection ();
TotalSize = Long.parselong (Urlc.getheaderfield ("content-length"));
System.out.println ("Download file size is:" + totalsize);
Urlc.disconnect (); Disconnect first, and then connect, or the following will report the error that has been connected
URLC = (httpurlconnection) url.openconnection ();
Determine if a file exists
if (GCO. FileExist (Localfile_bak))//Use breakpoints to continue the transmission, here based on whether the download file is local. TP has file with the same name as the extension
{
SYSTEM.OUT.PRINTLN ("file continued) ... " );
FileSize = Gco. FileSize (Localfile_bak); Get files in small to determine where random writes are
System.out.println ("fileSize:" + fileSize);
Set User-agent
Urlc.setrequestproperty ("User-agent", "Netfox");
Set the starting position of a breakpoint continuation
Urlc.setrequestproperty ("RANGE", "bytes=" + fileSize + "-");
Urlc.setrequestproperty ("RANGE", "bytes=" +filesize); This can not be written, not less this "-".
Setting Up Acceptance information
Urlc.setrequestproperty ("Accept", "image/gif,image/x-xbitmap,application/msword,*/*");
Rafile = new Randomaccessfile (Localfile_bak, "RW"); Random Azimuth Reading
Rafile.seek (fileSize); Position pointer to filesize location
bis = new Bufferedinputstream (Urlc.getinputstream ());
while (len = Bis.read (BT)) > 0)//Loop Get file
{
Rafile.write (BT, 0, Len);
BUFFER=BUFFER+BT;
System.
}
SYSTEM.OUT.PRINTLN ("file continued to receive!" " );
}
else//Use original download
{
FOS = new FileOutputStream (Localfile_bak); Name the file's extension without downloading it. bak
DOS = new DataOutputStream (FOS);
bis = new Bufferedinputstream (Urlc.getinputstream ());
System.out.println ("Receiving files ...") " );
int test = 0;
while (len = Bis.read (BT)) > 0)//Loop Get file
{
Dos.write (BT, 0, Len);
Test + +;
if (test = = 50)//Here is the test, you can delete here, you can download the normal
break;
}
System.out.println ("File received properly!") ");
}
System.out.println ("Shared:" +
(System.currenttimemillis ()-start)/1000);
if (bis!= null)
Bis.close ();
if (Dos!= null)
Dos.close ();
if (FOS!= null)
Fos.close ();
if (rafile!= null)
Rafile.close ();
System.out.println ("Localfile_bak:" + GCO.) FileSize (Localfile_bak));
if (GCO. FileSize (localfile_bak) = = totalsize)//After downloading, rename the file
{
Gco. Filerename (Localfile_bak,localfile);
}
System.exit (0);
}
catch (Exception e)
{
Try
{
if (bis!= null)
Bis.close ();
if (Dos!= null)
Dos.close ();
if (FOS!= null)
Fos.close ();
if (rafile!= null)
Rafile.close ();
}
catch (IOException f)
{
F.printstacktrace ();
}
E.printstacktrace ();
}
System.exit (0);
}
}

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.