Java – How to download a file from the Internet

参考链接:[url]https://www.mkyong.com/java/java-how-to-download-a-file-from-the-internet/[/url]

1、Apache Commons IO

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>


String fromFile = "ftp://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest";
String toFile = "F:\\arin.txt";
try {

// connectionTimeout, readTimeout = 10 seconds
FileUtils.copyURLToFile(new URL(fromFile), new File(toFile), 10000, 10000);

} catch (IOException e) {
e.printStackTrace();
}


2、Java NIO

String fromFile = "https://download-cf.jetbrains.com/idea/ideaIU-2018.1.6.win.zip";
String toFile = "F:\\IDEA\\ideaIU-2018.1.6.win.zip";

try {

URL website = new URL(fromFile);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(toFile);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
System.out.println("---------success----------------");
} catch (IOException e) {
e.printStackTrace();
}




static final String FILE_URL = "https://download-cf.jetbrains.com/idea/ideaIU-2018.1.6.exe";

static final String FILE_NAME = "F:\\IDEA\\ideaIU-2018.1.6.exe";

public static void main(String[] args) throws Exception {

javaIO();

}

static void javaIO() {
try (BufferedInputStream in = new BufferedInputStream(new URL(FILE_URL).openStream());
FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME)) {
byte dataBuffer[] = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
System.out.println("read >" + bytesRead);
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
} catch (IOException e) {
// handle exception
}
}


3、多线程下载文件

package net.liuzidong.download;

import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* 多线程下载文件
*/
public class DownloadUtil {

// 定义成员变量
private String path; // 远程资源路径
private String targetPath; // 本地存储路径
private DownFileThread[] threads; // 线程list
private int threadNum; // 线程数量
private long length; // 下载的文件大小

// 构造初始化
public DownloadUtil(String path, String targetPath, int threadNum) {
super();
this.path = path;
this.targetPath = targetPath;
this.threads = new DownFileThread[threadNum];
this.threadNum = threadNum;
}

// 多线程下载文件资源
public void download() {
URL url;
try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5 * 1000); // 设置超时时间为5秒
conn.setRequestMethod("GET");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("accept", "*/*");

// 获取远程文件的大小
length = conn.getContentLength();
conn.disconnect();

// 设置本地文件大小
RandomAccessFile targetFile = new RandomAccessFile(targetPath, "rw");
targetFile.setLength(length);

// 每个线程下载大小
long avgPart = length / threadNum + 1;
// 下载文件
for (int i = 0; i < threadNum; i++) {
long startPos = avgPart * i;
RandomAccessFile targetTmp = new RandomAccessFile(targetPath, "rw");
targetTmp.seek(startPos); // 分段下载
threads[i] = new DownFileThread(startPos, targetTmp, avgPart);
threads[i].start();
}
} catch (Exception e) {
e.printStackTrace();
}
}

// 监控下载进度
public double getDownRate() {
int currentSize = 0;
for (int i = 0; i < threadNum; i++) {
currentSize += threads[i].length;
}
return currentSize * 1.0 / length;
}

// 定义线程类
class DownFileThread extends Thread {

private long startPos;
private RandomAccessFile raf;
private long size;
private long length;

public DownFileThread(long startPos, RandomAccessFile raf, long size) {
super();
this.startPos = startPos;
this.raf = raf;
this.size = size;
}

public void run() {
URL url;
try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5 * 1000); // 设置超时时间为5秒
conn.setRequestMethod("GET");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("accept", "*/*");

InputStream in = conn.getInputStream();
in.skip(this.startPos);
byte[] buf = new byte[1024];
int hasRead = 0;
while (length < size && (hasRead = in.read(buf)) != -1) {
raf.write(buf, 0, hasRead);
length += hasRead;
}
raf.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

// 测试
public static void main(String[] args) {
String path = "http://ftp.jaist.ac.jp/pub/eclipse/technology/epp/downloads/release/photon/R/eclipse-jee-photon-R-win32-x86_64.zip";
String targetPath = "F:\\download\\eclipse\\eclipse-jee-photon-R-win32-x86_64.zip";
final DownloadUtil download = new DownloadUtil(path, targetPath, 3);
download.download();
System.out.println("开始下载...");
// 主线程负责下载文件,在启动一个线程负责监控下载的进度
new Thread(new Runnable() {

@Override
public void run() {
while (download.getDownRate() < 1) {
try {
Thread.sleep(2000); // 200毫秒扫描一次
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("下载完成...");
}

}).start();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值