java如何使用ftp下載遠(yuǎn)程服務(wù)器文件
java使用ftp下載遠(yuǎn)程服務(wù)器文件
第一種方法
連接中帶有賬號(hào)密碼直接遠(yuǎn)程下載:
public Result<?> download(){ //進(jìn)行下載文件---------------------------------開(kāi)始 //遠(yuǎn)程服務(wù)器下載地址 String fileurl = "ftp://superAdmin:superAdmin123@192.168.1.8:8888/usr/mp4pt/rec/CHN0/10110120011100/00001.mp4"; //文件物理路徑 String realfilepath = "F:/rhsftdemo/00001.mp4"; //本地中新建文件夾目錄 File file = new File("F:/rhsftdemo/"); if (!file.exists()) { file.mkdirs(); } log.info("存儲(chǔ)路徑realfilepath:" + realfilepath + "下載路徑:" + fileurl); download(fileurl,realfilepath); return Result.ok(); } private static final int BUFFER_SIZE = 4096; public void download(String ftpUrl,String savePath){ long startTime = System.currentTimeMillis(); // String ftpUrl = "ftp://ftp.f-secure.com/misc/unixutil/skeysrcs.zip"; String file = ""; // name of the file which has to be download String host = ""; // ftp server String user = ""; // user name of the ftp server String pass = ""; // password of the ftp server // String savePath = "F:\\skeysrcs.zip"; ftpUrl = String.format(ftpUrl, user, pass, host); System.out.println("Connecting to FTP server"); try { URL url = new URL(ftpUrl); URLConnection conn = url.openConnection(); InputStream inputStream = conn.getInputStream(); long filesize = conn.getContentLength(); System.out.println("Size of the file to download in kb is:-" + filesize / 1024); FileOutputStream outputStream = new FileOutputStream(savePath); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } long endTime = System.currentTimeMillis(); System.out.println("File downloaded"); System.out.println("Download time in sec. is:-" + (endTime - startTime) / 1000); outputStream.close(); inputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } }
第二種方法
通過(guò)賬號(hào)密碼登錄后進(jìn)行下載:
public Result<?> download(){ //進(jìn)行下載文件---------------------------------開(kāi)始 //遠(yuǎn)程服務(wù)器下載地址 String fileurl = "ftp://192.168.1.8:8888/usr/mp4pt/rec/CHN0/20220420095100/00001.mp4"; //本地文件物理路徑 String realfilepath = "F:/rhsftdemo/00001.mp4"; //本地中新建文件夾目錄 File file = new File("F:/rhsftdemo/"); if (!file.exists()) { file.mkdirs(); } log.info("存儲(chǔ)路徑realfilepath:" + realfilepath + "下載路徑:" + fileurl); boolean bb = retepasvfile(realfilepath,fileurl,"00001.mp4"); return Result.ok(); } /** * 本地保存 * * @param localurl 本地文件物理路徑 * @param hosturl 遠(yuǎn)程服務(wù)器下載地址 * @param filename 文件名稱(chēng) * * @return */ public synchronized boolean retepasvfile(String localurl, String hosturl, String filename) { FTPClient ftp = new FTPClient(); boolean re = false; try { log.info("本地存儲(chǔ)路徑==:" + localurl); File file = FileUtil.getFile(localurl); ftp.setConnectTimeout(10000); ftp.setDataTimeout(10000); ftp.connect("192.168.1.8", 21); if (!org.springframework.util.StringUtils.isEmpty("admin") && !org.springframework.util.StringUtils.isEmpty("superAdmin123") && "superAdmin" != "null" && "superAdmin123" != "null") { //這里輸入賬號(hào)密碼 ftp.login("superAdmin", "superAdmin123"); } else { log.info("開(kāi)始鏈接ftp:"); ftp.login("anonymous", "anonymous"); } //ftp.bin(); // String str=ftp.pwd(); ftp.enterLocalPassiveMode(); //被動(dòng)模式 ftp.setControlKeepAliveTimeout(60); ftp.setFileType(FTP.BINARY_FILE_TYPE); boolean b = ftp.changeWorkingDirectory(hosturl.substring(0, hosturl.lastIndexOf("/"))); log.info("pwd:" + b + "---切換目錄:" + hosturl.substring(0, hosturl.lastIndexOf("/"))); long locationsize = file.length();//服務(wù)器上文件的大小 if (b) { boolean res = false; res = ftp.retrieveFile(filename, new FileOutputStream(file)); if (res) { //ftp.disconnect(); log.info("文件下載完成"); re = true; } else { log.info("文件下載失敗"); //ftp.disconnect(); } } } catch (Exception e) { e.getStackTrace(); log.error("retepasvfileSamba:e=3" + e); } finally { try { ftp.disconnect(); } catch (Exception e) { } } return re; }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java?json轉(zhuǎn)換實(shí)體類(lèi)(JavaBean)及實(shí)體類(lèi)(JavaBean)轉(zhuǎn)換json代碼示例
這篇文章主要介紹了兩種常見(jiàn)的JSON與Java實(shí)體類(lèi)相互轉(zhuǎn)換的方法,分別是使用庫(kù)Jackson、Gson、Fastjson和在線工具,無(wú)論是將JSON轉(zhuǎn)換為Java實(shí)體類(lèi)還是將Java實(shí)體類(lèi)轉(zhuǎn)換為JSON,這些方法都能顯著簡(jiǎn)化開(kāi)發(fā)過(guò)程,需要的朋友可以參考下2024-12-12MyBatis中<collection>標(biāo)簽的多種用法
collection標(biāo)簽是處理一對(duì)多關(guān)系的關(guān)鍵工具,它能夠?qū)⒉樵兘Y(jié)果巧妙地映射到Java對(duì)象的集合屬性中,本文主要介紹了MyBatis中<collection>標(biāo)簽的多種用法,感興趣的可以了解一下2025-04-04Java、C++中子類(lèi)對(duì)父類(lèi)函數(shù)覆蓋的可訪問(wèn)性縮小的區(qū)別介紹
這篇文章主要給大家介紹了關(guān)于Java、C++中子類(lèi)對(duì)父類(lèi)函數(shù)覆蓋的可訪問(wèn)性縮小的區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01SpringBoot操作MaxComputer方式(保姆級(jí)教程)
這篇文章主要介紹了SpringBoot操作MaxComputer方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03Java中的this、super、final關(guān)鍵字詳解
這篇文章主要介紹了Java中的this、super、final關(guān)鍵字詳解,它在方法內(nèi)部使用,表示這個(gè)方法所屬對(duì)象的引用,它在構(gòu)造器內(nèi)部使用,表示該構(gòu)造器正在初始化的對(duì)象,this 可以調(diào)用類(lèi)的屬性、方法和構(gòu)造器,需要的朋友可以參考下2023-09-09詳解如何在項(xiàng)目中應(yīng)用SpringSecurity權(quán)限控制
本文主要介紹了如何在項(xiàng)目中應(yīng)用SpringSecurity權(quán)限控制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06一篇文章教你使用SpringBoot如何實(shí)現(xiàn)定時(shí)任務(wù)
這篇文章主要給大家介紹了關(guān)于如何利用一篇文章教你使用SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10