Java breakpoint continuation function instance of of Java to get a remote file

  • 2020-04-01 02:38:46
  • OfStack


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 . * ; 
 
public class GetRemoteFile_Client_GoOn { public GetRemoteFile_Client_GoOn()
   {
   } 
  private boolean FileExist(String pathAndFile) //Determine if the file has been downloaded, but not completed
  {
     File file = new File(pathAndFile);
     if (file.exists())
       return true ;
     else 
      return false ;
   } 
  private long FileSize(String pathAndFile) //Determine the size of the downloaded file
  {
     File file = new File(pathAndFile);
     return file.length();
   }
   private void FileRename(String fName,String nName) //Change the name of the downloaded 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 " ; //  File saved in place and file name, specific circumstances can be changed  
    String localFile_bak = localFile + " .tp " ; //Do not download the file plus. Tp extension, in order to distinguish
    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 file size 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 connect later, otherwise an error will be reported
      urlc = (HttpURLConnection) url.openConnection();
       //Determines if the file exists
      if (gco.FileExist(localFile_bak)) //With breakpoint continuation, the basis here is to see whether the download file locally. Tp has an extension with the same name file
      {
         System.out.println( "  File transfer...  " );
         fileSize = gco.FileSize(localFile_bak); //Get the file in small in order to determine the random write location
        System.out.println( " fileSize: " + fileSize);
         //Set the user-agent
         // urlc.setRequestProperty("User-Agent","NetFox");
         //Sets the starting position of the breakpoint continuation
        urlc.setRequestProperty( " RANGE " , " bytes= " + fileSize + " - " );
         // urlc.setRequestProperty("RANGE", "bytes="+fileSize); //  I can't write it like this. I can't write it without this "-".
         //Set accept message
        urlc.setRequestProperty( " Accept " , " image/gif,image/x-xbitmap,application/msword,*/* " );        
        raFile = new RandomAccessFile(localFile_bak, " rw " ); //Random azimuth reading
        raFile.seek(fileSize); //Locate the pointer to the fileSize position
        bis = new BufferedInputStream(urlc.getInputStream());
         while ((len = bis.read(bt)) > 0 ) //Loop fetch file
        {
           raFile.write(bt, 0 , len);
           // buffer=buffer+bt;
           // System. 
        }
         System.out.println( "  File continuation received!  " );        
      }
       else //Use original download
      {
         fos = new FileOutputStream(localFile_bak); //Name the file extension.bak without downloading it
        dos = new DataOutputStream(fos);
         bis = new BufferedInputStream(urlc.getInputStream());        
        System.out.println( "  Receiving files...  " );
         int test = 0 ;
         while ((len = bis.read(bt)) > 0 ) //Loop fetch 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 as normal!" );
      }      
      System.out.println( "  Share:  " + 
                         (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 );
   }
 } 


Related articles: