Here's how to save a url to file using the java.nio.file.Files class. This enables us to copy the InputStream from the URLin a simple way to a file. The source is HERE
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class UrlSaver {
public static void main(String[] args) throws IOException {
// Save url to file url.html in temp directory
String html = new File(System.getProperty("java.io.tmpdir"), "url.html").getAbsolutePath();
UrlSaver.copyStreamToFile(new URL(args[0]).openStream(), html);
}
public static void copyStreamToFile(InputStream source, String target) {
try {
Files.copy(source, new File(target).toPath(),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class UrlSaver {
public static void main(String[] args) throws IOException {
// Save url to file url.html in temp directory
String html = new File(System.getProperty("java.io.tmpdir"), "url.html").getAbsolutePath();
UrlSaver.copyStreamToFile(new URL(args[0]).openStream(), html);
}
public static void copyStreamToFile(InputStream source, String target) {
try {
Files.copy(source, new File(target).toPath(),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
}