This is the source code demonstration for tutorial at https://kodemate.com/download-file-using-java/
Created
January 7, 2019 16:01
-
-
Save petehouston/193b60b6b86194cdb53a83be12df6426 to your computer and use it in GitHub Desktop.
Download file 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
package com.kodemate.tutorials.downloadfile; | |
import java.io.BufferedInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URL; | |
import java.nio.channels.Channels; | |
import java.nio.channels.FileChannel; | |
import java.nio.channels.ReadableByteChannel; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardCopyOption; | |
public class DownloadFile { | |
static final String FILE_URL = "https://kodemate.com/wp-content/uploads/2018/08/kodemate.com-logo-design-header-retina-300x99.png"; | |
public static void main(String... args) { | |
usingJavaIO(); | |
usingFilesCopy(); | |
usingJavaNIO(); | |
} | |
public static void usingJavaIO() { | |
BufferedInputStream in; | |
FileOutputStream fileStream; | |
try { | |
in = new BufferedInputStream(new URL(FILE_URL).openStream()); | |
fileStream = new FileOutputStream("./logo.png"); | |
byte buffer[] = new byte[1024]; | |
int bRead; | |
while((bRead = in.read(buffer, 0, 1024)) != -1) { | |
fileStream.write(buffer, 0, bRead); | |
} | |
fileStream.close(); | |
in.close(); | |
} catch (IOException e) { | |
System.out.println("Error: " + e.getMessage()); | |
} | |
} | |
public static void usingFilesCopy() { | |
InputStream in = null; | |
try { | |
in = new URL(FILE_URL).openStream(); | |
Files.copy(in, Paths.get("./logo.png"), StandardCopyOption.REPLACE_EXISTING); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void usingJavaNIO() { | |
try { | |
ReadableByteChannel readChannel = Channels.newChannel(new URL(FILE_URL).openStream()); | |
FileOutputStream fileStream = new FileOutputStream("./logo.png"); | |
FileChannel writeChannel = fileStream.getChannel(); | |
writeChannel.transferFrom(readChannel, 0, Long.MAX_VALUE); | |
fileStream.close(); | |
readChannel.close(); | |
writeChannel.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment