FtpUtil.java
8.8 KB
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
211
212
213
214
215
216
217
218
219
package com.aigeo.util;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class FtpUtil {
// FTP服务器配置参数
private static final String SERVER = "193.112.177.8"; // FTP服务器地址
private static final int PORT = 21; // FTP服务器端口,默认21
private static final String USERNAME = "user1"; // FTP用户名
private static final String PASSWORD = "sz123321"; // FTP密码
// 本地文件路径及远程存储路径
private static final String LOCAL_FILE_PATH = "C:\\Users\\Lenovo\\Desktop\\sql.txt";
private static final String REMOTE_FILE_PATH = "/1/sql.txt";
// public static void upload(FtpDTO ftpDTO) {
// FTPClient ftpClient = new FTPClient();
// try {
// // 1. 连接FTP服务器
// System.out.println("正在连接到FTP服务器 " + ftpDTO.getFtpHost() + "...");
// ftpClient.connect(ftpDTO.getFtpHost(), PORT);
// int replyCode = ftpClient.getReplyCode();
// if (!FTPReply.isPositiveCompletion(replyCode)) {
// System.err.println("FTP服务器拒绝连接,响应代码:" + replyCode);
// return;
// }
// System.out.println("连接成功,准备登录...");
//
// // 2. 登录FTP服务器
// boolean success = ftpClient.login(ftpDTO.getUser(), ftpDTO.getPassword());
// if (!success) {
// System.err.println("FTP登录失败,请检查用户名和密码。");
// return;
// }
// System.out.println("登录成功!");
//
// // 3. 设置FTP客户端工作模式
// // 进入被动模式,以适应防火墙/NAT环境
// ftpClient.enterLocalPassiveMode();
// // 设置文件传输类型为二进制文件,确保上传文件内容正确
// ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//
// // 4. 上传文件
// System.out.println("开始上传文件:" + ftpDTO.getSourcePath());
// // 使用try-with-resources确保InputStream自动关闭
// try (InputStream inputStream = new FileInputStream(ftpDTO.getSourcePath())) {
// boolean uploadSuccess = ftpClient.storeFile(ftpDTO.getTargetPath(), inputStream);
// if (uploadSuccess) {
// System.out.println("文件上传成功!保存到:" + ftpDTO.getTargetPath());
// } else {
// System.err.println("文件上传失败,请检查文件路径和网络连接。");
// }
// }
//
// // 5. 登出并断开FTP连接
// ftpClient.logout();
// System.out.println("已登出FTP服务器。");
//
// } catch (IOException ex) {
// System.err.println("发生异常:" + ex.getMessage());
// ex.printStackTrace();
// } finally {
// // 6. 确保FTP连接被断开
// if (ftpClient.isConnected()) {
// try {
// ftpClient.disconnect();
// System.out.println("FTP连接已关闭。");
// } catch (IOException ex) {
// System.err.println("关闭FTP连接时发生异常:" + ex.getMessage());
// ex.printStackTrace();
// }
// }
// }
// }
// public static List<String> listFileNames(ApiConfig apiConfig, String directoryPath) {
// FTPClient ftpClient = new FTPClient();
// List<String> fileNames = new ArrayList<>();
//
// try {
// // 连接并登录FTP服务器
// ftpClient.connect(apiConfig.getApiUrl(), PORT);
// int replyCode = ftpClient.getReplyCode();
// if (!FTPReply.isPositiveCompletion(replyCode)) {
// return null;
// }
//
// boolean success = ftpClient.login(apiConfig.getApiId(), apiConfig.getApiKey());
// if (!success) {
// return null;
// }
//
// // 进入被动模式
// ftpClient.enterLocalPassiveMode();
//
// // 获取文件列表
// FTPFile[] files;
// if (directoryPath != null && !directoryPath.isEmpty()) {
// files = ftpClient.listFiles(directoryPath);
// } else {
// files = ftpClient.listFiles();
// }
//
// // 提取文件名称
// for (FTPFile file : files) {
// fileNames.add(file.getName());
// }
//
// return fileNames;
//
// } catch (IOException ex) {
// return null;
// } finally {
// // 确保连接被正确关闭
// if (ftpClient.isConnected()) {
// try {
// ftpClient.logout();
// ftpClient.disconnect();
// } catch (IOException ex) {
// System.err.println("关闭FTP连接时发生异常:" + ex.getMessage());
// ex.printStackTrace();
// }
// }
// }
// }
/**
* 获取FTP服务器当前目录下的文件列表
* @return FTPFile数组,包含目录中的文件和子目录信息
*/
// public static FTPFile[] getlistFiles() {
// return listFiles(null);
// }
/**
* 打印指定目录下的文件列表信息
* @param directoryPath 目录路径
*/
// public static void printFileList(String directoryPath) {
// FTPFile[] files = listFiles(directoryPath);
//
// if (files.length == 0) {
// System.out.println("目录为空或获取失败");
// return;
// }
//
// System.out.println("目录 " + (directoryPath != null ? directoryPath : "当前目录") + " 中的文件列表:");
// for (FTPFile file : files) {
// String fileType = file.isDirectory() ? "目录" : "文件";
// System.out.printf("%-10s %-20s %10d bytes%n",
// fileType, file.getName(), file.getSize());
// }
// }
// public static byte[] downloadFile(ApiConfig apiConfig, String targetPath) {
// FTPClient ftpClient = new FTPClient();
// try {
// // 1. 连接FTP服务器
// System.out.println("正在连接到FTP服务器 " + apiConfig.getApiUrl() + "...");
// ftpClient.connect(apiConfig.getApiUrl(), PORT);
// int replyCode = ftpClient.getReplyCode();
// if (!FTPReply.isPositiveCompletion(replyCode)) {
// System.err.println("FTP服务器拒绝连接,响应代码:" + replyCode);
// return null;
// }
// System.out.println("连接成功,准备登录...");
//
// // 2. 登录FTP服务器
// boolean success = ftpClient.login(apiConfig.getApiId(), apiConfig.getApiKey());
// if (!success) {
// System.err.println("FTP登录失败,请检查用户名和密码。");
// return null;
// }
// System.out.println("登录成功!");
//
// // 3. 设置FTP客户端工作模式
// // 进入被动模式,以适应防火墙/NAT环境
// ftpClient.enterLocalPassiveMode();
// // 设置文件传输类型为二进制文件,确保下载文件内容正确
// ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//
// // 4. 下载文件
// System.out.println("开始下载文件:" + targetPath);
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// boolean downloadSuccess = ftpClient.retrieveFile(targetPath, outputStream);
// if (downloadSuccess) {
// System.out.println("文件下载成功!文件大小:" + outputStream.size() + " 字节");
// return outputStream.toByteArray();
// } else {
// System.err.println("文件下载失败,请检查文件路径和网络连接。");
// return null;
// }
//
// } catch (IOException ex) {
// System.err.println("下载文件时发生异常:" + ex.getMessage());
// ex.printStackTrace();
// return null;
// } finally {
// // 5. 确保FTP连接被正确关闭
// if (ftpClient.isConnected()) {
// try {
// ftpClient.logout();
// ftpClient.disconnect();
// System.out.println("FTP连接已关闭。");
// } catch (IOException ex) {
// System.err.println("关闭FTP连接时发生异常:" + ex.getMessage());
// ex.printStackTrace();
// }
// }
// }
// }
}