Copy from an Internet URL to local file - Java File Path IO

Java examples for File Path IO:File Stream

Description

Copy from an Internet URL to local file

Demo Code

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) throws Exception {
    Path copy_to = Paths.get("C:/folder1/photos/test.jpg");
    URI u = URI.create("https://java2s.com/fake.jpg");

    try (InputStream in = u.toURL().openStream()) {

      Files.copy(in, copy_to);/*w  w  w  .j a  v  a 2s. c o  m*/

    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

Result


Related Tutorials