栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java读取ftp及sftp服务器文件内容

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java读取ftp及sftp服务器文件内容

1. ftp
引入依赖


    commons-net
    commons-net
    3.6

工具类

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class TestRead {
    
    public static List ReadFilesFromFTP(String hostName, int port, String userName, String userPass, String filePath, String fileHead) {
        FTPClient ftpClient = new FTPClient();
        ftpClient.setControlEncoding("UTF-8");
        ftpClient.enterLocalPassiveMode();
        ftpClient.configure(new FTPClientConfig("UNIX"));

        // 文件读取的结果存入result的集合中
        List result = new ArrayList<>();

        try{
            ftpClient.connect(hostName, port);
            ftpClient.login(userName, userPass);
            ftpClient.changeWorkingDirectory(filePath);
            // 找到目录下符合条件的文件
            String[] fs = ftpClient.listNames();
            for(String f : fs) {
                if(f.contains(fileHead)) {
                    InputStream in = ftpClient.retrieveFileStream(f);
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    String str;
                    while((str = br.readLine()) != null) {
                        result.add(str);
                    }
                    br.close();
                    in.close();
                }
            }


        } catch(IOException ie) {
            System.out.println(ie.getMessage());
        }
        return result;
    }
}

2. sftp
引入依赖


            com.jcraft
            jsch
            0.1.54

工具类

import com.jcraft.jsch.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class SftpUtils {

    private static ChannelSftp sftp = null;
    private static Session session = null;
    // 登录
    public static ChannelSftp login(String userName,String host,int port,String password) throws JSchException{
        JSch jSch = new JSch();
        // 设置用户名和主机,端口号一般都是22
        session = jSch.getSession(userName, host, port);
        // 设置密码
        session.setPassword(password);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        Channel channel = session.openChannel("sftp");
        channel.connect();

        sftp = (ChannelSftp) channel;
        System.out.println("connect success....");
        return sftp;
    }
    // 退出登录
    public static void logout() {
        if (sftp != null) {
            if (sftp.isConnected()) {
                sftp.disconnect();
            }
        }
        if (session != null) {
            if (session.isConnected()) {
                session.disconnect();
            }
        }
    }
    public static List readFile(String userName,String host,int port,String password,String fPath){
        BufferedReader reader = null;
        List ls = new ArrayList<>();
        // 登录sftp服务器
        try {
            ChannelSftp sftp = SftpUtils.login(userName,host,port,password);
        // 构建文件输入流,读取文件内容取
            reader = new BufferedReader(new InputStreamReader(sftp.get(fPath), StandardCharsets.UTF_8));
            String str;
            while((str = reader.readLine()) != null) {
                ls.add(str);
            }
            reader.close();
            //退出sftp
            SftpUtils.logout();

        } catch (JSchException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
        return ls;
    }
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/921758.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号