Skip to content

Instantly share code, notes, and snippets.

@plavjanik
Created May 2, 2020 05:31
Show Gist options
  • Select an option

  • Save plavjanik/794df017145ad632f26aa7fd086c02df to your computer and use it in GitHub Desktop.

Select an option

Save plavjanik/794df017145ad632f26aa7fd086c02df to your computer and use it in GitHub Desktop.
Download file from URL using Java
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
public class jurl {
public static void main(String[] args) throws MalformedURLException, IOException {
String url = args[0];
String filename = args[1];
try (ReadableByteChannel readableByteChannel = Channels.newChannel(new URL(url).openStream());
FileOutputStream fileOutputStream = new FileOutputStream(filename)) {
FileChannel fileChannel = fileOutputStream.getChannel();
fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
}
}
}
@plavjanik

plavjanik commented May 2, 2020 •

Copy link
Copy Markdown
Author

Poormans replacement of curl on systems that do not have it but have Java (e.g. z/OS on IBM mainframes).

Usage:

javac jurl.java
java jurl <url> <filename>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment