1、FtpUtils工具类
package pactera.tf.dl.common.util;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.IOException;
public class FtpUtils {
/**
* ftp链接
* @return
* @throws IOException
*/
public static FTPClient ftpConnection(String ip,int port,String username,String password) throws IOException {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ip, port);
ftpClient.login(username, password);
int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器
if(!FTPReply.isPositiveCompletion(replyCode)) {
ftpClient.disconnect();
System.out.println("--ftp连接失败--");
System.exit(1);
}else {
System.out.println("--ftp连接成功--");
}
ftpClient.enterLocalPassiveMode();//这句最好加告诉对面服务器开一个端口
} catch (Exception e) {
e.printStackTrace();
}
return ftpClient;
}
/**
* 断开FTP连接
* @param ftpClient 初始化的对象
* @throws IOException
*/
public static void close(FTPClient ftpClient) throws IOException{
if(ftpClient!=null && ftpClient.isConnected()){
ftpClient.logout();
ftpClient.disconnect();
}
}
}
2、解析文件数据
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;
public void coreDataWarehouse() throws Exception{
//获取昨天的时间
String yesterday = DateUtils.getChangeDate(-1);
//ftp地址
String ftpip = dlSysCfgManager.getVal(CfgSystemEnum.FTP_HOST.getCode());
//ftp端口
int ftpport = Integer.parseInt(dlSysCfgManager.getVal(CfgSystemEnum.FTP_PORT.getCode()));
//ftp用户名
String ftpusername = dlSysCfgManager.getVal(CfgSystemEnum.FTP_USERNAME.getCode());
//ftp密码
String ftppassword = dlSysCfgManager.getVal(CfgSystemEnum.FTP_PASSWORD.getCode());
//连接ftp
FTPClient ftpClient = FtpUtils.ftpConnection(ftpip,ftpport,ftpusername,ftppassword);
//ftp文件夹
String fileFolder = dlSysCfgManager.getVal(CfgSystemEnum.FTP_FILEFOLDER.getCode());
String fileName = fileFolder + "/" + yesterday;
try{
ftpClient.changeWorkingDirectory(fileName);
FTPFile[] files = ftpClient.listFiles();
if(files != null && files.length > 0){
for (FTPFile file : files){
if(file.getName().endsWith("rdy") || !(file.getName().contains(SysPrefixEnum.CORE.getCode()))){
continue;
}
//获取核心表数据
InputStream fileInputStream = ftpClient.retrieveFileStream(file.getName());
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
BufferedReader bufferRead = new BufferedReader(inputStreamReader);
String readLine = "";
while (!StringUtils.isEmpty(readLine = bufferRead.readLine())) {
System.out.println(readLine );
}
bufferRead.close();
inputStreamReader.close();
fileInputStream.close();
}
}
}catch (Exception e){
throw new DlCommonException(e, DlCommonErrorCodeEnum.EERROR_1007);
}finally {
FtpUtils.close(ftpClient);
}
}