溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

下載器-http 進度條 文件讀寫

發(fā)布時間:2020-07-08 11:09:33 來源:網(wǎng)絡(luò) 閱讀:271 作者:glblong 欄目:開發(fā)技術(shù)

 

下載器-http  進度條 文件讀寫

 

 

  1. final JButton button = new JButton("下載");  
  2. button.addActionListener(new ActionListener() 
  3.     public void actionPerformed(ActionEvent e) 
  4.     { 
  5.         new Thread() 
  6.         { 
  7.             @Override 
  8.             public void run() 
  9.             { 
  10.                 button.setEnabled(false);//設(shè)置按鈕在下載過程中不可操作 
  11.                 FileOutputStream fos = null
  12.                 try 
  13.                 { 
  14.                     URL url = new URL(txtFilefjavaindexhtml.getText());//建立HTTP連接 
  15.                     URLConnection conn = url.openConnection();//創(chuàng)建連接對象 
  16.                      
  17.                     int fileSize = conn.getContentLength();//用getContentLength()得到連接對象的文件大小 
  18.                      
  19.                     InputStream is = conn.getInputStream(); 
  20.                     byte[] buffer = new byte[1024]; 
  21.                     int length = 0
  22.                      
  23.                     fos = new FileOutputStream(txtFilefjavaindexhtml_1.getText()); 
  24.                              
  25.                     double downSize = 0
  26.                     while (-1 != (length = is.read(buffer))) 
  27.                     { 
  28.                         downSize += length; 
  29.                         double percent = downSize * 100 / fileSize;//計算下載百分比 
  30.                         fos.write(buffer, 0, length); 
  31.                         progressBar.setValue((int) percent);//設(shè)置進度條變化 
  32.                     } 
  33.                 } 
  34.                 catch (MalformedURLException e1) 
  35.                 { 
  36.                     e1.printStackTrace(); 
  37.                 } 
  38.                 catch (IOException e1) 
  39.                 { 
  40.                     e1.printStackTrace(); 
  41.                 } 
  42.                 finally//輸出流要記得在fanally關(guān)掉 
  43.                 { 
  44.                     button.setEnabled(true);//在finally中恢復(fù)按鈕可操作 
  45.                     if (null != fos) 
  46.                     { 
  47.                         try 
  48.                         { 
  49.                             fos.close(); 
  50.                         } 
  51.                         catch (IOException e1) 
  52.                         { 
  53.                             e1.printStackTrace(); 
  54.                         } 
  55.                     } 
  56.                 } 
  57.             } 
  58.         }.start(); 
  59.          
  60.     } 
  61. }); 
  62. button.setFont(new Font("宋體", Font.PLAIN, 24)); 
  63. button.setBounds(101649737); 
  64. frame.getContentPane().add(button); 
  65.  
  66. progressBar = new JProgressBar(); 
  67. progressBar.setBounds(15216427237); 
  68. frame.getContentPane().add(progressBar);//進度條按鈕 

 

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI