怎么设置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;
}
文檔在這里 。
總結
以上是生活随笔為你收集整理的怎么设置internet才能下载JAVA_如何使用Java从Internet下载和保存文件?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: coordinatorlayout_一篇
- 下一篇: java请实现程序输出以下星塔||输出菱