亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot項(xiàng)目集成FTP的方法步驟

 更新時(shí)間:2021年10月28日 11:07:55   作者:Ijiran  
本文主要介紹了SpringBoot項(xiàng)目集成FTP的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

寫在前面

FTP是一個(gè)文件傳輸協(xié)議,被開(kāi)發(fā)人員廣泛用于在互聯(lián)網(wǎng)中文件傳輸?shù)囊惶讟?biāo)準(zhǔn)協(xié)議。
而我們通常在開(kāi)發(fā)過(guò)程中也要通過(guò)FTP來(lái)搭建文件系統(tǒng),用于存儲(chǔ)系統(tǒng)文件等。
目前正值SpringBoot熱潮,所以我們接下來(lái)會(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,圖形化界面,有中文版,操作起來(lái)很簡(jiǎn)單。

開(kāi)始集成

引入相關(guān)jar包

這里我們對(duì)FTP相關(guān)的組件包使用的是edtFTPj,其實(shí)之前很多人都選擇的是Java自帶的包來(lái)實(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;
    }

    /**
     * 斷開(kāi)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("沒(méi)有輸入文件路徑");
            }
        } 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("沒(méi)有輸入文件路徑");
            }
        } 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("沒(méi)有輸入文件路徑");
            }
        } 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;
    }

    /**
     * 斷開(kāi)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("沒(méi)有輸入文件路徑");
            }
        } 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("沒(méi)有輸入文件路徑");
            }
        } 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("沒(méi)有輸入文件路徑");
            }
        } 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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java UDP通信客戶端與服務(wù)器端實(shí)例分析

    java UDP通信客戶端與服務(wù)器端實(shí)例分析

    這篇文章主要介紹了java UDP通信客戶端與服務(wù)器端,結(jié)合實(shí)例形式分析了java基于UDP通信的客戶端與服務(wù)器端具體實(shí)現(xiàn)技巧及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2020-01-01
  • Intellij IDEA 最全超實(shí)用快捷鍵整理(長(zhǎng)期更新)

    Intellij IDEA 最全超實(shí)用快捷鍵整理(長(zhǎng)期更新)

    這篇文章主要介紹了Intellij IDEA 最全實(shí)用快捷鍵整理(長(zhǎng)期更新),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • SpringSecurity入門使用教程

    SpringSecurity入門使用教程

    ? Spring Security是一個(gè)功能強(qiáng)大且高度可定制的身份驗(yàn)證和訪問(wèn)控制框架,它是用于保護(hù)基于Spring的應(yīng)用程序的實(shí)際標(biāo)準(zhǔn),這篇文章主要介紹了如何使用SpringSecurity,需要的朋友可以參考下
    2023-12-12
  • java語(yǔ)言與平臺(tái)基礎(chǔ)知識(shí)點(diǎn)

    java語(yǔ)言與平臺(tái)基礎(chǔ)知識(shí)點(diǎn)

    在本篇文章里小編給大家整理的是一篇關(guān)于java語(yǔ)言與平臺(tái)基礎(chǔ)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。
    2019-11-11
  • Mybatis中<if>和<choose>的區(qū)別及“=”判斷方式

    Mybatis中<if>和<choose>的區(qū)別及“=”判斷方式

    這篇文章主要介紹了Mybatis中<if>和<choose>的區(qū)別及“=”判斷方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 關(guān)于maven下載慢的問(wèn)題

    關(guān)于maven下載慢的問(wèn)題

    這篇文章主要介紹了關(guān)于maven下載慢的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • idea直接修改新的git地址的方法(圖文)

    idea直接修改新的git地址的方法(圖文)

    這篇文章主要介紹了idea直接修改新的git地址的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Java分布式鎖由淺入深介紹

    Java分布式鎖由淺入深介紹

    這篇文章主要介紹了Java分布式鎖,數(shù)據(jù)庫(kù)實(shí)現(xiàn)分布式鎖方式比較多,如悲觀鎖(查詢時(shí)增加for?update)、樂(lè)觀鎖(通過(guò)version字段)、增加一個(gè)表記錄鎖信息等。因?yàn)橐蕾囉跀?shù)據(jù)庫(kù),比較好理解,但是也存在一些問(wèn)題
    2023-03-03
  • 在IntelliJ IDEA中使用gulp的方法步驟(圖文)

    在IntelliJ IDEA中使用gulp的方法步驟(圖文)

    這篇文章主要介紹了在IntelliJ IDEA中使用gulp的方法步驟(圖文),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • spring security中的默認(rèn)登錄頁(yè)源碼跟蹤

    spring security中的默認(rèn)登錄頁(yè)源碼跟蹤

    原來(lái)Spring Security有一個(gè)默認(rèn)的WebSecurityConfigurerAdapter,發(fā)現(xiàn)其中有一個(gè)init方法,于是在這個(gè)方法打了斷點(diǎn),在應(yīng)用啟動(dòng)的時(shí)候進(jìn)行跟蹤,這篇文章主要介紹了spring security之 默認(rèn)登錄頁(yè)源碼跟蹤,需要的朋友可以參考下
    2021-11-11

最新評(píng)論