一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot項目集成FTP的方法步驟

SpringBoot項目集成FTP的方法步驟

2022-03-04 17:48Ijiran Java教程

本文主要介紹了SpringBoot項目集成FTP的方法步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

寫在前面

FTP是一個文件傳輸協議,被開發人員廣泛用于在互聯網中文件傳輸的一套標準協議。
而我們通常在開發過程中也要通過FTP來搭建文件系統,用于存儲系統文件等。
目前正值SpringBoot熱潮,所以我們接下來會一起學習一下SpringBoot如何集成FTP,以及相關的FTP組件包,還有其主要提供的幾個方法。

當然在這系列文章結尾,我們還會給出確切的FTP操作工具類,算是一些小成果,希望和大家共勉。

FTP相關軟件安裝

我在此就不介紹如何安裝FTP了,但是我可以推薦給大家一些軟件作為選擇。
Linux版本,推薦使用vsftpd進行搭建FTP,只需要改指定的幾個配置,添加上用戶即可。
Windows版本,推薦使用Serv-U進行搭建FTP,圖形化界面,有中文版,操作起來很簡單。

開始集成

引入相關jar包

這里我們對FTP相關的組件包使用的是edtFTPj,其實之前很多人都選擇的是Java自帶的包來實現FTP功能的。
在我們的SpringBoot項目中pom.xml下添加以下依賴。

?
1
2
3
4
5
<dependency>
    <groupId>com.enterprisedt</groupId>
    <artifactId>edtFTPj</artifactId>
    <version>1.5.3</version>
</dependency>

更新maven進行引入,然后我們進行下一步。

引入FTPUtils.java和FTPHelper.java

引入兩個工具類。

我這里先貢獻一下代碼,請大家酌情作為參考。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
 * Ftp 工具類
 */
public class FtpHelper {
 
    private FTPClient ftp;
 
    public FtpHelper() {
 
    }
 
    /**
     * 初始化Ftp信息
     *
     * @param ftpServer   ftp服務器地址
     * @param ftpPort     Ftp端口號
     * @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服務器地址
     * @param ftpPort     Ftp端口號
     * @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     文件對象
     * @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();
            }
        }
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
 * Ftp 工具類
 */
public class FtpHelper {
 
    private FTPClient ftp;
 
    public FtpHelper() {
 
    }
 
    /**
     * 初始化Ftp信息
     *
     * @param ftpServer   ftp服務器地址
     * @param ftpPort     Ftp端口號
     * @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服務器地址
     * @param ftpPort     Ftp端口號
     * @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     文件對象
     * @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();
            }
        }
    }
}

如何使用

?
1
2
3
4
5
6
7
8
9
10
11
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();
    }
}

到此這篇關于SpringBoot項目集成FTP的方法步驟的文章就介紹到這了,更多相關SpringBoot集成FTP內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://juejin.cn/post/7022817255779991589

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产毛片在线高清视频 | 清清草在线视频 | 亚洲成人aa | 国产目拍亚洲精品一区二区三区 | 精品视频久久久久 | 91啪在线观看国产在线 | 男人的私人影院 | 欧美日韩国产一区二区三区不卡 | 小寡妇水真多好紧 | 91频视| 国产伦精品一区二区三区免费迷 | 国产香蕉视频在线观看 | 久久99视热频国只有精品 | 欧美精品一区二区三区免费播放 | 亚洲aⅴ男人的天堂在线观看 | 精品视频一区二区三区 | 亚洲国产香蕉视频欧美 | 成人在线一区二区三区 | 免费观看一级特黄三大片视频 | 精品久久久久香蕉网 | 久久久精品免费免费直播 | 极品美女写真菠萝蜜视频 | 男人狂躁女人下面的视频免费 | 日本最大的黄色网站 | 草草视频免费看 | 久久久伊人影院 | 日本高清中文字幕 | 精品日本三级在线观看视频 | 天若有情1992国语版完整版 | 国产精品国语自产拍在线观看 | 欧美视频精品一区二区三区 | 国产草逼视频 | 波多野结衣同性系列698 | 国产亚洲精品激情一区二区三区 | 精品视频在线观看 | 2019aw网站 | 非洲黑女人性xxxx | 欧美另类杂交a | 99久久999久久久综合精品涩 | 午夜精品久久久久久久99蜜桃 | 久久影院中文字幕 |