【问题标题】:How to transfer *.pgp files using SFTP spring Integration如何使用 SFTP spring 集成传输 *.pgp 文件
【发布时间】:2016-03-18 12:24:07
【问题描述】:

我们正在开发通用自动化应用程序,它将从 SFTP 服务器下载 *.pgp 文件。 该应用程序可以与 *.txt 文件一起正常工作。但是当我们尝试拉取 *.pgp 文件时,我们会遇到以下异常。

2016-03-18 17:45:45 INFO  jsch:52 - SSH_MSG_SERVICE_REQUEST sent
2016-03-18 17:45:46 INFO  jsch:52 - SSH_MSG_SERVICE_ACCEPT received
2016-03-18 17:45:46 INFO  jsch:52 - Next authentication method: publickey
2016-03-18 17:45:48 INFO  jsch:52 - Authentication succeeded (publickey).
sftpSession org.springframework.integration.sftp.session.SftpSession@37831f
files size158
java.io.IOException: inputstream is closed
at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2884)
at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2908)
at com.jcraft.jsch.ChannelSftp.access$500(ChannelSftp.java:36)
at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1390)
at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1340)
at org.springframework.util.StreamUtils.copy(StreamUtils.java:126)
at org.springframework.util.FileCopyUtils.copy(FileCopyUtils.java:109)
at     org.springframework.integration.sftp.session.SftpSession.read(SftpSession.java:129)
at com.sftp.test.SFTPTest.main(SFTPTest.java:49)

java代码:

public class SFTPTest {

public static void main(String[] args) {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
    DefaultSftpSessionFactory defaultSftpSessionFactory = applicationContext.getBean("defaultSftpSessionFactory", DefaultSftpSessionFactory.class);
    System.out.println(defaultSftpSessionFactory);
    SftpSession sftpSession = defaultSftpSessionFactory.getSession();
    System.out.println("sftpSessikon "+sftpSession);
    String remoteDirectory = "/";
    String localDirectory = "C:/312421/temp/";
    OutputStream outputStream = null;
    List<String> fileAtSFTPList = new ArrayList<String>();
    try {
        String[] fileNames = sftpSession.listNames(remoteDirectory);
        for (String fileName : fileNames) {
            boolean isMatch = fileCheckingAtSFTPWithPattern(fileName);
            if(isMatch){
                fileAtSFTPList.add(fileName);
            }
        }
        System.out.println("files size" + fileAtSFTPList.size());
        for (String fileName : fileAtSFTPList) {
            File file = new File(localDirectory + fileName);

            /*InputStream ipstream= sftpSession.readRaw(fileName);
            FileUtils.writeByteArrayToFile(file, IOUtils.toByteArray(ipstream));
            ipstream.close();*/

            outputStream = new FileOutputStream(file);
             sftpSession.read(remoteDirectory + fileName, outputStream);
            outputStream.close();
        } 
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }finally {
        try {
            if (outputStream != null)
                outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




}


public static boolean fileCheckingAtSFTPWithPattern(String fileName){
    Pattern pattern = Pattern.compile(".*\\.pgp$");
    Matcher matcher = pattern.matcher(fileName);
    if(matcher.find()){
        return true;
    }
    return false;
}

}

请建议如何解决这个问题。 谢谢

【问题讨论】:

    标签: spring spring-integration sftp


    【解决方案1】:

    文件类型与 Spring Integration 无关——看起来服务器在读取序言时正在关闭连接——在获取数据之前...

    at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2908)
    at com.jcraft.jsch.ChannelSftp.access$500(ChannelSftp.java:36)
    at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1390)
    at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1340)
    

    数据本身直到稍后才会被读取(ChannelSftp 中的第 1442 行)。

    所以看起来像是服务器端的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多