How to get the actual file object from Camel FTP route exchange

Suman Dhar picture Suman Dhar · Aug 3, 2015 · Viewed 8.7k times · Source

In my Camel router:

from(<SourceURI>)
.process(new Processor() {
    @Override
    public void process(Exchange exchange) throws Exception {
        // I want to extract the file object from the exchange
    }
.to(<targetURI>).

How can I achieve this?

I tried e.g. exchange.getIn().getHeader(Exchange.FILE_NAME, String.class) which gives me the file name. I am searching for something Exchange.FILE which gives me the actual file object. My Ultimate goal is to extract the file in the processor as the routed exchange is an archive file.

Answer

Claus Ibsen picture Claus Ibsen · Aug 3, 2015

Get the file from the body. Camel uses a 'org.apache.camel.component.file.GenericFile' to store as the file body. But you can use Camel's type converters to get the file in a type you want.

For example you can get the content in different types, such as:

String text = exchange.getIn().getBody(String.class);
byte[] bytes = exchange.getIn().getBody(byte[].class);
InputStream is = exchange.getIn().getBody(InputStream.class);