Java:Download Restart -


i have java program download file through https connection.the program follows,

public class download extends observable implements runnable {     private static final int max_buffer_size = 1024;     public static final int downloading = 0;     public static final int paused = 1;      public static final int complete = 2;     public static final int cancelled = 3;     public static final int error = 4;      private url url; // download url         private static float size; // size of download in bytes      private int downloaded; // number of bytes downloaded        private int status; // current status of download     private string location;          public download(url url,string location){         this.url = url;         size=-1;         downloaded=0;         status=downloading;         this.location=location;         download();     }      public string geturl(){         return url.tostring();     }      public static float getsize(){         return size;     }      public float getprogress(){         return  ((float) downloaded / size) * 100;     }      public void pause(){         status = paused;         statechanged();     }      public void resume(){         status = downloading;         statechanged();         download();     }      public void cancel(){         status = cancelled;         statechanged();     }      public void error(){         status = error;         statechanged();     }      private string getfilename(url url){         string filepath = url.getfile();          int slashindex = filepath.lastindexof("/");         string filename = filepath.substring(slashindex+1,filepath.length());         string downloadpath = location+"/"+filename;         return downloadpath;      }      private void download(){         thread thread = new thread(this);         thread.start();     }      @override     public void run() {              randomaccessfile randomaccessfile = null;         inputstream inputstream = null;         int responsecode = 0;         try{             httpsurlconnection connection = getsslcertificate(geturl());             connection.setrequestmethod("get");             connection.setrequestproperty("range","bytes=" + downloaded + "-");             connection.connect();             responsecode=connection.getresponsecode();             if(responsecode/100 != 2){                 error();             }             int contentlength = connection.getcontentlength();             if(contentlength <1){                 error();             }             if(size == -1){                 size = contentlength;                 statechanged();             }             randomaccessfile = new randomaccessfile(getfilename(url),"rw");             randomaccessfile.seek(downloaded);             inputstream = connection.getinputstream();             while(status == downloading){                 //byte buffer[]=new byte[3000000];                 byte buffer[];                 float finalsize=size - downloaded;                 if ( finalsize > max_buffer_size) {                     buffer = new byte[(int) finalsize];                 } else {                     buffer = new byte[max_buffer_size];                 }                 int read = inputstream.read(buffer);                 if(read == -1)                     break;                  randomaccessfile.write(buffer, 0, read);                 downloaded += read;                 statechanged();             }         }catch (exception e) {             e.printstacktrace();         }finally{             try {                 randomaccessfile.close();             } catch (ioexception e) {                 e.printstacktrace();             }             try {                 inputstream.close();             } catch (ioexception e) {                 e.printstacktrace();             }          }      }      private void statechanged(){         setchanged();         notifyobservers();     }      private httpsurlconnection getsslcertificate(string urlpath) throws nosuchalgorithmexception, keymanagementexception, ioexception{         sslcontext ctx = sslcontext.getinstance("tls");         ctx.init(new keymanager[0],new trustmanager[]{new defaulttrustmanager()},new securerandom());         sslcontext.setdefault(ctx);          url url = new url(urlpath);         httpsurlconnection conn = (httpsurlconnection) url.openconnection();         conn.sethostnameverifier(new hostnameverifier() {              @override             public boolean verify(string arg0, sslsession arg1) {                 return true;             }         });          return conn;     }      public static void main(string[] args) throws malformedurlexception {         system.out.println("enter url , press enter:");         scanner scanner = new scanner(system.in);         string link = scanner.nextline();         system.out.println("enter destination location , press enter:");         string destination=scanner.nextline();         if(link.length() >0 && destination.length()>0){             download d = new download(new url(link),destination);             system.out.println("downloading... ");             d.run();             system.out.println("downloaded successfully" +" " +"size:"+(getsize()/1048576) + " mb");          }else{              system.out.println("error! please provide url , destination location");              system.exit(1);          }     } 

i need implement resume download,ie if there internet breakdown while downloading , again internet comes,the download needs starts point stops.

randomaccessfile = new randomaccessfile(getfilename(url),"rw"); 

there 1 problem found. think everytime creating new file. if file exists, donot need write line. make change first.

one more issue "downloaded" variable. in constructor, everytime initialized 0. need take last value stops. can below line.

downloaded = (int) randomaccessfile.length(); 

moreover, can check post's answers.

how resume interrupted download - part 2


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -