温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

下载器-http 进度条 文件读写

发布时间:2020-07-08 11:09:33 来源:网络 阅读:260 作者:glblong 栏目:开发技术

 

下载器-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);//设置按钮在下载过程中不可操作 
  11.                 FileOutputStream fos = null
  12.                 try 
  13.                 { 
  14.                     URL url = new URL(txtFilefjavaindexhtml.getText());//建立HTTP连接 
  15.                     URLConnection conn = url.openConnection();//创建连接对象 
  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);//设置进度条变化 
  32.                     } 
  33.                 } 
  34.                 catch (MalformedURLException e1) 
  35.                 { 
  36.                     e1.printStackTrace(); 
  37.                 } 
  38.                 catch (IOException e1) 
  39.                 { 
  40.                     e1.printStackTrace(); 
  41.                 } 
  42.                 finally//输出流要记得在fanally关掉 
  43.                 { 
  44.                     button.setEnabled(true);//在finally中恢复按钮可操作 
  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问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI