File object is null for Camel Exchange getbody

Viewed 1505

In my camel route i am trying to get file object.

rest("/file")
    .post("/extract")
    .to("direct:extract");
    from("direct:extract")
    .process(new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
             File file = exchange.getIn().getBody(File.class);
             LOG.info("file : "+file);
     multipartEntityBuilder.addPart("file", new FileBody(file, ContentType.MULTIPART_FORM_DATA,filename));

        }
        })

Here from rest i'm sending file, in processor when i tried to get through exchange getBody i'm getting as null. But same thing if i try to fetch Inputstream and byte[] means its working fine.

    byte[] bytes = exchange.getIn().getBody(byte[].class);
    LOG.info("bytes : "+bytes);
    InputStream is = exchange.getIn().getBody(InputStream.class);

my goal is to fetch file object from exchange getBody ,is any thing wrong please let me know.

2 Answers
.produces(MediaType.APPLICATION_JSON)
    .consumes(MediaType.MULTIPART_FORM_DATA)
   .to("direct:extract");

    from("direct:extract")

    .setBody().simple("${body}")
    .to("http4://....")
    .end();

you don't need to convert it into Byte[] or inputstream,you can directly pass it as a body and then set it as a body

There is no java.io.File as the message body, as a HTTP file upload is not represented as a java.io.File but as an input stream instead. The java.io.File is for the regular file component in Camel.

Related