使用java实现在下载文件的过程中显示进度条,简单例子

package com.file;

import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;


// 使用java实现在下载文件的过程中显示进度条
public class TestFile {

    public static class ProgressBarThread implements Runnable {
        private ArrayList<Integer> proList = new ArrayList<Integer>();
        private int progress; //当前进度
        private int totalSize; //总大小
        private boolean run = true;

        public ProgressBarThread(int totalSize) {
            this.totalSize = totalSize;
            //TODO 创建进度条
        }

        /**
         * @param progress 进度
         */
        public void updateProgress(int progress) {
            synchronized (this.proList) {
                if (this.run) {
                    this.proList.add( progress );
                    this.proList.notify();
                }
            }
        }

        public void finish() {
            this.run = false;
            //关闭进度条
        }

        @Override
        public void run() {
            synchronized (this.proList) {
                try {
                    while (this.run) {
                        if (this.proList.size() == 0) {
                            this.proList.wait();
                        }
                        synchronized (proList) {
                            this.progress += this.proList.remove( 0 );
                            //TODO 更新进度条
                            DecimalFormat decimalFormat = new DecimalFormat( "0.00" );
                            System.err.println( "当前进度:" + decimalFormat.format( this.progress / (float) this.totalSize * 100 ) + "%" );
                        }
                    }

                    System.out.println( "下载完成" );
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

  

测试代码

    // 测试
    public static void main(String[] args) {
        try {
            File file = new File( "/Users/zhenning/Documents/tools/mysql51.docset.zip" );
            FileInputStream fis = new FileInputStream( file );
            FileOutputStream fos = new FileOutputStream( "/Users/zhenning/Desktop/mysql51.docset.zip" );
            ProgressBarThread pbt = new ProgressBarThread( (int) file.length() );//创建进度条
            new Thread( pbt ).start();//开启线程,刷新进度条
            byte[] buf = new byte[1024];
            int size = 0;
            while ((size = fis.read( buf )) > -1) { //循环读取
                fos.write( buf, 0, size );
                pbt.updateProgress( size );//写完一次,更新进度条
            }
            pbt.finish(); //文件读取完成,关闭进度条
            fos.flush();
            fos.close();
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  

//

    // 测试
    public static void main(String[] args) {
        HttpClient httpClient = new DefaultHttpClient();
        try {
            HttpResponse resp = httpClient.execute( new HttpPost( "http://wppkg.baidupcs.com/issue/netdisk/MACguanjia/BaiduNetdisk_mac_3.4.1.dmg" ) );
            HttpEntity entity = resp.getEntity();
            int length = (int) entity.getContentLength();//这个就是下载的文件(不单指文件)大小
            InputStream is = entity.getContent();
            ProgressBarThread pbt = new ProgressBarThread( length );//创建进度条
            new Thread( pbt ).start(); //开启线程,刷新进度条
            byte[] buf = new byte[1024];
            int size = 0;
            FileOutputStream fos = new FileOutputStream( new File( "/Users/zhenning/Desktop/BaiduNetdisk_mac_3.4.1.dmg" ) ); //

            while ((size = is.read( buf )) > -1) { //循环读取
                fos.write( buf, 0, size );
                pbt.updateProgress( size );//写完一次,更新进度条
            }
            pbt.finish(); //文件读取完成,关闭进度条
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  

原文地址:https://www.cnblogs.com/Alexr/p/13697136.html