繁体   English   中英

spring SFTP从byte []远程创建文件

[英]spring SFTP create file in remote from byte[]

如何在byte []中创建远程目录中的文件,因为PollableChannel中有send()方法。 从下面的代码能够发送文件到远程,但它在本地机器上创建一个文件。 如何避免在本地机器上创建文件?

PollableChannel remoteFileChannel = context.getBean("outputChannel", PollableChannel.class); 

Message<byte[]> sendFile = MessageBuilder.withPayload("hi how are you".getBytes()).build();

remoteFileChannel.send(sendFile);

spring sftp配置:

<bean id="sftpSessionFactory"
        class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory"
        p:host="${sftp.host}"
        p:port="${sftp.port}"
        p:user="${sftp.username}"
        p:password="${sftp.password}"
        p:allowUnknownKeys="${sftp.allowUnknownKeys}" />

<int:channel id="outputChannel">
        <int:queue />
</int:channel>

<int-sftp:outbound-channel-adapter id="outboundAdapter"
        session-factory="sftpSessionFactory"
        channel="outputChannel"
        charset="UTF-8"
        remote-directory="${sftp.remotedir}"
        remote-filename-generator="randomFileNameGenerator" /> 

如何创建一个具有随机名称的文件并将字节写入其中?

我试过自定义文件名生成器类:

@Component("randomFileNameGenerator")
public class RandomFileNameGenerator implements FileNameGenerator {

    @Override
    public String generateFileName(Message<?> msg) {
        long number = (long) Math.floor(Math.random() * 90000000L) + 1000000L;
        String fileName = String.format("%d.txt", number);
        return fileName;
    }

}

如果文件名未设置,则创建名称为“adas-asdfsadf-545sadf.msg”的文件。 任何人都可以指出我做错了什么

首先,您不会显示Spring Integration配置。

另一方面,如果看起来代码是您的,那么为什么你说“它正在创建一个文件”并不清楚。 那么,那就是你创建文件的人。

有一些开箱即用的组件,如<int-sftp:outbound-channel-adapter><int-sftp:outbound-gateway> ,它们绝对可以满足您的目标。

它们都基于RemoteFileTemplate.send()操作,它可以很好地处理byte[] payload

请参阅“ 参考手册样品”中的更多信息。

UPDATE

如何创建一个具有随机名称的文件并将字节写入其中?

对我来说似乎已经修复了byte[]问题。

回覆。 “随机名称”。 看起来你正确的方式: remote-filename-generator="fileNameGenerator"完全适用于该任务。 请参阅Framework中的FileNameGenerator策略及其实现。 您可以在自定义实现中使用randomize函数,并从该通道适配器定义中引用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM