Advertisement
Guest User

Untitled

a guest
Mar 4th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. System.out.println("Downloading: " + url);
  2. URL file = new URL(url);
  3. int size = file.openConnection().getContentLength();
  4.  
  5.  
  6. String path = "/Users/user/Downloads/test_download";
  7. String fileName = url.substring(url.lastIndexOf("/")+1);
  8. path = path + fileName;
  9.  
  10. ReadableByteChannel in = Channels.newChannel(file.openStream());
  11. SeekableByteChannel out = Files.newByteChannel(Paths.get(fileName), WRITE, TRUNCATE_EXISTING, CREATE);
  12. ByteBuffer buf = ByteBuffer.allocate(size);
  13. threadName = fileName;
  14. System.out.println("Size: " + size + ";" + Paths.get(fileName) + "; " + Thread.currentThread().getName());
  15.  
  16. while (in.read(buf) != -1) {
  17. buf.flip();
  18. out.write(buf);
  19. buf.rewind();
  20. buf.limit(buf.capacity());
  21. }
  22.  
  23. in.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement