實現在下載文件過程顯示進度條,簡單例子


 

 

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();
        }
    }

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM