Created
May 2, 2020 05:31
-
-
Save plavjanik/794df017145ad632f26aa7fd086c02df to your computer and use it in GitHub Desktop.
Download file from URL using Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Poormans replacement of
curlon systems that do not have it but have Java (e.g. z/OS on IBM mainframes).Usage: