SpringBoot整合FTP實(shí)現(xiàn)文件傳輸?shù)牟襟E
實(shí)現(xiàn)ftp文件傳輸?shù)牟襟E:
1.ftp綁定ip端口登錄
2.切換到指定地址
3.文件下載
4.關(guān)閉ftp連接
項(xiàng)目中使用的jar包
<!-- ftp包-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.9.0</version>
</dependency>
項(xiàng)目中使用ftp代碼:
public void getQxjFile() throws IOException {
FTPClient ftpClient = new FTPClient(); //創(chuàng)建FTP連接客戶端
ftpClient.enterLocalPassiveMode();// 設(shè)置被動模式
//ftp設(shè)置ip,端口
ftpClient.connect(costomDefineData.getQxjIp(), Integer.parseInt(costomDefineData.getQxjPort()));
//設(shè)置調(diào)用為被動模式
ftpClient.enterLocalPassiveMode();
//ftpClient.enterLocalActiveMode(); 設(shè)置為主動模式
//設(shè)置文件以二進(jìn)制文件模式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//ftp登錄
boolean loggedIn = ftpClient.login(costomDefineData.getQxjUserName(), costomDefineData.getQxjPassword());
if (loggedIn) {
System.out.println("登錄成功");
} else {
System.out.println("登錄失敗");
}
//切換到登錄后的文件夾 這里指定ftp服務(wù)器文件存放位置
boolean changed = ftpClient.changeWorkingDirectory("/");
if (changed) {
//獲取到對應(yīng)的FTP文件 這是獲取對應(yīng)文件夾下全部文件
FTPFile[] files = ftpClient.listFiles();
System.out.println("獲取文件個數(shù)" + files.length);
for (FTPFile file : files) {
if (file.isFile()) {
File localDir = new File(costomDefineData.getQxjFilePath() + YM + "/" + Day);
if (!localDir.exists()) {
localDir.mkdirs();
}
File localFile = new File(costomDefineData.getQxjFilePath() + YM + "/" + Day + "/" + file.getName());
if (!localFile.exists()) {
localFile.createNewFile();
}
//將ftp服務(wù)器上文件同步到本地
ftpClient.retrieveFile("/" + file.getName(), new FileOutputStream(localFile));
BufferedReader reader = new BufferedReader(new FileReader(localFile));
// 讀取文件內(nèi)容并解析
String line;
String result = "";
while ((line = reader.readLine()) != null) {
// 解析每一行的數(shù)據(jù)
result = result + line;
}
}
}
//實(shí)現(xiàn)ftp上文件刪除
boolean deleted = ftpClient.deleteFile("/" + file.getName());
}
//ftp用戶登出
ftpClient.logout();
//ftp去掉鏈接
ftpClient.disconnect();
}
用ftp實(shí)現(xiàn)上傳功能
public class FTPExample {
public static void main(String[] args) {
FTPClient ftpClient = new FTPClient();
// 連接和登錄代碼省略
try {
// 上傳文件
File localFile = new File("local-file.txt");
String remoteFile = "remote-file.txt";
FileInputStream inputStream = new FileInputStream(localFile);
boolean uploaded = ftpClient.storeFile(remoteFile, inputStream);
inputStream.close();
if (uploaded) {
System.out.println("文件上傳成功!");
} else {
System.out.println("文件上傳失??!");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 斷開連接代碼省略
}
}
}
以上就是SpringBoot整合FTP實(shí)現(xiàn)文件傳輸?shù)牟襟E的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot FTP文件傳輸?shù)馁Y料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java最簡潔數(shù)據(jù)結(jié)構(gòu)之冒泡排序快速理解
冒泡排序是編程中數(shù)據(jù)結(jié)構(gòu)繞不過的一個基礎(chǔ)點(diǎn),有關(guān)于冒泡排序的文章也有很多,但可能會比較繚亂未能理解,本章將一子u為簡潔明了的例圖帶你通關(guān)冒泡排序2021-11-11
Java實(shí)現(xiàn)紀(jì)元秒和本地日期時間互換的方法【經(jīng)典實(shí)例】
這篇文章主要介紹了Java實(shí)現(xiàn)紀(jì)元秒和本地日期時間互換的方法,結(jié)合具體實(shí)例形式分析了Java日期時間相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
Mybatis-plus多數(shù)據(jù)源配置的兩種方式總結(jié)
這篇文章主要為大家詳細(xì)介紹了Mybatis-plus中多數(shù)據(jù)源配置的兩種方式,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起了解一下2022-10-10
深入理解happens-before和as-if-serial語義
本文大部分整理自《Java并發(fā)編程的藝術(shù)》,溫故而知新,加深對基礎(chǔ)的理解程度。下面可以和小編來一起學(xué)習(xí)下2019-05-05

