SpringBoot接入ftp/ftps并上傳文件和配置的代碼指南
1. 整體描述
接入ftp服務(wù)器,在springboot上實現(xiàn)起來也不算復(fù)雜,本文主要講下如何在springboot下接入ftp服務(wù)上傳文件,并對出現(xiàn)的問題做一些記錄,ftp服務(wù)的參數(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相關(guān)的類
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服務(wù)器已存在該文件,則不創(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 關(guān)閉連接
/**
* 關(guān)閉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服務(wù)器已存在該文件,則不創(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)建遠程服務(wù)器目錄
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);
// 關(guān)閉ftp連接
closeFTP(ftpsClient);
}
2.7 運行結(jié)果

log顯示上傳成功,去ftp對應(yīng)目錄也能看到上傳的文件。
3. FTP服務(wù)器配置
服務(wù)器相關(guān)配置也需要修改一下,要不登錄上傳等會報錯。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 設(shè)置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服務(wù)
systemctl restart vsftpd
3.3 設(shè)置FTPS連接為隱式連接
FTPS支持顯示連接和隱式連接兩種,如果需要隱式連接,修改配置文件。添加如下配置:
implicit_ssl=YES
然后重啟ftp服務(wù)即可。
4. 總結(jié)
以上就是配置和連接FTP/FTPS的基本操作,當(dāng)然還有一些復(fù)雜的操作和配置,就自己探索吧。
到此這篇關(guān)于SpringBoot接入ftp/ftps并上傳文件和配置的代碼指南的文章就介紹到這了,更多相關(guān)SpringBoot接入ftp/ftps并上傳和配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot使用Scheduling實現(xiàn)動態(tài)增刪啟停定時任務(wù)教程
這篇文章主要介紹了springboot使用Scheduling實現(xiàn)動態(tài)增刪啟停定時任務(wù)教程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
java.lang.FileNotFoundException 異常的正確解決方法(親測有效)
java.io.FileNotFoundException是一個在文件操作過程中常見的異常,它屬于IOException的一個子類,這篇文章主要介紹了java.lang.FileNotFoundException 異常的正確解決方法(親測有效),需要的朋友可以參考下2024-01-01
SpringBoot+thymeleaf+Echarts+Mysql 實現(xiàn)數(shù)據(jù)可視化讀取的示例
本文主要介紹了SpringBoot+thymeleaf+Echarts+Mysql 實現(xiàn)數(shù)據(jù)可視化讀取的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
解決swagger2中@ApiResponse的response不起作用
這篇文章主要介紹了解決swagger2中@ApiResponse的response不起作用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06

