Guest User

Untitled

a guest
Apr 26th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. package JamMinecraftLauncher;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10. import java.util.ArrayList;
  11.  
  12. public class DownloadsThread implements Runnable{
  13. String Destination = "";
  14. ArrayList<String> URLsList;
  15.  
  16. public DownloadsThread(ArrayList<String> urlsList, String destination){
  17. URLsList = urlsList;
  18. Destination = destination;
  19. }
  20.  
  21. @Override
  22. public void run() {
  23. for (String URL:URLsList){
  24. String[] UrlSplited = URL.split("/");
  25. String fileName = UrlSplited[UrlSplited.length - 1];
  26. try {
  27. downloadFile(URL, Destination + fileName);
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. synchronized (GUI.lock) {
  33. GUI.lock.notify();
  34. }
  35. }
  36.  
  37. public void downloadFile(String URL, String DestinationPlusName) throws IOException{
  38. float byteDownloaded = 1;
  39. URL url = new URL(URL);
  40. URLConnection connection = url.openConnection();
  41. InputStream stream = connection.getInputStream();
  42. BufferedInputStream in = new BufferedInputStream(stream);
  43. FileOutputStream file = new FileOutputStream(DestinationPlusName);
  44. BufferedOutputStream out = new BufferedOutputStream(file);
  45. int i;
  46. float totalSize = connection.getContentLength();
  47. while ((i = in.read()) != -1) {
  48. GUI.progressBarInstall.setValue((int)((byteDownloaded / totalSize) * 100));
  49. GUI.progressBarInstall.update(GUI.progressBarInstall.getGraphics());
  50. out.write(i);
  51. byteDownloaded++;
  52. }
  53. out.flush();
  54. file.close();
  55. }
  56.  
  57. }
Add Comment
Please, Sign In to add comment