cjburkey01

Apache Commons IO Download With Progress Bar

Oct 12th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. package com.cjburkey.mods.bayc;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import java.io.File;
  6. import java.io.InputStream;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. import javax.swing.JFrame;
  10. import javax.swing.JProgressBar;
  11. import javax.swing.ProgressMonitorInputStream;
  12. import org.apache.commons.io.FileUtils;
  13.  
  14. public class Net {
  15.    
  16.     public static final void download(final String url, final File dest) {
  17.         try {
  18.                        
  19.             final JFrame frame = new JFrame();
  20.             final JProgressBar bar = new JProgressBar(0, 100);
  21.            
  22.             bar.setStringPainted(true);
  23.             bar.setString("Loading...");
  24.             bar.setPreferredSize(new Dimension(512, (int) bar.getPreferredSize().getHeight()));
  25.            
  26.             frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  27.             frame.setLayout(new FlowLayout());
  28.             frame.add(bar);
  29.             frame.pack();
  30.             frame.setResizable(false);
  31.             frame.setLocationRelativeTo(null);
  32.             frame.setVisible(true);
  33.            
  34.             new Thread(new Runnable() {
  35.                 public void run() {
  36.                     URLConnection con = null;
  37.                    
  38.                     try {
  39.                         con = new URL(url).openConnection();
  40.                     } catch (Exception e) {
  41.                         Core.error(e);
  42.                     }
  43.                    
  44.                     long total = con.getContentLength(), prog = 0;
  45.                     float percent = 0;
  46.                    
  47.                     while(percent < 99) {
  48.                         prog = dest.length();
  49.                         percent = ((float) prog / (float) total) * 100;
  50.                        
  51.                         bar.setValue((int) percent);
  52.                         bar.setString(String.format("%.2f", percent) + "%");
  53.                        
  54.                         frame.setTitle(dest.getName().replaceAll(".jar", ""));
  55.                         frame.requestFocus();
  56.                         frame.setLocationRelativeTo(null);
  57.                        
  58.                         try {
  59.                             Thread.sleep(250);
  60.                         } catch (Exception e) {
  61.                             Core.error(e);
  62.                         }
  63.                     }
  64.                 }
  65.             }).start();
  66.            
  67.             Core.downloaded = true;
  68.            
  69.             InputStream stream = new URL(url).openStream();
  70.             ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(null, "Downloading...", stream);
  71.             FileUtils.copyInputStreamToFile(pmis, dest);
  72.            
  73.             frame.dispose();
  74.         } catch(Exception e) {
  75.             Core.error(e);
  76.         }
  77.     }
  78.    
  79. }
Add Comment
Please, Sign In to add comment