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è)置被動(dòng)模式
//ftp設(shè)置ip,端口
ftpClient.connect(costomDefineData.getQxjIp(), Integer.parseInt(costomDefineData.getQxjPort()));
//設(shè)置調(diào)用為被動(dòng)模式
ftpClient.enterLocalPassiveMode();
//ftpClient.enterLocalActiveMode(); 設(shè)置為主動(dòng)模式
//設(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) {
//獲取到對(duì)應(yīng)的FTP文件 這是獲取對(duì)應(yīng)文件夾下全部文件
FTPFile[] files = ftpClient.listFiles();
System.out.println("獲取文件個(gè)數(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 {
// 斷開(kāi)連接代碼省略
}
}
}
以上就是SpringBoot整合FTP實(shí)現(xiàn)文件傳輸?shù)牟襟E的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot FTP文件傳輸?shù)馁Y料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring中@Service注解的作用與@Controller和@RestController之間區(qū)別
這篇文章主要介紹了Spring中@Service注解的作用與@Controller和@RestController之間的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-03-03
HashMap的get()方法的NullPointerException問(wèn)題
這篇文章主要介紹了HashMap的get()方法的NullPointerException問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
java同步器AQS架構(gòu)AbstractQueuedSynchronizer原理解析下
這篇文章主要為大家介紹了java同步器AQS架構(gòu)AbstractQueuedSynchronizer原理解析下,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
Java開(kāi)發(fā)中的volatile你必須要了解一下

