Guest User

Untitled

a guest
Apr 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1.     public static void download(final String host, final String fileName)
  2.     {
  3.         new Thread(new Runnable() {
  4.             public void run() {
  5.                 Windows.progressBar.setValue(0);
  6.                 try
  7.                 {
  8.                     URL url = new URL(host);
  9.                     URLConnection connexion = url.openConnection();
  10.        
  11.                     FileOutputStream writeFile = null;
  12.                     writeFile = new FileOutputStream(fileName);
  13.                     InputStream input = connexion.getInputStream();
  14.                     byte[] buffer = new byte[1024];
  15.                     int read;
  16.                     long readed = 0;
  17.                     int total = connexion.getContentLength();
  18.                    
  19.                     while ((read = input.read(buffer)) > 0) /* Téléchargement */
  20.                     {
  21.                         readed += read;
  22.                         Windows.progressBar.setValue((int) (100 * readed / total));
  23.                         Windows.telechargement.setText((int) (100 * readed / total) + "%");
  24.                         Windows.contentPane.validate();
  25.                         writeFile.write(buffer, 0, read);
  26.                         System.out.println((int) (100 * readed / total) + "%");
  27.                     }
  28.                                    
  29.                     input.close();
  30.                     writeFile.close();
  31.                 }
  32.                 catch (Exception e)
  33.                 {
  34.                     e.printStackTrace();
  35.                 }
  36.          }
  37.           }).start();
  38.        
  39.         while(Windows.progressBar.getValue() < 100)
  40.         {
  41.             System.out.println("");
  42.         }
  43.     }
Add Comment
Please, Sign In to add comment