SpringBoot項(xiàng)目集成FTP的方法步驟
寫在前面
FTP是一個(gè)文件傳輸協(xié)議,被開(kāi)發(fā)人員廣泛用于在互聯(lián)網(wǎng)中文件傳輸?shù)囊惶讟?biāo)準(zhǔn)協(xié)議。
而我們通常在開(kāi)發(fā)過(guò)程中也要通過(guò)FTP來(lái)搭建文件系統(tǒng),用于存儲(chǔ)系統(tǒng)文件等。
目前正值SpringBoot熱潮,所以我們接下來(lái)會(huì)一起學(xué)習(xí)一下SpringBoot如何集成FTP,以及相關(guān)的FTP組件包,還有其主要提供的幾個(gè)方法。
當(dāng)然在這系列文章結(jié)尾,我們還會(huì)給出確切的FTP操作工具類,算是一些小成果,希望和大家共勉。
FTP相關(guān)軟件安裝
我在此就不介紹如何安裝FTP了,但是我可以推薦給大家一些軟件作為選擇。
Linux版本,推薦使用vsftpd進(jìn)行搭建FTP,只需要改指定的幾個(gè)配置,添加上用戶即可。
Windows版本,推薦使用Serv-U進(jìn)行搭建FTP,圖形化界面,有中文版,操作起來(lái)很簡(jiǎn)單。
開(kāi)始集成
引入相關(guān)jar包
這里我們對(duì)FTP相關(guān)的組件包使用的是edtFTPj,其實(shí)之前很多人都選擇的是Java自帶的包來(lái)實(shí)現(xiàn)FTP功能的。
在我們的SpringBoot項(xiàng)目中pom.xml下添加以下依賴。
<dependency> <groupId>com.enterprisedt</groupId> <artifactId>edtFTPj</artifactId> <version>1.5.3</version> </dependency>
更新maven進(jìn)行引入,然后我們進(jìn)行下一步。
引入FTPUtils.java和FTPHelper.java
引入兩個(gè)工具類。
我這里先貢獻(xiàn)一下代碼,請(qǐng)大家酌情作為參考。
/** * Ftp 工具類 */ public class FtpHelper { private FTPClient ftp; public FtpHelper() { } /** * 初始化Ftp信息 * * @param ftpServer ftp服務(wù)器地址 * @param ftpPort Ftp端口號(hào) * @param ftpUsername ftp 用戶名 * @param ftpPassword ftp 密碼 */ public FtpHelper(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) { connect(ftpServer, ftpPort, ftpUsername, ftpPassword); } /** * 連接到ftp * * @param ftpServer ftp服務(wù)器地址 * @param ftpPort Ftp端口號(hào) * @param ftpUsername ftp 用戶名 * @param ftpPassword ftp 密碼 */ public void connect(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) { ftp = new FTPClient(); try { ftp.setControlEncoding("UTF-8"); ftp.setRemoteHost(ftpServer); ftp.setRemotePort(ftpPort); ftp.setTimeout(6000); ftp.setConnectMode(FTPConnectMode.ACTIVE); ftp.connect(); ftp.login(ftpUsername, ftpPassword); ftp.setType(FTPTransferType.BINARY); } catch (Exception e) { e.printStackTrace(); ftp = null; } } /** * 更改ftp路徑 * * @param ftp * @param dirName * @return */ public boolean checkDirectory(FTPClient ftp, String dirName) { boolean flag; try { ftp.chdir(dirName); flag = true; } catch (Exception e) { e.printStackTrace(); flag = false; } return flag; } /** * 斷開(kāi)ftp鏈接 */ public void disconnect() { try { if (ftp.connected()) { ftp.quit(); } } catch (Exception e) { e.printStackTrace(); } } /** * 讀取ftp文件流 * * @param filePath ftp文件路徑 * @return s * @throws Exception */ public InputStream downloadFile(String filePath) throws Exception { InputStream inputStream = null; String fileName = ""; filePath = StringUtils.removeStart(filePath, "/"); int len = filePath.lastIndexOf("/"); if (len == -1) { if (filePath.length() > 0) { fileName = filePath; } else { throw new Exception("沒(méi)有輸入文件路徑"); } } else { fileName = filePath.substring(len + 1); String type = filePath.substring(0, len); String[] typeArray = type.split("/"); for (String s : typeArray) { ftp.chdir(s); } } byte[] data; try { data = ftp.get(fileName); inputStream = new ByteArrayInputStream(data); } catch (Exception e) { e.printStackTrace(); } return inputStream; } /** * 上傳文件到ftp * * @param file 文件對(duì)象 * @param filePath 上傳的路徑 * @throws Exception */ public void uploadFile(File file, String filePath) throws Exception { InputStream inStream = new FileInputStream(file); uploadFile(inStream, filePath); } /** * 上傳文件到ftp * * @param inStream 上傳的文件流 * @param filePath 上傳路徑 * @throws Exception */ public void uploadFile(InputStream inStream, String filePath) throws Exception { if (inStream == null) { return; } String fileName = ""; filePath = StringUtils.removeStart(filePath, "/"); int len = filePath.lastIndexOf("/"); if (len == -1) { if (filePath.length() > 0) { fileName = filePath; } else { throw new Exception("沒(méi)有輸入文件路徑"); } } else { fileName = filePath.substring(len + 1); String type = filePath.substring(0, len); String[] typeArray = type.split("/"); for (String s : typeArray) { if (!checkDirectory(ftp, s)) { ftp.mkdir(s); } } } ftp.put(inStream, fileName); } /** * 刪除ftp文件 * * @param filePath 文件路徑 * @throws Exception */ public void deleteFile(String filePath) throws Exception { String fileName = ""; filePath = StringUtils.removeStart(filePath, "/"); int len = filePath.lastIndexOf("/"); if (len == -1) { if (filePath.length() > 0) { fileName = filePath; } else { throw new Exception("沒(méi)有輸入文件路徑"); } } else { fileName = filePath.substring(len + 1); String type = filePath.substring(0, len); String[] typeArray = type.split("/"); for (String s : typeArray) { if (checkDirectory(ftp, s)) { ftp.chdir(s); } } } ftp.delete(fileName); } /** * 切換目錄 * * @param path * @throws Exception */ public void changeDirectory(String path) { if (!ValidateUtils.isEmpty(path)) { try { ftp.chdir(path); } catch (Exception e) { e.printStackTrace(); } } } }
/** * Ftp 工具類 */ public class FtpHelper { private FTPClient ftp; public FtpHelper() { } /** * 初始化Ftp信息 * * @param ftpServer ftp服務(wù)器地址 * @param ftpPort Ftp端口號(hào) * @param ftpUsername ftp 用戶名 * @param ftpPassword ftp 密碼 */ public FtpHelper(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) { connect(ftpServer, ftpPort, ftpUsername, ftpPassword); } /** * 連接到ftp * * @param ftpServer ftp服務(wù)器地址 * @param ftpPort Ftp端口號(hào) * @param ftpUsername ftp 用戶名 * @param ftpPassword ftp 密碼 */ public void connect(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) { ftp = new FTPClient(); try { ftp.setControlEncoding("UTF-8"); ftp.setRemoteHost(ftpServer); ftp.setRemotePort(ftpPort); ftp.setTimeout(6000); ftp.setConnectMode(FTPConnectMode.ACTIVE); ftp.connect(); ftp.login(ftpUsername, ftpPassword); ftp.setType(FTPTransferType.BINARY); } catch (Exception e) { e.printStackTrace(); ftp = null; } } /** * 更改ftp路徑 * * @param ftp * @param dirName * @return */ public boolean checkDirectory(FTPClient ftp, String dirName) { boolean flag; try { ftp.chdir(dirName); flag = true; } catch (Exception e) { e.printStackTrace(); flag = false; } return flag; } /** * 斷開(kāi)ftp鏈接 */ public void disconnect() { try { if (ftp.connected()) { ftp.quit(); } } catch (Exception e) { e.printStackTrace(); } } /** * 讀取ftp文件流 * * @param filePath ftp文件路徑 * @return s * @throws Exception */ public InputStream downloadFile(String filePath) throws Exception { InputStream inputStream = null; String fileName = ""; filePath = StringUtils.removeStart(filePath, "/"); int len = filePath.lastIndexOf("/"); if (len == -1) { if (filePath.length() > 0) { fileName = filePath; } else { throw new Exception("沒(méi)有輸入文件路徑"); } } else { fileName = filePath.substring(len + 1); String type = filePath.substring(0, len); String[] typeArray = type.split("/"); for (String s : typeArray) { ftp.chdir(s); } } byte[] data; try { data = ftp.get(fileName); inputStream = new ByteArrayInputStream(data); } catch (Exception e) { e.printStackTrace(); } return inputStream; } /** * 上傳文件到ftp * * @param file 文件對(duì)象 * @param filePath 上傳的路徑 * @throws Exception */ public void uploadFile(File file, String filePath) throws Exception { InputStream inStream = new FileInputStream(file); uploadFile(inStream, filePath); } /** * 上傳文件到ftp * * @param inStream 上傳的文件流 * @param filePath 上傳路徑 * @throws Exception */ public void uploadFile(InputStream inStream, String filePath) throws Exception { if (inStream == null) { return; } String fileName = ""; filePath = StringUtils.removeStart(filePath, "/"); int len = filePath.lastIndexOf("/"); if (len == -1) { if (filePath.length() > 0) { fileName = filePath; } else { throw new Exception("沒(méi)有輸入文件路徑"); } } else { fileName = filePath.substring(len + 1); String type = filePath.substring(0, len); String[] typeArray = type.split("/"); for (String s : typeArray) { if (!checkDirectory(ftp, s)) { ftp.mkdir(s); } } } ftp.put(inStream, fileName); } /** * 刪除ftp文件 * * @param filePath 文件路徑 * @throws Exception */ public void deleteFile(String filePath) throws Exception { String fileName = ""; filePath = StringUtils.removeStart(filePath, "/"); int len = filePath.lastIndexOf("/"); if (len == -1) { if (filePath.length() > 0) { fileName = filePath; } else { throw new Exception("沒(méi)有輸入文件路徑"); } } else { fileName = filePath.substring(len + 1); String type = filePath.substring(0, len); String[] typeArray = type.split("/"); for (String s : typeArray) { if (checkDirectory(ftp, s)) { ftp.chdir(s); } } } ftp.delete(fileName); } /** * 切換目錄 * * @param path * @throws Exception */ public void changeDirectory(String path) { if (!ValidateUtils.isEmpty(path)) { try { ftp.chdir(path); } catch (Exception e) { e.printStackTrace(); } } } }
如何使用
public static void main(String[] args) { try { // 從ftp下載文件 FtpHelper ftp = new FtpHelper("127.0.0.1", 21, "root", "123456"); File file = new File("D:\1.doc"); ftp.uploadFile(file, "test/weradsfad2.doc"); ftp.disconnect(); } catch (Exception e) { e.printStackTrace(); } }
到此這篇關(guān)于SpringBoot項(xiàng)目集成FTP的方法步驟的文章就介紹到這了,更多相關(guān)SpringBoot集成FTP內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot集成Hutool防止XSS攻擊的兩種解決方法
- SpringBoot使用hutool-captcha實(shí)現(xiàn)驗(yàn)證碼生成與驗(yàn)證
- SpringBoot整合Hutool實(shí)現(xiàn)文件上傳的使用示例
- SpringBoot 項(xiàng)目使用hutool 工具進(jìn)行 http 接口調(diào)用的處理方法
- SpringBoot+Hutool+thymeleaf完成導(dǎo)出Excel的實(shí)現(xiàn)方法
- SpringBoot集成FTP與SFTP連接池流程
- SpringBoot使用hutool操作FTP的詳細(xì)過(guò)程
相關(guān)文章
java UDP通信客戶端與服務(wù)器端實(shí)例分析
這篇文章主要介紹了java UDP通信客戶端與服務(wù)器端,結(jié)合實(shí)例形式分析了java基于UDP通信的客戶端與服務(wù)器端具體實(shí)現(xiàn)技巧及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-01-01Intellij IDEA 最全超實(shí)用快捷鍵整理(長(zhǎng)期更新)
這篇文章主要介紹了Intellij IDEA 最全實(shí)用快捷鍵整理(長(zhǎng)期更新),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02java語(yǔ)言與平臺(tái)基礎(chǔ)知識(shí)點(diǎn)
在本篇文章里小編給大家整理的是一篇關(guān)于java語(yǔ)言與平臺(tái)基礎(chǔ)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。2019-11-11Mybatis中<if>和<choose>的區(qū)別及“=”判斷方式
這篇文章主要介紹了Mybatis中<if>和<choose>的區(qū)別及“=”判斷方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06在IntelliJ IDEA中使用gulp的方法步驟(圖文)
這篇文章主要介紹了在IntelliJ IDEA中使用gulp的方法步驟(圖文),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01spring security中的默認(rèn)登錄頁(yè)源碼跟蹤
原來(lái)Spring Security有一個(gè)默認(rèn)的WebSecurityConfigurerAdapter,發(fā)現(xiàn)其中有一個(gè)init方法,于是在這個(gè)方法打了斷點(diǎn),在應(yīng)用啟動(dòng)的時(shí)候進(jìn)行跟蹤,這篇文章主要介紹了spring security之 默認(rèn)登錄頁(yè)源碼跟蹤,需要的朋友可以參考下2021-11-11