sftp的方式读取EDoc并解析(java)

 java连接sftp服务器需要的jar

<dependency>
     <groupId>com.jcraft</groupId>
      <artifactId>jsch</artifactId>
      <version>0.1.55</version>
</dependency>

参考网上写的工具类

import com.jcraft.jsch.*;

import java.util.Properties;

/**
 * @author 
 * @version 1.0
 * @description
 * @date 2021/5/9 10:55
 **/
public class SftpUtil {
    private static Session sshSession;
    /**
     * 连接sftp服务器
     * @param host  ftp地址
     * @param port  端口
     * @param userName 账号
     * @param password 密码
     * @return
     */
    public static ChannelSftp sftpConnection(String host, int port, String userName, String password) throws JSchException {
        JSch jsch = new JSch();
        ChannelSftp channelSftp = null;
        try {
            jsch.getSession(userName, host, port);
            sshSession = jsch.getSession(userName, host, port);
            sshSession.setPassword(password);
            Properties properties = new Properties();
            properties.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(properties);
            sshSession.connect();
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            channelSftp = (ChannelSftp) channel;
        }catch (JSchException e){
            e.printStackTrace();
            throw new JSchException("Sftp服务器登录异常!");
        }
        return channelSftp;
    }

    /**
     *@description 退出Sftp服务器登录
     *@return
     **/
    public static void sftpClose(ChannelSftp channelSftp){
        if (channelSftp != null) {
            if (channelSftp.isConnected()){
                channelSftp.disconnect();
            }
        }

        if (sshSession != null) {
            if (sshSession.isConnected()){
                sshSession.disconnect();
                sshSession = null;
            }
        }
    }

}
import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 
 * @version 1.0
 * @description
 * @date 2021/5/7 18:12
 **/
public class EDocAnalysisUtils {
  
    public static Map<String, List<List<String>>> getE(String filePath, String charsetName,List<String> list) throws IOException {
        Path path = Paths.get(filePath);
        List<String> stringList = Files.readLines(path.toFile(), Charset.forName(charsetName));
        Map<Boolean, List<String>> booleanListMap = stringList.stream().collect(Collectors.groupingBy(s -> s.contains("<")));
        List<String> keyList = booleanListMap.get(true);
        List<Integer> indexList = keyList.stream().map(s -> stringList.indexOf(s)).collect(Collectors.toList());
        Map<String, List<List<String>>> hashMap = new HashMap<>();
        for (int i = 0; i < indexList.size(); i = i + 2) {
            hashMap.put(keyList.get(i), stringList.subList(indexList.get(i) + 2, indexList.get(i + 1)).stream()
                    .map(s -> s.replaceFirst("#\\s+","")).map(s -> Arrays.asList(s.split("\\s+")))
                    .filter(s -> !Collections.disjoint(s, list))
                    .collect(Collectors.toList()));
        }
        return hashMap;
    }

    public static Map<String, List<List<String>>> getE(String filePath, String charsetName) throws IOException {
        Path path = Paths.get(filePath);
        List<String> stringList = Files.readLines(path.toFile(), Charset.forName(charsetName));
        Map<Boolean, List<String>> booleanListMap = stringList.stream().collect(Collectors.groupingBy(s -> s.contains("<")));
        List<String> keyList = booleanListMap.get(true);
        List<Integer> indexList = keyList.stream().map(s -> stringList.indexOf(s)).collect(Collectors.toList());
        Map<String, List<List<String>>> hashMap = new HashMap<>();
        for (int i = 0; i < indexList.size(); i = i + 2) {
            hashMap.put(keyList.get(i), stringList.subList(indexList.get(i) + 2, indexList.get(i + 1)).stream()
                    .map(s -> s.replaceFirst("#\\s+","")).map(s -> Arrays.asList(s.split("\\s+")))
                    .collect(Collectors.toList()));
        }
        return hashMap;
    }

    public static Map<String, List<List<String>>> getE(File file, String charsetName) throws IOException {
        List<String> stringList = Files.readLines(file, Charset.forName(charsetName));
        Map<Boolean, List<String>> booleanListMap = stringList.stream().collect(Collectors.groupingBy(s -> s.contains("<")));
        List<String> keyList = booleanListMap.get(true);
        List<Integer> indexList = keyList.stream().map(s -> stringList.indexOf(s)).collect(Collectors.toList());
        Map<String, List<List<String>>> hashMap = new HashMap<>();
        for (int i = 0; i < indexList.size(); i = i + 2) {
            hashMap.put(keyList.get(i), stringList.subList(indexList.get(i) + 2, indexList.get(i + 1)).stream()
                    .map(s -> s.replaceFirst("#\\s+","")).map(s -> Arrays.asList(s.split("\\s+")))
                    .collect(Collectors.toList()));
        }
        return hashMap;
    }

    public static Map<String, List<List<String>>> getE(File file, String charsetName, List<String> list) throws IOException {
        List<String> stringList = Files.readLines(file, Charset.forName(charsetName));
        Map<Boolean, List<String>> booleanListMap = stringList.stream().collect(Collectors.groupingBy(s -> s.contains("<")));
        List<String> keyList = booleanListMap.get(true);
        List<Integer> indexList = keyList.stream().map(s -> stringList.indexOf(s)).collect(Collectors.toList());
        Map<String, List<List<String>>> hashMap = new HashMap<>();
        for (int i = 0; i < indexList.size(); i = i + 2) {
            hashMap.put(keyList.get(i), stringList.subList(indexList.get(i) + 2, indexList.get(i + 1)).stream()
                    .map(s -> s.replaceFirst("#\\s+","")).map(s -> Arrays.asList(s.split("\\s+")))
                    .filter(s -> !Collections.disjoint(s, list))
                    .collect(Collectors.toList()));
        }
        return hashMap;
    }


}
EDocAnalysisUtils 这个类的几个参数说明
filePath EDoc文件路径
charsetName 以那种编码解析(“UTF-8”或者“GBK”或其他)
list  过滤需要的数据
方法说明:
Collections.disjoint(s, list)
s集合和list有相同元素返回false, 没有相同元素则返回true。

连接sftp,并解析
ChannelSftp sftp = SftpUtil.sftpConnection(host,port,userName,password);
       try {
            if (path != null && !"".equals(path)) {
                sftp.cd(path);//进入所在路径
            }
            Vector ls = sftp.ls(path);
        // 获取路径下最后一个元素
       ChannelSftp.LsEntry lsEntry = (ChannelSftp.LsEntry)ls.lastElement();
        File file = new File("xxx.DT");
        // 把sftp服务器上的文件写入file文件中
        sftp.get(lsEntry.getFilename(), new FileOutputStream(file));
        // 关闭sftp连接
        SftpUtil.sftpClose(sftp);
        // 过滤需要的数据,这里可以用sql查询获得, 自己新建只是为了不报错
        List<String> list = new ArrayList<>();         Map<String, List<List<String>>> listMap = EDocAnalysisUtils.getE(file, "GBK",list);
        // listMap 就是解析之后的数据,剩下就是对数据的操作,此处省略 }
catch (Exception e){ e.printStackTrace(); }

path 参数说明: 文件在sftp的路径

部分代码手打的,可能会有错误。
自己最近工作的整理,希望对你们也有帮助。
 

关于sftp连接,参考

https://blog.csdn.net/Aoutlaw/article/details/103559886,感谢!

posted @ 2021-05-11 18:46  记忆沙漏  阅读(281)  评论(0编辑  收藏  举报