日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2025/3/12 java 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 怎么设置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下载和保存文件?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。