SpringBoot接入ftp/ftps并上傳文件和配置的代碼指南
1. 整體描述
接入ftp服務器,在springboot上實現(xiàn)起來也不算復雜,本文主要講下如何在springboot下接入ftp服務上傳文件,并對出現(xiàn)的問題做一些記錄,ftp服務的參數(shù)配置等
2. 具體實現(xiàn)
2.1 引入pom
在springboot的配置文件引入:
<dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.8.0</version> </dependency>
apache的包,其中有ftp相關的類
2.2 創(chuàng)建ftps連接
其中使用FTPClient創(chuàng)建的是FTP連接,F(xiàn)TPSClient創(chuàng)建的是FTPS連接,我這里創(chuàng)建了FTPS連接
/** * 打開FTP連接 * * @param hostname hostname * @param port port * @param username username * @param password password */ public static FTPSClient openFTP(String hostname, int port, String username, String password) { // 顯示方式傳false,隱式方式傳true FTPSClient ftpsClient = new FTPSClient(true); ftpsClient.setControlEncoding("UTF-8"); try { System.out.println("ftpsClient connect"); ftpsClient.connect(hostname, port); boolean flag = ftpsClient.login(username, password); // 切換到被動模式 ftpsClient.setControlEncoding("UTF-8"); ftpsClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpsClient.enterLocalPassiveMode(); ftpsClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); ftpsClient.execPROT("P"); ftpsClient.setAuthValue("TLS"); if (!flag) { System.out.println("login failed"); return null; } String path = ftpsClient.printWorkingDirectory(); System.out.println("path:" + path); } catch (Exception e) { e.printStackTrace(); } return ftpsClient; }
2.3 上傳文件
/** * 創(chuàng)建多層目錄文件,如果有ftp服務器已存在該文件,則不創(chuàng)建,如果無,則創(chuàng)建 * * @param filePath 上傳文件本地路徑 * @param ftpPath 上傳文件FTP路徑 * @param ftpsClient ftp客戶端 */ public static void uploadFileToFTP(String filePath, String ftpPath, FTPSClient ftpsClient) { try { File file = new File(filePath); boolean changeFlag = ftpsClient.changeWorkingDirectory(ftpPath); System.out.println("changeFlag:" + changeFlag); if (!changeFlag) { boolean createFlag = createDirectory(ftpPath, ftpsClient); System.out.println("createFlag:" + createFlag); } String uploadPath = ftpsClient.printWorkingDirectory(); System.out.println("uploadPath:" + uploadPath); ftpsClient.setFileType(FTPClient.BINARY_FILE_TYPE); boolean uploadFlag = ftpsClient.storeUniqueFile(file.getName(), new FileInputStream(file)); System.out.println("uploadFlag:" + uploadFlag); System.out.println("uploadFlag:" + ftpsClient.getReplyString()); } catch (Exception e) { e.printStackTrace(); } }
2.4 關閉連接
/** * 關閉FTP連接 * * @param ftpsClient ftp客戶端 */ public static void closeFTP(FTPSClient ftpsClient) { try { ftpsClient.logout(); ftpsClient.disconnect(); } catch (Exception e) { e.printStackTrace(); } }
2.5 創(chuàng)建FTP目錄
/** * 創(chuàng)建多層目錄文件,如果有ftp服務器已存在該文件,則不創(chuàng)建,如果無,則創(chuàng)建 * * @param remote 目錄 * @param ftpsClient ftp客戶端 */ public static boolean createDirectory(String remote, FTPSClient ftpsClient) { String directory = remote; try { if (!remote.endsWith("/")) { directory = directory + "/"; } // 如果遠程目錄不存在,則遞歸創(chuàng)建遠程服務器目錄 if (!directory.equals("/") && !ftpsClient.changeWorkingDirectory(directory)) { int start = 0; int end = 0; if (directory.startsWith("/")) { start = 1; } else { start = 0; } end = directory.indexOf("/", start); String paths = "", path = ""; while (true) { String subDirectory = remote.substring(start, end); path = path + "/" + subDirectory; // 目錄不存在就創(chuàng)建 if (!ftpsClient.changeWorkingDirectory(subDirectory)) { if (ftpsClient.makeDirectory(subDirectory)) { ftpsClient.changeWorkingDirectory(subDirectory); } } paths = paths + "/" + subDirectory; start = end + 1; end = directory.indexOf("/", start); // 檢查所有目錄是否創(chuàng)建完畢 if (end <= start) { break; } } } return true; } catch (Exception e) { e.printStackTrace(); } return false; }
2.6 main方法
public static void main(String[] args) { // 打開ftp連接 FTPSClient ftpsClient = openFTP("192.168.1.100", 10012, "test1", "123456"); if (null == ftpsClient) { return; } // 上傳文件 uploadFileToFTP("C:\\Users\\10187\\Desktop\\ftp.txt", "/TEST/PICTURE", ftpsClient); // 下載文件 downloadFileFromFTP(ftpsClient); // 關閉ftp連接 closeFTP(ftpsClient); }
2.7 運行結果
log顯示上傳成功,去ftp對應目錄也能看到上傳的文件。
3. FTP服務器配置
服務器相關配置也需要修改一下,要不登錄上傳等會報錯。FTP的配置文件一般在/etc/vsftpd下面,有個vsftpd.conf的文件
3.1 修改require_ssl_reuse參數(shù)
如果上傳文件的時候報錯:522 SSL connection failed; session reuse required: see require_ssl_reuse option in vsftpd.conf man page。
需要修改這個參數(shù),默認是YES,需要改成NO
3.2 設置FTPS連接
第一步:創(chuàng)建私鑰、證書
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/vsftpd/vsftpd_ssl_key.pem -out /etc/vsftpd/vsftpd_ssl_cert.pem
第二步:添加以下配置到配置文件
ssl_enable=YES ssl_tlsv1=YES ssl_sslv2=YES ssl_sslv3=YES allow_anon_ssl=NO force_local_data_ssl=YES force_local_logins_ssl=YES rsa_cert_file=/etc/vsftpd/vsftpd_ssl_cert.pem rsa_private_key_file=/etc/vsftpd/vsftpd_ssl_key.pem require_ssl_reuse=NO
第三步:重啟ftp服務
systemctl restart vsftpd
3.3 設置FTPS連接為隱式連接
FTPS支持顯示連接和隱式連接兩種,如果需要隱式連接,修改配置文件。添加如下配置:
implicit_ssl=YES
然后重啟ftp服務即可。
4. 總結
以上就是配置和連接FTP/FTPS的基本操作,當然還有一些復雜的操作和配置,就自己探索吧。
到此這篇關于SpringBoot接入ftp/ftps并上傳文件和配置的代碼指南的文章就介紹到這了,更多相關SpringBoot接入ftp/ftps并上傳和配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java統(tǒng)計一個字符串在另外一個字符串出現(xiàn)次數(shù)的方法
這篇文章主要介紹了Java統(tǒng)計一個字符串在另外一個字符串出現(xiàn)次數(shù)的方法,涉及java字符串遍歷、正則匹配等相關操作技巧,需要的朋友可以參考下2018-03-03已有的springcloud+mybatis項目升級為mybatis-plus的方法
這篇文章主要介紹了已有的springcloud+mybatis項目升級為mybatis-plus,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03Java?8函數(shù)式接口之BinaryOperator使用示例詳解
這篇文章主要大家介紹了Java?8函數(shù)式接口之BinaryOperator,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07idea2020.1最新版永久破解/pycharm也可用(步驟詳解)
這篇文章主要介紹了idea2020.1最新版永久破解/pycharm也可用,本文給大家分享簡單實現(xiàn)步驟,通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04