update:米东三方接口数据

This commit is contained in:
Yuan
2025-07-31 15:27:47 +08:00
parent 6b49fbfaca
commit d773108ac0
12 changed files with 1653 additions and 99 deletions

View File

@@ -35,22 +35,52 @@ public class DownloadFtpUtil {
FTPClient ftpClient = new FTPClient();
try {
ftpClient = new FTPClient();
// 设置连接超时时间
ftpClient.setConnectTimeout(30000); // 30秒连接超时
ftpClient.setDataTimeout(60000); // 60秒数据传输超时
log.info("正在连接FTP服务器: " + ftpHost + ":" + ftpPort);
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
// 检查连接是否成功
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
log.info("未连接到FTP用户名或密码错误。");
log.error("FTP连接失败服务器响应码: " + ftpClient.getReplyCode());
ftpClient.disconnect();
} else {
log.info("FTP连接成功。");
return null;
}
log.info("正在登录FTP服务器用户名: " + ftpUserName);
boolean loginSuccess = ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!loginSuccess) {
log.error("FTP登录失败请检查用户名和密码");
ftpClient.disconnect();
return null;
}
log.info("FTP连接和登录成功");
return ftpClient;
} catch (SocketException e) {
log.error("FTP连接Socket异常: " + e.getMessage());
e.printStackTrace();
log.info("FTP的IP地址可能错误请正确配置。");
} catch (IOException e) {
log.error("FTP连接IO异常: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
log.error("FTP连接未知异常: " + e.getMessage());
e.printStackTrace();
log.info("FTP的端口错误,请正确配置。");
}
return ftpClient;
// 如果连接失败,确保断开连接
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
log.error("断开FTP连接失败: " + e.getMessage());
}
}
return null;
}
@@ -72,10 +102,11 @@ public class DownloadFtpUtil {
* @param fileName 文件名称
*/
public static boolean downloadFtpFile(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort, String ftpPath, String localPath,
String fileName) {
String ftpPassword, int ftpPort, String ftpPath, String localPath,
String fileName) {
FTPClient ftpClient = null;
OutputStream os = null;
try {
//先判断下载的路径是否存在 不存在创建
File file = new File(localPath);
@@ -84,28 +115,73 @@ public class DownloadFtpUtil {
}
ftpClient = getFTPClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
// 检查FTP客户端是否为空
if (ftpClient == null) {
log.error("FTP客户端创建失败请检查FTP连接参数");
return false;
}
// 检查连接是否成功
if (!ftpClient.isConnected()) {
log.error("FTP连接失败无法连接到服务器");
return false;
}
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
File localFile = new File(localPath + File.separatorChar + fileName);
OutputStream os = new FileOutputStream(localFile);
boolean b = ftpClient.retrieveFile(fileName, os);
// 设置文件类型前检查连接状态
if (ftpClient.isConnected()) {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
os.close();
ftpClient.logout();
// 切换到指定目录
if (ftpPath != null && !ftpPath.trim().isEmpty()) {
ftpClient.changeWorkingDirectory(ftpPath);
}
File localFile = new File(localPath + File.separatorChar + fileName);
os = new FileOutputStream(localFile);
boolean b = ftpClient.retrieveFile(fileName, os);
if (b) {
log.info("文件下载成功: " + fileName);
} else {
log.error("文件下载失败: " + fileName);
}
return b;
} else {
log.error("FTP连接已断开");
return false;
}
return b;
} catch (FileNotFoundException e) {
log.error("没有找到" + ftpPath + "文件");
log.error("没有找到文件: " + ftpPath + "/" + fileName);
e.printStackTrace();
} catch (SocketException e) {
log.error("连接FTP失败.");
log.error("连接FTP失败: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
log.error("文件读取错误: " + e.getMessage());
e.printStackTrace();
log.error("文件读取错误。");
} finally {
// 确保资源正确关闭
if (os != null) {
try {
os.close();
} catch (IOException e) {
log.error("关闭输出流失败: " + e.getMessage());
}
}
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
log.error("关闭FTP连接失败: " + e.getMessage());
}
}
}
return false;
}