Guest User

My downloader

a guest
Sep 5th, 2011
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. public void download(String address, String localFileName, String saveTo) {
  2. OutputStream out = null;
  3. URLConnection conn = null;
  4. InputStream in = null;
  5. try {
  6. File f = new File(saveTo + localFileName);
  7. f.createNewFile();
  8. URL url = new URL(address);
  9. out = new BufferedOutputStream(new FileOutputStream(saveTo + localFileName));
  10. conn = url.openConnection();
  11. in = conn.getInputStream();
  12. byte[] buffer = new byte[1024];
  13. int numRead;
  14. long numWritten = 0;
  15. int length = conn.getContentLength();
  16. while ((numRead = in.read(buffer)) != -1)
  17. {
  18. int percentage = (int)(((double)numWritten / (double)length) * 100D);
  19. //percentage.. need estimated time..
  20. out.write(buffer, 0, numRead);
  21. numWritten += numRead;
  22. }
  23. } catch (Exception exception) {
  24. exception.printStackTrace();
  25. } finally {
  26. try {
  27. if (in != null) {
  28. in.close();
  29. }
  30. if (out != null) {
  31. out.close();
  32. }
  33. } catch (IOException ioe) {
  34. }
  35. }
  36. }
Add Comment
Please, Sign In to add comment