Java get download progress

I'm writing Java applications (using NetBeans as IDE and JFrame forms), part of which is downloading files How do I update the progress bar with the current download progress, or at least get the total number of bytes currently downloaded in another thread?

The following is part of my code:

Runnable updatethread = new Runnable() {
        public void run() {
            try {
                java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.net.URL("server/package.zip").openStream());
                java.io.FileOutputStream fos = new java.io.FileOutputStream("package.zip");
                java.io.bufferedoutputstream bout = new bufferedoutputstream(fos,1024);
                byte[] data = new byte[1024];
                int x=0;
                while((x=in.read(data,1024))>=0)
                {
                    bout.write(data,x);
                }
                bout.close();
                in.close();
            }
            catch(FileNotFoundException e) { } catch (IOException e) { }
        }
    };
new Thread(updatethread).

start();
try {
    updatethread.wait();

} catch (InterruptedException ex) { }

Solution

A working example that uses your code and displays progress in the progress bar might look like this:

import java.io.bufferedoutputstream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class Progressbar {

    public static void main(String[] args) {

        final JProgressBar jProgressBar = new JProgressBar();
        jProgressBar.setMaximum(100000);
        JFrame frame = new JFrame();
        frame.setContentPane(jProgressBar);
        frame.setDefaultCloSEOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(300,70);
        frame.setVisible(true);

        Runnable updatethread = new Runnable() {
            public void run() {
                try {

                    URL url = new URL("http://downloads.sourceforge.net/project/bitcoin/Bitcoin/blockchain/bitcoin_blockchain_170000.zip");
                    HttpURLConnection httpconnection = (HttpURLConnection) (url.openConnection());
                    long completeFileSize = httpconnection.getContentLength();

                    java.io.BufferedInputStream in = new java.io.BufferedInputStream(httpconnection.getInputStream());
                    java.io.FileOutputStream fos = new java.io.FileOutputStream(
                            "package.zip");
                    java.io.bufferedoutputstream bout = new bufferedoutputstream(
                            fos,1024);
                    byte[] data = new byte[1024];
                    long downloadedFileSize = 0;
                    int x = 0;
                    while ((x = in.read(data,1024)) >= 0) {
                        downloadedFileSize += x;

                        // calculate progress
                        final int currentProgress = (int) ((((double)downloadedFileSize) / ((double)completeFileSize)) * 100000d);

                        // update progress bar
                        SwingUtilities.@R_403_842@(new Runnable() {

                            @Override
                            public void run() {
                                jProgressBar.setValue(currentProgress);
                            }
                        });

                        bout.write(data,x);
                    }
                    bout.close();
                    in.close();
                } catch (FileNotFoundException e) {
                } catch (IOException e) {
                }
            }
        };
        new Thread(updatethread).

        start();
    }

}

This shows how to do it - you should consider GUI threads, etc. before using it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>