Java如何實(shí)現(xiàn)遠(yuǎn)程文件下載到本地目錄
前言
今天開發(fā)時(shí)遇見了一個(gè)下載附件的需求
他的附件是存在一個(gè)網(wǎng)盤里查詢時(shí)只是給我返回了一個(gè)https的路徑
需要通過這個(gè)路徑把附件下載到本地的目錄里
一、正文介紹
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
/**
* @Author
* @Date
* @Version 1.0
* <p>備注:遠(yuǎn)程文件下載到本地 方法二選一<p>
*/
public class downloadUtil {
/**
* 下載遠(yuǎn)程文件并保存到本地
*
* @param remoteFilePath-遠(yuǎn)程文件路徑
* @param localFilePath-本地文件路徑(帶文件名)
*/
public static void downloadFile1(String remoteFilePath, String localFilePath) {
URL urlfile = null;
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File f = new File(localFilePath);
try {
urlfile = new URL(remoteFilePath);
httpUrl = (HttpURLConnection) urlfile.openConnection();
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(f));
int len = 2048;
byte[] b = new byte[len];
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
bos.flush();
bis.close();
httpUrl.disconnect();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 下載遠(yuǎn)程文件并保存到本地
*
* @param remoteFilePath-遠(yuǎn)程文件路徑
* @param localFilePath-本地文件路徑(帶文件名)
*/
public static void downloadFile2(String remoteFilePath, String localFilePath) {
URL website = null;
ReadableByteChannel rbc = null;
FileOutputStream fos = null;
try {
website = new URL(remoteFilePath);
rbc = Channels.newChannel(website.openStream());
fos = new FileOutputStream(localFilePath);//本地要存儲(chǔ)的文件地址 例如:test.txt
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(rbc!=null){
try {
rbc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 小試牛刀
* @param args
*/
public static void main(String[] args) {
/*遠(yuǎn)程文件路徑*/
String remoteFilePath1 = "https://tenfei01.cfp.cn/creative/vcg/800/new/VCG211157640278-VXD.jpg";
String remoteFilePath2 = "https://pic.3gbizhi.com/2019/1112/20191112013312648.jpg";
/*本地文件路徑(帶文件名)*/
String localFilePath1 ="E:\\LeStoreDownload\\update\\廣州塔.jpg";
String localFilePath2 ="E:\\LeStoreDownload\\update\\大橋.jpg";
downloadFile1(remoteFilePath1,localFilePath1);
downloadFile2(remoteFilePath2,localFilePath2);
}
}
二、測(cè)試介紹
這里我使用的是網(wǎng)上搜索的圖片路徑做了一下測(cè)試僅供參考 如正文介紹
總結(jié)
使用Java實(shí)現(xiàn)遠(yuǎn)程文件下載到本地目錄記錄就到此結(jié)束了
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA maven 構(gòu)建簡(jiǎn)單springmvc項(xiàng)目(圖文教程)
在工作當(dāng)中,我們有時(shí)需要?jiǎng)?chuàng)建一個(gè)全新的工程,而基于spring-mvc web的工程較為常見,這篇文章主要介紹了IntelliJ IDEA maven 構(gòu)建簡(jiǎn)單springmvc項(xiàng)目(圖文教程),感興趣的小伙伴們可以參考一下2018-05-05
啟動(dòng)Springboot項(xiàng)目時(shí)找不到Mapper的問題及解決
這篇文章主要介紹了啟動(dòng)Springboot項(xiàng)目時(shí)找不到Mapper的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Java簡(jiǎn)單計(jì)算器的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java簡(jiǎn)單計(jì)算器的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
java虛擬機(jī)鉤子關(guān)閉函數(shù)addShutdownHook的操作
這篇文章主要介紹了java虛擬機(jī)鉤子關(guān)閉函數(shù)addShutdownHook的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
解決SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼問題的幾種方法
本篇文章主要介紹了解決SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼問題的幾種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Java五子棋簡(jiǎn)單實(shí)現(xiàn)代碼舉例
Java五子棋游戲是一種經(jīng)典的兩人對(duì)戰(zhàn)棋類游戲,它基于簡(jiǎn)單的規(guī)則,即任何一方的棋子在棋盤上形成連續(xù)的五個(gè),無論是橫、豎還是斜線,都將獲勝,這篇文章主要介紹了Java五子棋實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2024-10-10
一文帶你學(xué)會(huì)Java網(wǎng)絡(luò)編程
網(wǎng)絡(luò)編程是指編寫運(yùn)行在多個(gè)設(shè)備(計(jì)算機(jī))的程序,這些設(shè)備都通過網(wǎng)絡(luò)連接起來。這篇文章將帶大家深入了解一下Java的網(wǎng)絡(luò)編程,需要的可以了解一下2022-08-08

