[BAEL-15998] - Move articles out of core-java-io part 1
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package com.baeldung.download;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.asynchttpclient.*;
|
||||
|
||||
import java.io.*;
|
||||
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;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class FileDownload {
|
||||
|
||||
public static void downloadWithJavaIO(String url, String localFilename) {
|
||||
|
||||
try (BufferedInputStream in = new BufferedInputStream(new URL(url).openStream()); FileOutputStream fileOutputStream = new FileOutputStream(localFilename)) {
|
||||
|
||||
byte dataBuffer[] = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
|
||||
fileOutputStream.write(dataBuffer, 0, bytesRead);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void downloadWithJava7IO(String url, String localFilename) {
|
||||
|
||||
try (InputStream in = new URL(url).openStream()) {
|
||||
Files.copy(in, Paths.get(localFilename), StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void downloadWithJavaNIO(String fileURL, String localFilename) throws IOException {
|
||||
|
||||
URL url = new URL(fileURL);
|
||||
try (ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(localFilename); FileChannel fileChannel = fileOutputStream.getChannel()) {
|
||||
|
||||
fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
|
||||
fileOutputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void downloadWithApacheCommons(String url, String localFilename) {
|
||||
|
||||
int CONNECT_TIMEOUT = 10000;
|
||||
int READ_TIMEOUT = 10000;
|
||||
try {
|
||||
FileUtils.copyURLToFile(new URL(url), new File(localFilename), CONNECT_TIMEOUT, READ_TIMEOUT);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void downloadWithAHC(String url, String localFilename) throws ExecutionException, InterruptedException, IOException {
|
||||
|
||||
FileOutputStream stream = new FileOutputStream(localFilename);
|
||||
AsyncHttpClient client = Dsl.asyncHttpClient();
|
||||
|
||||
client.prepareGet(url)
|
||||
.execute(new AsyncCompletionHandler<FileOutputStream>() {
|
||||
|
||||
@Override
|
||||
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
|
||||
stream.getChannel()
|
||||
.write(bodyPart.getBodyByteBuffer());
|
||||
return State.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileOutputStream onCompleted(Response response) throws Exception {
|
||||
return stream;
|
||||
}
|
||||
})
|
||||
.get();
|
||||
|
||||
stream.getChannel().close();
|
||||
client.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.download;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
public class ResumableDownload {
|
||||
|
||||
public static long downloadFile(String downloadUrl, String saveAsFileName) throws IOException, URISyntaxException {
|
||||
|
||||
File outputFile = new File(saveAsFileName);
|
||||
URLConnection downloadFileConnection = new URI(downloadUrl).toURL()
|
||||
.openConnection();
|
||||
return transferDataAndGetBytesDownloaded(downloadFileConnection, outputFile);
|
||||
}
|
||||
|
||||
private static long transferDataAndGetBytesDownloaded(URLConnection downloadFileConnection, File outputFile) throws IOException {
|
||||
|
||||
long bytesDownloaded = 0;
|
||||
try (InputStream is = downloadFileConnection.getInputStream(); OutputStream os = new FileOutputStream(outputFile, true)) {
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
int bytesCount;
|
||||
while ((bytesCount = is.read(buffer)) > 0) {
|
||||
os.write(buffer, 0, bytesCount);
|
||||
bytesDownloaded += bytesCount;
|
||||
}
|
||||
}
|
||||
return bytesDownloaded;
|
||||
}
|
||||
|
||||
public static long downloadFileWithResume(String downloadUrl, String saveAsFileName) throws IOException, URISyntaxException {
|
||||
File outputFile = new File(saveAsFileName);
|
||||
|
||||
URLConnection downloadFileConnection = addFileResumeFunctionality(downloadUrl, outputFile);
|
||||
return transferDataAndGetBytesDownloaded(downloadFileConnection, outputFile);
|
||||
}
|
||||
|
||||
private static URLConnection addFileResumeFunctionality(String downloadUrl, File outputFile) throws IOException, URISyntaxException, ProtocolException, ProtocolException {
|
||||
long existingFileSize = 0L;
|
||||
URLConnection downloadFileConnection = new URI(downloadUrl).toURL()
|
||||
.openConnection();
|
||||
|
||||
if (outputFile.exists() && downloadFileConnection instanceof HttpURLConnection) {
|
||||
HttpURLConnection httpFileConnection = (HttpURLConnection) downloadFileConnection;
|
||||
|
||||
HttpURLConnection tmpFileConn = (HttpURLConnection) new URI(downloadUrl).toURL()
|
||||
.openConnection();
|
||||
tmpFileConn.setRequestMethod("HEAD");
|
||||
long fileLength = tmpFileConn.getContentLengthLong();
|
||||
existingFileSize = outputFile.length();
|
||||
|
||||
if (existingFileSize < fileLength) {
|
||||
httpFileConnection.setRequestProperty("Range", "bytes=" + existingFileSize + "-" + fileLength);
|
||||
} else {
|
||||
throw new IOException("File Download already completed.");
|
||||
}
|
||||
}
|
||||
return downloadFileConnection;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user