1

我有一个接收文件并将其发送到骆驼路线的服务。在这条路线上,我想使用 BeanIO 解组该文件,但它不能识别 InputStream 类型的输入。

@RestController
@RequestMapping(value = "/file")
public class FileController {

    @Autowired
    private CamelContext camelContext;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public void upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        ProducerTemplate template = camelContext.createProducerTemplate();
        template.sendBody("direct:routeTest", new ByteArrayInputStream(multipartFile.getBytes()));
    }

}

@Component
public class SampleRouter extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("direct:routeTest")
                .log("${body}")
                .to("dataformat:beanio:unmarshal?mapping=mapping.xml&streamName=testFile")
                .process(msg -> msg.getIn()
                        .getBody(List.class)
                        .forEach(o -> {...}))
                .end();
    }

}

我已经测试了一个使用 File 组件读取文件并将结果发送到 BeanIO 组件的路由。在这种情况下,它可以工作。

如何在 Apache Camel 上将 BeanIO 与 InputStream 类型的输入一起使用?是否有任何组件可以将我的 InputStream 转换为与 BeanIO 组件兼容的东西?

4

2 回答 2

3

正如 mgyongyosi 所说,当.log("${body}")读取流时,位置到它的末尾。为了解决这个问题,我们可以在读取 InputStream 后重置它的位置:

from("direct:routeTest")
        .log("${body}")
        .process(exchange -> ((InputStream) exchange.getIn().getBody()).reset())
        .to("dataformat:beanio:unmarshal?mapping=mapping.xml&streamName=testFile")

或删除该行.log("${body}")

于 2017-08-22T12:03:11.363 回答
1

其他解决方案可以是添加.streamCaching()到路由中。然后你可以多次读取同一个流。官方文档

于 2017-08-22T21:52:11.630 回答