multithreading - multi threaded downloader in java -


ok using following function create multiple threads download file. can see functions takes link, starting byte, ending byte , path download file argument. call function 2 times create 2 threads download required file.

for example, if file of 100 bytes following

thread-1 --> downloadfile("http://localhost/file.zip", 0, 50, "output.zip");

thread-2 --> downloadfile("http://localhost/file.zip", 50, 100, "output.zip");

but know happens, few bytes don't downloaded , progress bar gets stuck @ 99%. that's problem!!!

why gets stuck @ 99%? in words why bytes being lost? see total number of bytes in downloaded variable.

here function

public void downloadfile(final string link, final long start,final long end, final string path){     new thread(new runnable(){         public void run(){             try {                 url url = new url(link);                 httpurlconnection conn = (httpurlconnection) url.openconnection();                 conn.setrequestproperty("range", "bytes="+start+"-"+end);                 bufferedinputstream bis = new bufferedinputstream(conn.getinputstream());                 randomaccessfile raf = new randomaccessfile(path,"rw");                 raf.seek(start);                  int i=0;                 byte bytes[] = new byte[1024];                   while((i = bis.read(bytes))!=-1){                     raf.write(bytes, 0, i);                     downloaded = downloaded+i;                      int perc = (int) ((downloaded*100)/filesize);                     progress.setvalue(perc);                     percentlabel.settext(long.tostring(downloaded)+" out of "+filesize);                 }                   if(filesize==downloaded){                     progress.setvalue(100);                     joptionpane.showmessagedialog(null, "download success! ");                     progress.setvalue(0);                     downloaded=0;                     downbtn.settext("download");                 }                 bis.close();                 raf.close();               } catch (malformedurlexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (ioexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }          }     }).start(); } 

thanks in anticipation.

randomaccessfile not thread safe.

raf.seek(begin) fails, see documentation of randomaccessfile.seek()

sets file-pointer offset, measured beginning of file, @ next read or write occurs. offset may set beyond end of file. setting offset beyond end of file not change file length. file length change writing after offset has been set beyond end of file.

you may download parts of file separate files merge them.

are sure parallel downloads faster?


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -