怎么设置internet才能下载JAVA_如何使用Java从Internet下载和保存文件?

我需要获取一个在线文件(例如http://www.example.com/information.asp )并将其保存到目录中。 我知道有几种逐行捕获和读取在线文件(URL)的方法,但是有没有一种方法可以使用Java下载和保存文件?

#1楼

import java.io.*;

import java.net.*;

public class filedown {

public static void download(String address, String localFileName) {

OutputStream out = null;

URLConnection conn = null;

InputStream in = null;

try {

URL url = new URL(address);

out = new BufferedOutputStream(new FileOutputStream(localFileName));

conn = url.openConnection();

in = conn.getInputStream();

byte[] buffer = new byte[1024];

int numRead;

long numWritten = 0;

while ((numRead = in.read(buffer)) != -1) {

out.write(buffer, 0, numRead);

numWritten += numRead;

}

System.out.println(localFileName + "\t" + numWritten);

}

catch (Exception exception) {

exception.printStackTrace();

}

finally {

try {

if (in != null) {

in.close();

}

if (out != null) {

out.close();

}

}

catch (IOException ioe) {

}

}

}

public static void download(String address) {

int lastSlashIndex = address.lastIndexOf('/');

if (lastSlashIndex >= 0 &&

lastSlashIndex < address.length() - 1) {

download(address, (new URL(address)).getFile());

}

else {

System.err.println("Could not figure out local file name for "+address);

}

}

public static void main(String[] args) {

for (int i = 0; i < args.length; i++) {

download(args[i]);

}

}

}

#2楼

Nio的用法更简单:

URL website = new URL("http://www.website.com/information.asp");

try (InputStream in = website.openStream()) {

Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);

}

#3楼

简单使用存在一个问题:

org.apache.commons.io.FileUtils.copyURLToFile(URL, File)

如果您需要下载并保存非常大的文件,或者通常情况下需要自动重试以防连接断开。

在这种情况下,我建议使用Apache HttpClient以及org.apache.commons.io.FileUtils。 例如:

GetMethod method = new GetMethod(resource_url);

try {

int statusCode = client.executeMethod(method);

if (statusCode != HttpStatus.SC_OK) {

logger.error("Get method failed: " + method.getStatusLine());

}

org.apache.commons.io.FileUtils.copyInputStreamToFile(

method.getResponseBodyAsStream(), new File(resource_file));

} catch (HttpException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

method.releaseConnection();

}

#4楼

这个答案几乎与选择的答案完全一样,但是有两个增强:这是一个方法,它关闭了FileOutputStream对象:

public static void downloadFileFromURL(String urlString, File destination) {

try {

URL website = new URL(urlString);

ReadableByteChannel rbc;

rbc = Channels.newChannel(website.openStream());

FileOutputStream fos = new FileOutputStream(destination);

fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

fos.close();

rbc.close();

} catch (IOException e) {

e.printStackTrace();

}

}

#5楼

使用Java 7+使用以下方法从Internet下载文件并将其保存到某个目录:

private static Path download(String sourceURL, String targetDirectory) throws IOException

{

URL url = new URL(sourceURL);

String fileName = sourceURL.substring(sourceURL.lastIndexOf('/') + 1, sourceURL.length());

Path targetPath = new File(targetDirectory + File.separator + fileName).toPath();

Files.copy(url.openStream(), targetPath, StandardCopyOption.REPLACE_EXISTING);

return targetPath;

}

文档在这里 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值