Elias Khattar
Elias Khattar

Reputation: 173

File to byte [] in spring integration flow

I have a part of my project where I'm polling a file from FTP and I want to convert or transform the file content to byte[] and save it in the database as byte[] , I found a FileToByteArrayTransformer but not sure how I can use it.

Can some one please enlighten me on how to use it?

Here is my code:

   @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();
    }

Upvotes: 0

Views: 353

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121552

The FileToByteArrayTransformer is a GenericTransformer<Message<?>, Message<?>> implementation. That's one.

On the other hand there is respective 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) {

So, what you need in that place is just this:

.transform(new FileToByteArrayTransformer())

Upvotes: 0

Related Questions