swing 下载文件时的进度条

简介:

Java swing中经常要使用到进度条,比如下载文件和拷贝文件时显示进度,如下图所示:

 下载完成之后:

难点在于:在读取输入流的时候,实时更新进度条。 

我自己定了一个规则:

(1) 输入流的大小小于1024,则字节数组的长度就是输入流的大小

<!--[if !supportLists]-->(1)      (2) <!--[endif]-->获取文件的大小,并平分为100等份,得到1

<!--[if !supportLists]-->(2)       (3)<!--[endif]-->拿每一等份(1)和1024比较:若小于等于1024,则字节数组大小为1

若大于1024,则除以1024,得到2,则字节数组大小为商1/2

<!--[if !supportLists]-->(3)      

 

规定:100%时的颜色为蓝色:copyProgressBar.setForeground(Color.blue);

核心代码:

Java代码   收藏代码
  1. /*** 
  2.      * 在实际使用过程中,应该放在线程中 
  3.      * @param in 
  4.      * @param sourceSize 
  5.      *            : 输入流的长度,即要读取的字节个数 
  6.      * @param targfile 
  7.      */  
  8.     public static void progress(JProgressBar copyProgressBar, InputStream in,  
  9.             long sourceSize, File targfile) {  
  10.         FileOutputStream target = null;  
  11.         try {  
  12.             int bytesArrLeng = 0;  
  13.             if (sourceSize < SystemHWUtil.BUFF_SIZE_1024) {//  
  14.                 bytesArrLeng = (int) sourceSize;  
  15.             } else {  
  16.                 long shangOne = sourceSize / SystemHWUtil.NUMBER_100;  
  17.                 if (shangOne == 0) {// sourceSize<100  
  18.                     shangOne = shangOne + 1;  
  19.                 }  
  20.   
  21.                 if (shangOne <= SystemHWUtil.BUFF_SIZE_1024) {  
  22.                     bytesArrLeng = (int) shangOne;  
  23.                 } else {  
  24.                     long shangTwo = shangOne / SystemHWUtil.BUFF_SIZE_1024;  
  25.                     if (shangOne % SystemHWUtil.BUFF_SIZE_1024 != 0) {  
  26.                         shangTwo = shangTwo + 1L;  
  27.                     }  
  28.                     bytesArrLeng = (int) (shangOne / shangTwo);  
  29.                 }  
  30.             }  
  31.             System.out.println("字节数组的长度是:" + bytesArrLeng);  
  32.             target = new FileOutputStream(targfile);  
  33.             byte[] buffer = new byte[bytesArrLeng];  
  34.             int byteNum;  
  35.             long hasReadByte = 0L;// 已经读取的字节个数  
  36.             float result;  
  37.             int progressSize = 0;  
  38.             while ((byteNum = in.read(buffer)) != SystemHWUtil.NEGATIVE_ONE) {  
  39.                 target.write(buffer, 0, byteNum);  
  40.                 hasReadByte = hasReadByte + byteNum;  
  41.                 result = (float) ((double) hasReadByte / sourceSize);  
  42.                 progressSize = Math.round(result * SystemHWUtil.NUMBER_100);  
  43.                 // copyProgressBar.setString(progressSize + "%");  
  44.                 // copyProgressBar.setValue(progressSize);  
  45.                 updateProgress(copyProgressBar, progressSize);  
  46.             }  
  47.             if (progressSize < SystemHWUtil.NUMBER_100) {  
  48.                 progressSize = SystemHWUtil.NUMBER_100;  
  49.                 updateProgress(copyProgressBar, progressSize);  
  50.             }  
  51.             copyProgressBar.setForeground(Color.blue);  
  52.             // System.out  
  53.             // .println("[SystemUtil:copyFile]:file copy successfully!");  
  54.             // resultTextArea.setText();  
  55.   
  56.         } catch (Exception e) {  
  57.             e.printStackTrace();  
  58.             // copyFileBtn.setEnabled(true);  
  59.         } finally {  
  60.             if (in != null) {  
  61.                 try {  
  62.                     in.close();  
  63.                 } catch (IOException e) {  
  64.                     e.printStackTrace();  
  65.                 }  
  66.                 in = null;  
  67.             }  
  68.             if (target != null) {  
  69.                 try {  
  70.                     target.close();  
  71.                 } catch (IOException e) {  
  72.                     e.printStackTrace();  
  73.                 }  
  74.                 target = null;  
  75.             }  
  76.         }  
  77.     }  
  78.   
  79.     /*** 
  80.      * 更新进度条上得进度数字 
  81.      * @param copyProgressBar 
  82.      * @param progressSize 
  83.      */  
  84.     private static void updateProgress(JProgressBar copyProgressBar, int progressSize) {  
  85.         copyProgressBar.setString(progressSize + "%");  
  86.         copyProgressBar.setValue(progressSize);  
  87.     }  

 使用进度条的代码:

Java代码   收藏代码
  1. new Thread(new Runnable() {  
  2.   
  3.                     @Override  
  4.                     public void run() {  
  5.                         try {  
  6.                             HttpSocketUtil.setDetail(true);  
  7.                             // HttpSocketUtil.httpRequest(urlTF.getText(), null,  
  8.                             // null, true, selectedFile, -1, -1);  
  9.                             HttpURLConnection huc = HttpSocketUtil  
  10.                                     .beforeConnect(httpSenderApp.getUrlTF()  
  11.                                             .getText(), nullnullnullnull,  
  12.                                             SystemHWUtil.NEGATIVE_ONE,  
  13.                                             SystemHWUtil.NEGATIVE_ONE);  
  14.                             InputStream in = huc.getInputStream();  
  15.                             int contentLength;  
  16.                             String sizeHeadKey = null;  
  17.                             if (ValueWidget.isNullOrEmpty(sizeHeadKey)) {// 若header中没有size  
  18.                                 contentLength = huc.getContentLength();  
  19.                             } else {  
  20.                                 contentLength = Integer.parseInt(huc  
  21.                                         .getHeaderField(sizeHeadKey));  
  22.                             }  
  23.                             ComponentUtil.progress(  
  24.                                     httpSenderApp.getCopyProgressBar(), in,  
  25.                                     contentLength, selectedFile);  
  26.   
  27.                             GUIUtil23.infoDialog("下载成功!size:"  
  28.                                     + FileUtils.formatFileSize2(selectedFile  
  29.                                             .length()));  
  30.                         } catch (Exception e) {  
  31.                             GUIUtil23.errorDialog(e.getMessage());  
  32.                             e.printStackTrace();  
  33.                         }  
  34.   
  35.                     }  
  36.                 }).start();  

 

依赖的jar:io0007-find_progess-0.0.7.2-SNAPSHOT.jar

demo:http-sender

注意:项目使用maven 构建

相关文章
|
5月前
swing/swt 支持多屏幕显示
swing/swt 支持多屏幕显示
|
Android开发
eclipse awt包在图片上显示文字
eclipse awt包在图片上显示文字
118 0
eclipse awt包在图片上显示文字
|
前端开发 JavaScript Java
关于Easy UI中文件上传处理进度条的实现
关于Easy UI中文件上传处理进度条的实现
321 0
pyqt5的下载进度条 实现模板
pyqt5的下载进度条 实现模板
2932 0