SpringBoot項(xiàng)目集成FTP的方法步驟
寫在前面
FTP是一個(gè)文件傳輸協(xié)議,被開發(fā)人員廣泛用于在互聯(lián)網(wǎng)中文件傳輸?shù)囊惶讟?biāo)準(zhǔn)協(xié)議。
而我們通常在開發(fā)過程中也要通過FTP來搭建文件系統(tǒng),用于存儲(chǔ)系統(tǒng)文件等。
目前正值SpringBoot熱潮,所以我們接下來會(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,圖形化界面,有中文版,操作起來很簡(jiǎn)單。
開始集成
引入相關(guān)jar包
這里我們對(duì)FTP相關(guān)的組件包使用的是edtFTPj,其實(shí)之前很多人都選擇的是Java自帶的包來實(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;
}
/**
* 斷開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("沒有輸入文件路徑");
}
} 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("沒有輸入文件路徑");
}
} 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("沒有輸入文件路徑");
}
} 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;
}
/**
* 斷開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("沒有輸入文件路徑");
}
} 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("沒有輸入文件路徑");
}
} 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("沒有輸入文件路徑");
}
} 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ān)文章
深入淺析Spring 的aop實(shí)現(xiàn)原理
AOP(Aspect-OrientedProgramming,面向方面編程),可以說是OOP(Object-Oriented Programing,面向?qū)ο缶幊蹋┑难a(bǔ)充和完善。本文給大家介紹Spring 的aop實(shí)現(xiàn)原理,感興趣的朋友一起學(xué)習(xí)吧2016-03-03
Java IO流學(xué)習(xí)總結(jié)之文件傳輸基礎(chǔ)
這篇文章主要介紹了Java IO流學(xué)習(xí)總結(jié)之文件傳輸基礎(chǔ),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java io流的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04
springBoot靜態(tài)資源加載不到,并且配置了也不生效問題及解決
這篇文章總結(jié)了一個(gè)在Spring Boot 2.6.x版本中,由于路徑匹配策略改變導(dǎo)致靜態(tài)資源無法加載的問題,并提供了解決方案:通過配置類或在配置文件中設(shè)置路徑匹配策略為AntPathMatcher,或者直接降級(jí)Spring Boot版本2025-02-02
Java中system.exit(0) 和 system.exit(1)區(qū)別
本文主要介紹了Java中system.exit(0) 和 system.exit(1)區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
java應(yīng)用開發(fā)之JVM運(yùn)行時(shí)內(nèi)存分析
這篇文章主要介紹了java應(yīng)用開發(fā)之JVM運(yùn)行時(shí)內(nèi)存,文中附含圖文示例內(nèi)容分析非常簡(jiǎn)要,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
idea?2024使用Maven創(chuàng)建Java?Web項(xiàng)目詳細(xì)圖文教程
這篇文章主要給大家介紹了關(guān)于idea?2024使用Maven創(chuàng)建Java?Web項(xiàng)目的相關(guān)資料,介紹了如何使用Maven創(chuàng)建一個(gè)Spring?MVC項(xiàng)目,并配置Tomcat服務(wù)器以運(yùn)行一個(gè)簡(jiǎn)單的Helloworld?JSP頁面,需要的朋友可以參考下2024-12-12

