寫在前面
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