3

how to create an file in remote directory from byte[], as there is send() methods available in PollableChannel. from below code able to send file to remote, but it's creating an file in local machine. how to avoid creating file in local machine?

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

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

remoteFileChannel.send(sendFile);

spring sftp configuration:

<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" /> 

how to create an file with random name and write bytes into it?

I have tried with custom file name generator 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;
    }

}

where the filename was not setting, creating file with name like "adas-asdfsadf-545sadf.msg". can any one point me where am doing wrong

Abhishek Nayak
  • 3,732
  • 3
  • 33
  • 64

1 Answers1

1

First of all you don't show your Spring Integration configuration.

From other hand it isn't clear why you say " it's creating an file", if it looks like code is yours. So, that is you who create a file.

There are out-of-the-box components like <int-sftp:outbound-channel-adapter> and <int-sftp:outbound-gateway>, which definitely can address your goal.

Both of them are based on the RemoteFileTemplate.send() operation which handles byte[] payload very well.

See more info in the Reference Manual and Samples.

UPDATE

how to create an file with random name and write bytes into it?

Seems for me we have just fixed with you the byte[] question.

Re. "random name". Looks like you go right way: the remote-filename-generator="fileNameGenerator" is exactly for that task. See FileNameGenerator strategy and its implementations in the Framework. You can utilize your randomize function in your custom implementation and refer it from that channel-adapter definition.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Thank you Artem, i have updated my configuration in question. – Abhishek Nayak Mar 14 '16 at 13:49
  • `RemoteFileTemplate.send(byte[])` is suitable to my requirement, except the filename, how to set 10.TXT ? – Abhishek Nayak Mar 14 '16 at 14:03
  • You don't need to worry about `RemoteFileTemplate`. The ` ` is exactly for you. Just send `byte[]` and read an UPDATE in my answer. – Artem Bilan Mar 14 '16 at 14:05
  • is there an way to create an custom FileNameGenerator class? or generating random number with spring expression language? – Abhishek Nayak Mar 14 '16 at 14:14
  • ??? You just should write your own class which implements `FileNameGenerator` and register it as a Spring ``. Yes, you can use `remote-filename-generator-expression` instead, if your logic is enough simple. – Artem Bilan Mar 14 '16 at 14:48
  • I have tried with creating custom file name generator class, but not working. i have updated the question. – Abhishek Nayak Mar 14 '16 at 15:00
  • I don't see any problems in your code. Would be great if you can to debug your code and Spring Integration as well. – Artem Bilan Mar 14 '16 at 15:03