Java 從網(wǎng)上下載文件的幾種方式實(shí)例代碼詳解
廢話不多說了,直接給大家貼代碼了,具體代碼如下所示;
package com.github.pandafang.tool;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import org.apache.commons.io.FileUtils;
/**
* 文件工具類
* @author panda fang
* @date 2017-08-26
* @version 1.0
*/
public class FileTool {
/**
* 使用傳統(tǒng)io stream 下載文件
* @param url
* @param saveDir
* @param fileName
*/
public static void download(String url, String saveDir, String fileName) {
BufferedOutputStream bos = null;
InputStream is = null;
try {
byte[] buff = new byte[8192];
is = new URL(url).openStream();
File file = new File(saveDir, fileName);
file.getParentFile().mkdirs();
bos = new BufferedOutputStream(new FileOutputStream(file));
int count = 0;
while ( (count = is.read(buff)) != -1) {
bos.write(buff, 0, count);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 利用 commonio 庫下載文件,依賴Apache Common IO ,官網(wǎng) https://commons.apache.org/proper/commons-io/
* @param url
* @param saveDir
* @param fileName
*/
public static void downloadByApacheCommonIO(String url, String saveDir, String fileName) {
try {
FileUtils.copyURLToFile(new URL(url), new File(saveDir, fileName));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 使用NIO下載文件, 需要 jdk 1.4+
* @param url
* @param saveDir
* @param fileName
*/
public static void downloadByNIO(String url, String saveDir, String fileName) {
ReadableByteChannel rbc = null;
FileOutputStream fos = null;
FileChannel foutc = null;
try {
rbc = Channels.newChannel(new URL(url).openStream());
File file = new File(saveDir, fileName);
file.getParentFile().mkdirs();
fos = new FileOutputStream(file);
foutc = fos.getChannel();
foutc.transferFrom(rbc, 0, Long.MAX_VALUE);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (rbc != null) {
try {
rbc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (foutc != null) {
try {
foutc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 使用NIO下載文件, 需要 jdk 1.7+
* @param url
* @param saveDir
* @param fileName
*/
public static void downloadByNIO2(String url, String saveDir, String fileName) {
try (InputStream ins = new URL(url).openStream()) {
Path target = Paths.get(saveDir, fileName);
Files.createDirectories(target.getParent());
Files.copy(ins, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
}
下載一個(gè)百度logo 測試一下
public static void main(String[] args) {
FileTool.downloadByNIO2("http://www.baidu.com/img/bd_logo1.png", "/home/panda/picture", "baidu_logo.png");
System.out.println("done...");
}
總結(jié)
以上所述是小編給大家介紹的Java 從網(wǎng)上下載文件的幾種方式實(shí)例代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Springboot 整合 Java DL4J 實(shí)現(xiàn)文物保護(hù)系統(tǒng)的詳細(xì)過程
在數(shù)字化時(shí)代,文物保護(hù)尤為關(guān)鍵,本文介紹如何利用SpringBoot和Deeplearning4j構(gòu)建一個(gè)圖像識(shí)別的文物保護(hù)系統(tǒng),系統(tǒng)采用卷積神經(jīng)網(wǎng)絡(luò)(CNN),能夠識(shí)別文物的損壞情況,本文介紹Springboot 整合 Java DL4J 實(shí)現(xiàn)文物保護(hù)系統(tǒng),感興趣的朋友一起看看吧2024-10-10
Java并發(fā)Futures和Callables類實(shí)例詳解
Callable對象返回Future對象,該對象提供監(jiān)視線程執(zhí)行的任務(wù)進(jìn)度的方法, Future對象可用于檢查Callable的狀態(tài),然后線程完成后從Callable中檢索結(jié)果,這篇文章給大家介紹Java并發(fā)Futures和Callables類的相關(guān)知識(shí),感興趣的朋友一起看看吧2024-05-05
SpringBoot如何優(yōu)雅的實(shí)現(xiàn)重試功能
這篇文章主要詳細(xì)介紹了SpringBoot如何優(yōu)雅的實(shí)現(xiàn)重試功能的步驟詳解,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,感興趣的同學(xué)可以借鑒閱讀2023-06-06
SpringMVC?Restful風(fēng)格與中文亂碼問題解決方案介紹
Restful就是一個(gè)資源定位及資源操作的風(fēng)格,不是標(biāo)準(zhǔn)也不是協(xié)議,只是一種風(fēng)格,是對http協(xié)議的詮釋,下面這篇文章主要給大家介紹了關(guān)于SpringMVC對Restful風(fēng)格支持的相關(guān)資料,需要的朋友可以參考下2022-10-10
postman中POST請求時(shí)參數(shù)包含參數(shù)list設(shè)置方式
這篇文章主要介紹了postman中POST請求時(shí)參數(shù)包含參數(shù)list設(shè)置方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05

