Java ftp获取单个文件

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);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值