简体   繁体   中英

multi threaded downloader in java

OK I am using the following function to create multiple threads to download a file. You can see the functions takes link, starting byte, ending byte and the path to download the file as argument. I call this function 2 times to create two threads to download the required file.

For example, if the file is of 100 bytes I do the following

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

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

But you know what happens, only a few bytes don't get downloaded and my progress bar gets stuck at 99%. That's the problem!!!

Why it gets stuck at 99%? In words why some bytes are being lost? I could see the total number of bytes in the downloaded variable.

Here is the 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 is not thread safe.

raf.seek(begin) fails, see the documentation of RandomAccessFile.seek()

Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.

You may download parts of file into separate files then merge them.

Are you sure that parallel downloads are faster?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM