Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.simple.connection;
- import java.io.BufferedInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URL;
- import java.net.URLConnection;
- public class Example1 {
- public static final String ADDRESS = "http://fullhdwall.com/wp-content/uploads/2016/11/Yellow-New-Year-Background.jpg";
- public static final String TARGET_FILE = "Foo.jpg";
- public static void main(String[] args) throws IOException {
- byte[] target;
- long before = System.nanoTime();
- URLConnection conn = new URL(ADDRESS).openConnection();
- try (InputStream is = new BufferedInputStream(conn.getInputStream());
- FileOutputStream fileOutputStream = new FileOutputStream(TARGET_FILE)) {
- target = new byte[conn.getContentLength()];
- int bytesRead;
- while ((bytesRead = is.read(target, 0, target.length)) != -1) {
- fileOutputStream.write(target, 0, bytesRead);
- }
- }
- long after = System.nanoTime();
- System.out.println("Download time: " + ((after - before)/1e6) + "ms");
- System.out.println("Total: " + target.length/1024 + "kB");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement