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 static void main(String[] args) throws Exception {
FTPClient ftpClient = new FTPClient();
try{
ftpClient.connect("10.20.175.72", 21);
ftpClient.login("admin", "Gzyh@1234");
int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器
if(!FTPReply.isPositiveCompletion(replyCode)) {
ftpClient.disconnect();
System.out.println("--ftp连接失败--");
System.exit(1);
}else {
System.out.println("--ftp连接成功--");
}
ftpClient.enterLocalPassiveMode();//这句最好加告诉对面服务器开一个端口
String newFileName = "测试.txt"; //新文件名
String fileName = "/data/DWHSBB/20201212/CORE_CIEIAA_20201212_INIT.DATA"; //原文件名
String downUrl = "/mnt"; //下载路径
boolean isTrue = false;
OutputStream os=null;
File localFile = new File(downUrl + "/" + newFileName);
os = new FileOutputStream(localFile);
isTrue = ftpClient.retrieveFile(new String(fileName.getBytes(),"ISO-8859-1"), os);
os.close();
System.out.println(isTrue);
if(isTrue){
System.out.println("file.size = " + localFile.length());
FileInputStream fileInputStream = new FileInputStream(localFile);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
BufferedReader bufferRead = new BufferedReader(inputStreamReader);
String readLine = "";
int i = 0;
while (!StringUtils.isEmpty(readLine = bufferRead.readLine())) {
if(i == 0){
System.out.println("readLine = " + readLine);
String[] lines = readLine.split("\\|@\\|");
System.out.println("lines.length = " + lines.length);
for (int j = 0; j < lines.length ; j++){
if(!StringUtils.isEmpty(lines[j].trim())){
System.out.println("lines[" + j + "] = " + lines[j].trim());
}
}
}
i++;
}
System.out.println("i = " + i);
bufferRead.close();
inputStreamReader.close();
fileInputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}finally {
close(ftpClient);
}
}