spring 集成流程中的字节 [] 文件
File to byte [] in spring integration flow
我有一部分项目正在从 FTP 轮询文件,我想将文件内容转换或转换为 byte[] 并将其作为 byte[] 保存在数据库中,我找到了一个 FileToByteArrayTransformer,但不确定如何使用它。
谁能教教我怎么用吗?
这是我的代码:
@Bean
public IntegrationFlow ftpInboundFlow() {
return IntegrationFlows
.from(Ftp.inboundAdapter(ftpSessionFactory())
.preserveTimestamp(true)
.remoteDirectory(appProperties.getFtp().getRemoteDirectory())
.patternFilter(appProperties.getFtp().getFilter())
.deleteRemoteFiles(true)
.localDirectory(new File("inbound"))
.temporaryFileSuffix(appProperties.getFtp().getTemporaryFileSuffix()),
e -> e.id("ftpInboundAdapter")
.poller(Pollers.fixedDelay(appProperties.getFtp().getPollerDelay()))
.autoStartup(true))
.<File, byte[]>transform(p -> new FileToByteArrayTransformer().transform(p.getAbsoluteFile()))
.handle( p -> {
log.info("After transform " + p);
CustomerFile customerFile = CustomerFile.builder()
.content(p)
.customerFileType(CustomerFileType.ORDER_BOOK)
.build();
customerFileService.saveCustomerFile(customerFile);
}
// .publishSubscribeChannel(s ->
// s.subscribe(h -> h
// .handle(p -> {
// byte[] payload = SerializationUtils.serialize(p.getPayload());
// CustomerFile customerFile = CustomerFile.builder()
// .content(payload)
// .customerFileType(CustomerFileType.ORDER_BOOK)
// .build();
// customerFileService.saveCustomerFile(customerFile);
// }, e -> e.advice(expressionAdvice()))
// )
// )
/* .handle(Ftp.outboundAdapter(ftpSessionFactory())
.useTemporaryFileName(true)
.autoCreateDirectory(true)
.remoteDirectory("/ftp/GE/Inbound/history"))*/
.get();
}
FileToByteArrayTransformer
是一个 GenericTransformer<Message<?>, Message<?>>
实现。就是一个。
另一方面有相应的 DSL API:
/**
* Populate the {@link MessageTransformingHandler} instance for the provided
* {@link GenericTransformer}. Use {@link #transform(Class, GenericTransformer)} if
* you need to access the entire message.
* @param genericTransformer the {@link GenericTransformer} to populate.
* @param <S> the source type - 'transform from'.
* @param <T> the target type - 'transform to'.
* @return the current {@link IntegrationFlowDefinition}.
* @see org.springframework.integration.transformer.MethodInvokingTransformer
* @see org.springframework.integration.handler.LambdaMessageProcessor
*/
public <S, T> B transform(GenericTransformer<S, T> genericTransformer) {
所以,你在那个地方需要的就是这个:
.transform(new FileToByteArrayTransformer())
我有一部分项目正在从 FTP 轮询文件,我想将文件内容转换或转换为 byte[] 并将其作为 byte[] 保存在数据库中,我找到了一个 FileToByteArrayTransformer,但不确定如何使用它。
谁能教教我怎么用吗?
这是我的代码:
@Bean
public IntegrationFlow ftpInboundFlow() {
return IntegrationFlows
.from(Ftp.inboundAdapter(ftpSessionFactory())
.preserveTimestamp(true)
.remoteDirectory(appProperties.getFtp().getRemoteDirectory())
.patternFilter(appProperties.getFtp().getFilter())
.deleteRemoteFiles(true)
.localDirectory(new File("inbound"))
.temporaryFileSuffix(appProperties.getFtp().getTemporaryFileSuffix()),
e -> e.id("ftpInboundAdapter")
.poller(Pollers.fixedDelay(appProperties.getFtp().getPollerDelay()))
.autoStartup(true))
.<File, byte[]>transform(p -> new FileToByteArrayTransformer().transform(p.getAbsoluteFile()))
.handle( p -> {
log.info("After transform " + p);
CustomerFile customerFile = CustomerFile.builder()
.content(p)
.customerFileType(CustomerFileType.ORDER_BOOK)
.build();
customerFileService.saveCustomerFile(customerFile);
}
// .publishSubscribeChannel(s ->
// s.subscribe(h -> h
// .handle(p -> {
// byte[] payload = SerializationUtils.serialize(p.getPayload());
// CustomerFile customerFile = CustomerFile.builder()
// .content(payload)
// .customerFileType(CustomerFileType.ORDER_BOOK)
// .build();
// customerFileService.saveCustomerFile(customerFile);
// }, e -> e.advice(expressionAdvice()))
// )
// )
/* .handle(Ftp.outboundAdapter(ftpSessionFactory())
.useTemporaryFileName(true)
.autoCreateDirectory(true)
.remoteDirectory("/ftp/GE/Inbound/history"))*/
.get();
}
FileToByteArrayTransformer
是一个 GenericTransformer<Message<?>, Message<?>>
实现。就是一个。
另一方面有相应的 DSL API:
/**
* Populate the {@link MessageTransformingHandler} instance for the provided
* {@link GenericTransformer}. Use {@link #transform(Class, GenericTransformer)} if
* you need to access the entire message.
* @param genericTransformer the {@link GenericTransformer} to populate.
* @param <S> the source type - 'transform from'.
* @param <T> the target type - 'transform to'.
* @return the current {@link IntegrationFlowDefinition}.
* @see org.springframework.integration.transformer.MethodInvokingTransformer
* @see org.springframework.integration.handler.LambdaMessageProcessor
*/
public <S, T> B transform(GenericTransformer<S, T> genericTransformer) {
所以,你在那个地方需要的就是这个:
.transform(new FileToByteArrayTransformer())