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" />
如何创建一个具有随机名称的文件并将字节写入其中?
我试过使用自定义文件名生成器 class:
@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 集成配置。
另一方面,不清楚为什么你说“它正在创建一个文件”,如果代码看起来是你的。所以,创建文件的就是你。
有 <int-sftp:outbound-channel-adapter>
和 <int-sftp:outbound-gateway>
等开箱即用的组件,绝对可以满足您的目标。
它们都是基于 RemoteFileTemplate.send()
操作,可以很好地处理 byte[]
payload
。
在 Reference Manual and Samples 中查看更多信息。
更新
how to create an file with random name and write bytes into it?
对我来说,我们刚刚解决了 byte[]
问题。
回复。 "random name"。看起来你走对了路:remote-filename-generator="fileNameGenerator"
正是用于该任务。请参阅 FileNameGenerator
策略及其在框架中的实施。您可以在自定义实现中使用随机化函数,并从该通道适配器定义中引用它。
如何从 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" />
如何创建一个具有随机名称的文件并将字节写入其中?
我试过使用自定义文件名生成器 class:
@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 集成配置。
另一方面,不清楚为什么你说“它正在创建一个文件”,如果代码看起来是你的。所以,创建文件的就是你。
有 <int-sftp:outbound-channel-adapter>
和 <int-sftp:outbound-gateway>
等开箱即用的组件,绝对可以满足您的目标。
它们都是基于 RemoteFileTemplate.send()
操作,可以很好地处理 byte[]
payload
。
在 Reference Manual and Samples 中查看更多信息。
更新
how to create an file with random name and write bytes into it?
对我来说,我们刚刚解决了 byte[]
问题。
回复。 "random name"。看起来你走对了路:remote-filename-generator="fileNameGenerator"
正是用于该任务。请参阅 FileNameGenerator
策略及其在框架中的实施。您可以在自定义实现中使用随机化函数,并从该通道适配器定义中引用它。