zoukankan      html  css  js  c++  java
  • 使用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();
            }
        }
    

      

  • 相关阅读:
    谈mvc开发中gzip压缩的应用
    MIME 类型(HttpContext.Response.ContentType)列表
    Asp.net使用HttpModule压缩并删除空白Html请求
    ASP.NET MVC 网站优化之压缩技术
    从零开始编写自己的C#框架(25)——网站部署 【转】
    ReSharper的功能真的很强大主要是针对代码规范和优化,园子里介绍的也不少,如果你没有安装,那我只能表示你们会相见恨晚
    多用户角色权限访问模块问题”的解决思路( 位运算 + ActionFilterAttribute )
    Asp.Net Web Api 图片上传
    sqlserver并发用户数
    在线图片服务设计小计
  • 原文地址:https://www.cnblogs.com/Alexr/p/13697136.html
Copyright © 2011-2022 走看看