简体   繁体   English

Java中的多线程下载器

[英]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 例如,如果文件为100字节,请执行以下操作

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

thread-2 --> DownloadFile(" http://localhost/file.zip ", 50, 100, "output.zip"); 线程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%. 但是您知道会发生什么,只有几个字节没有下载,并且进度条卡在了99%上。 That's the problem!!! 那就是问题所在!!!

Why it gets stuck at 99%? 为什么会卡在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. RandomAccessFile不是线程安全的。

raf.seek(begin) fails, see the documentation of RandomAccessFile.seek() raf.seek(begin)失败,请参见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? 您确定并行下载速度更快吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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