4

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.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Suman Dhar
  • 111
  • 1
  • 1
  • 9

2 Answers2

12

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); 
Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
3

For those who have a from("file:...") the following works:

File in = exchange.getIn().getBody(File.class);
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107