Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Sftp文件上载失败_Java_Spring Boot_Sftp_Jsch_Spring Integration Sftp - Fatal编程技术网

Java Sftp文件上载失败

Java Sftp文件上载失败,java,spring-boot,sftp,jsch,spring-integration-sftp,Java,Spring Boot,Sftp,Jsch,Spring Integration Sftp,我使用spring boot开发了一个调度程序。此计划程序在本地VM中创建文本文件,并将这些文件上载到远程FTP位置。 计划程序每天运行5个时间段。最后一个时段是晚上11点45分。 问题是,在晚上11点45分,文件上传不起作用,但文件正在本地创建。 日志包括在这里 2018-10-19 00:00:26.338 ERROR --- [task-scheduler-4] ework.integration.handler.LoggingHandler : org.springframework.m

我使用spring boot开发了一个调度程序。此计划程序在本地VM中创建文本文件,并将这些文件上载到远程FTP位置。 计划程序每天运行5个时间段。最后一个时段是晚上11点45分。 问题是,在晚上11点45分,文件上传不起作用,但文件正在本地创建。 日志包括在这里

2018-10-19 00:00:26.338 ERROR --- [task-scheduler-4] ework.integration.handler.LoggingHandler : org.springframework.messaging.MessageDeliveryException: Error handling message for file [/apps/logs/lesipay-scheduler/to_ctf/ClientCreation18102018_23.txt -> ClientCreation18102018_23.txt]; nested exception is org.springframework.messaging.MessagingException: Failed to write to '/logs/dumpfiles/to_ctf/ClientCreation18102018_23.txt.writing' while uploading the file; nested exception is org.springframework.core.NestedIOException: failed to write file; nested exception is 4: java.io.IOException: inputstream is closed, failedMessage=GenericMessage [payload=/apps/logs/lesipay-scheduler/to_ctf/ClientCreation18102018_23.txt, headers={id=e914c7a2-2b4c-74e2-92d6-b8158ed72874, timestamp=1539886500712}]
        at org.springframework.integration.file.remote.RemoteFileTemplate$1.doInSession(RemoteFileTemplate.java:321)
        at org.springframework.integration.file.remote.RemoteFileTemplate$1.doInSession(RemoteFileTemplate.java:283)
        at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:435)
        at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:283)
        at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:273)
        at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:265)
        at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.handleMessageInternal(FileTransferringMessageHandler.java:170)
        at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
        at org.springframework.integration.config.annotation.ServiceActivatorAnnotationPostProcessor$ReplyProducingMessageHandlerWrapper.handleRequestMessage(ServiceActivatorAnnotationPostProcessor.java:98)
        at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:109)
        at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
        at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
        at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:148)
        at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:121)
        at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:89)
        at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:423)
        at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:373)
        at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
        at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
        at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105)
        at org.springframework.messaging.core.AbstractMessageSendingTemplate.convertAndSend(AbstractMessageSendingTemplate.java:143)
        at org.springframework.messaging.core.AbstractMessageSendingTemplate.convertAndSend(AbstractMessageSendingTemplate.java:135)
        at org.springframework.integration.gateway.MessagingGatewaySupport.send(MessagingGatewaySupport.java:375)
        at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod(GatewayProxyFactoryBean.java:477)
        at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke(GatewayProxyFactoryBean.java:429)
        at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke(GatewayProxyFactoryBean.java:420)
        at org.springframework.integration.gateway.GatewayCompletableFutureProxyFactoryBean.invoke(GatewayCompletableFutureProxyFactoryBean.java:65)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
        at com.sun.proxy.$Proxy72.upload(Unknown Source)
        at lk.dialog.lesipayscheduler.scheduler.LesipayFileGenScheduledTask.writeToSftpFile(LesipayFileGenScheduledTask.java:184)
        at lk.dialog.lesipayscheduler.scheduler.LesipayFileGenScheduledTask.setRepoParams(LesipayFileGenScheduledTask.java:132)
        at lk.dialog.lesipayscheduler.scheduler.LesipayFileGenScheduledTask.fileGeneratorFor21Hr(LesipayFileGenScheduledTask.java:120)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
        at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
        at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: org.springframework.messaging.MessagingException: Failed to write to '/logs/dumpfiles/to_ctf/ClientCreation18102018_23.txt.writing' while uploading the file; nested exception is org.springframework.core.NestedIOException: failed to write file; nested exception is 4: java.io.IOException: inputstream is closed
        at org.springframework.integration.file.remote.RemoteFileTemplate.sendFileToRemoteDirectory(RemoteFileTemplate.java:558)
        at org.springframework.integration.file.remote.RemoteFileTemplate.access$500(RemoteFileTemplate.java:60)
        at org.springframework.integration.file.remote.RemoteFileTemplate$1.doInSession(RemoteFileTemplate.java:306)
        ... 46 more
    Caused by: org.springframework.core.NestedIOException: failed to write file; nested exception is 4: java.io.IOException: inputstream is closed
        at org.springframework.integration.sftp.session.SftpSession.write(SftpSession.java:159)
        at org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession.write(CachingSessionFactory.java:228)
        at org.springframework.integration.file.remote.RemoteFileTemplate.sendFileToRemoteDirectory(RemoteFileTemplate.java:530)
        ... 48 more
    Caused by: 4: java.io.IOException: inputstream is closed
        at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:697)
        at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:540)
        at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:492)
        at org.springframework.integration.sftp.session.SftpSession.write(SftpSession.java:156)
        ... 50 more
    Caused by: java.io.IOException: inputstream is closed
        at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2911)
        at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2935)
        at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:583)
        ... 53 more
用于文件上载的代码段

private void writeToSftpFile(List<String> stringList, String key, String specialCategory) {
    String outFileName = createSftpDirectory(key,specialCategory);
    if (stringList.size() > 0) {
        try (PrintStream ps = new PrintStream(outFileName)) {
            for (String str : stringList) {
                ps.println(str);
            }
            pathList.add(outFileName);
            logger.info("{} ", outFileName, " created.");
        } catch (IOException e) {
            logger.error("Couldn't write file -> {}", e);
        }
    } else {
        logger.info("No record for ->{}", outFileName);
    }

    if (key.equals("facilityKey")) {
        for (String localPath : pathList) {
            logger.info("localpath -> {}", localPath);
            uploadGateway.upload(new File(localPath));
        }
        pathList = new ArrayList<>();
    }

    if (specialCategory.equals("facilitySpecial")) {
        for (String localPath : pathList) {
            logger.info("localpath -> {}", localPath);
            uploadGateway.upload(new File(localPath));
        }
        pathList = new ArrayList<>();
    }

}
Sftp配置

@Configuration
public class SftpConfig {

    @Autowired
    private ConfigSource configSource;

    @Bean
    public SessionFactory<ChannelSftp.LsEntry> sftpFileSessionFactory() {
        DefaultSftpSessionFactory ssf = new DefaultSftpSessionFactory(true);
        ssf.setHost(configSource.getSftpHost());
        ssf.setPort(configSource.getSftpPort());
        ssf.setUser(configSource.getSftpUser());
        ssf.setPassword(configSource.getSftpPassword());
        ssf.setAllowUnknownKeys(true);
        return new CachingSessionFactory<>(ssf);
    }

    @Bean
    @ServiceActivator(inputChannel = "toSftpChannel")
    public MessageHandler handler() {
        SftpMessageHandler handler = new SftpMessageHandler(sftpFileSessionFactory());
        handler.setRemoteDirectoryExpression(new LiteralExpression(configSource.getRemoteDir()));
        handler.setFileNameGenerator(new FileNameGenerator() {
            @Override
            public String generateFileName(Message<?> message) {
                if (message.getPayload() instanceof File) {
                    return ((File) message.getPayload()).getName();
                } else {
                    throw new IllegalArgumentException("File expected as payload.");
                }
            }
        });
        return handler;
    }
}
一种方法是:

import pysftp as sftp

def sftpExample():
    try:
        s = sftp.Connection('ftp.company.com', username='enterusername', password='enterpassword')

        remotepath1='/home/filepath/sample.xlsx'
        localpath1="\\\\corporatenetwork\\datapath\\sample.xlsx"

        s.get(remotepath1,localpath1, preserve_mtime=True)


        s.close()

    except Exception, e:
        print str(e)

sftpExample()
一种方法是:

import pysftp as sftp

def sftpExample():
    try:
        s = sftp.Connection('ftp.company.com', username='enterusername', password='enterpassword')

        remotepath1='/home/filepath/sample.xlsx'
        localpath1="\\\\corporatenetwork\\datapath\\sample.xlsx"

        s.get(remotepath1,localpath1, preserve_mtime=True)


        s.close()

    except Exception, e:
        print str(e)

sftpExample()

在与网络团队进行一些监控之后,我可以找到这个问题的根本原因

原因是长时间打开的ssh会话正在被关闭。在使用ssh会话并关闭会话后,没有适当的机制来处理ssh会话


在处理会话创建和关闭后,我们能够解决该问题

在与网络团队进行一些监控后,我可以找到该问题的根本原因

原因是长时间打开的ssh会话正在被关闭。在使用ssh会话并关闭会话后,没有适当的机制来处理ssh会话


在处理了会话创建和关闭之后,我们能够解决这个问题

看看@pygo,这个问题在其他时间段也可以正常工作。有时这是由于网络问题造成的???@pygo您知道在文件上传完成后如何关闭sftp会话。为什么不使用pysftp?我只是发布了答案,因为它无法在评论部分发布,但是,有一个@pygo的问题是,在其他时间段,它可以正常工作。有时这是由于网络问题造成的???@pygo您知道在文件上传完成后如何关闭sftp会话。为什么不使用pysftp?我只是发布了答案,因为无法在评论部分发布,但是,感谢您的回答。但这是针对python的。我的项目是SpringBoot与sftp的集成。你知道我可以遵循的任何文件吗?我看看能不能找到。谢谢你的回答。但这是针对python的。我的项目是SpringBoot与sftp的集成。你知道我可以遵循的任何文档吗?我看看能不能找到。你能更新你的代码片段吗?或者链接到解决方案的相应文档?您可以更新代码片段吗?或链接到解决方案的适当文档?