Apache Camel-分段文件上传

时间:2018-07-06 22:43:28

标签: spring-mvc apache-camel multipartform-data

使用Apache-Camel ESB,尝试将xlsx文件上传到Spring Rest Web应用程序。从apache-camel ESB上传失败。但是从Postman上传效果很好。下面共享代码段。

  1. 骆驼路由器中的处理器代码如下

        from("file://data/PASInput").process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
    
            MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
            multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            String filename = (String) exchange.getIn().getHeader(Exchange.FILE_NAME);
            File file = exchange.getIn().getBody(File.class);
            multipartEntityBuilder.addPart("file",
                new FileBody(file, ContentType.MULTIPART_FORM_DATA, filename));
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            multipartEntityBuilder.build().writeTo(out);
            InputStream  inputStream = new ByteArrayInputStream(out.toByteArray());
            exchange.getOut().setBody(inputStream);         
        }
    }).to("http://localhost:8080/Pastel/api/convertor/pas/pastel")
            .log(LoggingLevel.ERROR, "RESPONSE BODY ${body}").end();
    
  2. Pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>2.21.0.fuse-000077-redhat-1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-cxf</artifactId>
            <version>2.21.0.fuse-000077-redhat-1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http</artifactId>
            <version>2.21.0.fuse-000077-redhat-1</version>
        </dependency>       
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http4</artifactId>
            <version>2.17.2</version>
        </dependency>
    
  3. 错误

        org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://localhost:8080/Pastel/api/convertor/pas/pastel with statusCode: 500
        at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:274)
       at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:183)
       at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
       at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)
       at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
       at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
       at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
       at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
       at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
       at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:452)
       at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:219)
       at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:183)
       at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:174)
       at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:101)
       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    
  4. 当我们使用邮递员访问Web服务时,没有错误。能够成功上传服务器。 Spring MVC代码,

                @RequestMapping(value = "/pas/pastel", method = RequestMethod.POST)
                @ResponseBody
                public void convertPASToPastel(HttpServletRequest request, HttpServletResponse response,
                @RequestParam(value = "file") final MultipartFile pasFile) {
                   try {
                      System.out.print("Here");
                   }
                }
    

1 个答案:

答案 0 :(得分:4)

您可能会在Spring后端日志中看到此错误消息:

  

org.springframework.web.multipart.MultipartException:当前请求不是多部分请求。

您需要设置正确的ContentType标头。如果您要以这种方式实施,请参考this类似问题以寻求解决方案。


但是,如果您切换co camel-http4组件(在pom.xml中已经有此组件),您可以摆脱混乱。该组件包含用于将HttpEntity转换为InputStream的逻辑。然后,您可以直接设置HttpEntity来交换正文。

然后您的路线将如下所示:

from("file://data/PASInput").process(new Processor() {
    @Override
    public void process(Exchange exchange) throws Exception {
        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
        multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        File file = exchange.getIn().getBody(File.class);
        multipartEntityBuilder.addPart("file",
                new FileBody(file, ContentType.MULTIPART_FORM_DATA, filename));
        exchange.getOut().setBody(multipartEntityBuilder.build());
    }
}).to("http4://localhost:8080/Pastel/api/convertor/pas/pastel")
        .log(LoggingLevel.ERROR, "RESPONSE BODY ${body}").end();

还有一个便条。 从不混合组件版本,始终对与Apache Camel版本相同的组件使用组件。否则,您将看到可预测的结果。当方法为@ResponseBody时,为什么还要在Spring控制器中有注释void?您不需要。