首页 > 解决方案 > Camel Exchange getbody 的文件对象为空

问题描述

在我的骆驼路线中,我试图获取文件对象。

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));

        }
        })

从休息开始,我在处理器中发送文件,当我试图通过交换 getBody 时,我得到了空值。但是如果我尝试获取 Inputstream 并且 byte[] 意味着它工作正常,那么同样的事情。

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

我的目标是从交换 getBody 获取文件对象,有什么问题请告诉我。

标签: apache-camelapache-httpcomponents

解决方案


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

    from("direct:extract")

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

不需要转换成Byte[]或者inputstream,直接作为body传递,然后设置为body


推荐阅读