Citrus框架 - 使用Spring集成通过SFTP上传文件
问题描述:
作为Citrus测试的一部分,我想通过SFTP将CSV文件推送到服务器。看来这应该可以使用Spring Integration。Citrus框架 - 使用Spring集成通过SFTP上传文件
柑橘-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:citrus="http://www.citrusframework.org/schema/config"
xmlns:citrus-test="http://www.citrusframework.org/schema/testcase"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp.xsd
http://www.citrusframework.org/schema/config http://www.citrusframework.org/schema/config/citrus-config.xsd
http://www.citrusframework.org/schema/testcase http://www.citrusframework.org/schema/testcase/citrus-testcase.xsd
">
<!-- Common settings -->
<context:property-placeholder location="classpath:citrus.properties"/>
<citrus:schema-repository id="schemaRepository"/>
<!-- SFTP component -->
<bean id="sftpSessionFactory" class="org.springframework.integration.file.remote.session.CachingSessionFactory">
<constructor-arg ref="defaultSftpSessionFactory" />
</bean>
<bean id="defaultSftpSessionFactory"
class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="${sftp.host}"/>
<property name="privateKey" value="${sftp.private.keyfile}"/>
<property name="port" value="${sftp.port}"/>
<property name="user" value="${sftp.username}"/>
<property name="allowUnknownKeys" value="true"/>
</bean>
<int-sftp:outbound-channel-adapter id="sftpOutboundAdapter"
session-factory="sftpSessionFactory"
channel="inputChannel"
remote-filename-generator-expression="payload.getName()"
remote-directory="/tmp/citrus">
<int-sftp:request-handler-advice-chain>
<int:retry-advice />
</int-sftp:request-handler-advice-chain>
</int-sftp:outbound-channel-adapter>
<int:channel id="inputChannel"/>
<citrus:channel-endpoint id="sftpEndpoint" channel="inputChannel" />
</beans>
SftpTransferIT.java
public class SftpTransferIT extends TestNGCitrusTestDesigner {
@Autowired
ChannelEndpoint sftpEndpoint;
@Test @Parameters("context")
@CitrusTest(name = "SftpTransferIT.sendFileByFTP")
public void sendFileByFTP(@Optional @CitrusResource TestContext context) {
final File file = new File("C:\\Temp\\example.xml");
final org.springframework.messaging.Message<File> message = MessageBuilder.withPayload(file).build();
Message ftpMessage = new ChannelMessageConverter().convertInbound(message,sftpEndpoint.getEndpointConfiguration(),context);
send(sftpEndpoint).message(ftpMessage);
}
}
以下异常在这种情况下抛出:
Caused by: org.springframework.messaging.MessagingException: Failed to write to '/tmp/citrus/example.xml.writing' while uploading the file; nested exception is org.springframework.core.NestedIOException: failed to write file; nested exception is 2: No such file
at org.springframework.integration.file.remote.RemoteFileTemplate.sendFileToRemoteDirectory(RemoteFileTemplate.java:537)
at org.springframework.integration.file.remote.RemoteFileTemplate.lambda$send$0(RemoteFileTemplate.java:310)
... 69 more
Caused by: org.springframework.core.NestedIOException: failed to write file; nested exception is 2: No such file
at org.springframework.integration.sftp.session.SftpSession.write(SftpSession.java:158)
at org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession.write(CachingSessionFactory.java:228)
at org.springframework.integration.file.remote.RemoteFileTemplate.sendFileToRemoteDirectory(RemoteFileTemplate.java:509)
... 70 more
Caused by: 2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2873)
我用下面的代码试图
我认为错误在于我如何通过Citrus发送消息。有人可以建议如何通过Citrus正确发送org.springframework.messaging.Message吗?
或者更好的做法是在没有Citrus send()的情况下使用纯Spring集成来执行这样的任务吗?
答
尝试配置use-temporary-file-name = "false"
:
默认情况下,每一个文件,该文件是被转移将出现在与其中通过缺省情况下,.writing附加的后缀的文件系统中的过程;这可以使用temporary-file-suffix属性进行更改。
但是,有些情况下您不想使用此技术(例如,如果服务器不允许重命名文件)。对于这种情况,可以通过将use-temporary-file-name设置为false来禁用此功能(默认为true)。当此属性为false时,该文件将以其最终名称编写,而消费应用程序将需要其他一些机制来检测该文件是否在访问之前完全上传。
并确保您的SFTP确实存在/tmp/citrus
。