Apache Camel - 多部分文件上传

5
使用Apache-Camel ESB,尝试将xlsx文件上传到Spring Rest Web应用程序。从Apache-Camel ESB上传失败。但是从Postman上传正常。以下是共享的代码片段。
  1. Processor Code in Router of Camel looks like

        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. Error

        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. When we hit the webservice using postman, there are no errors. Able to upload the servers successfully. Spring mvc code,

                @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");
                   }
                }
    

在您的处理器中设置Content-Type。您看过我在这里的答案了吗?Camel send multipart/form-data request,或者这里 - Bedla
1
仅供参考,没有Apache-Camel ESB。Camel在Fuse,Service-Mix,Tomcat,WildFly等平台上运行。 - Namphibian
1个回答

10
你可能会在Spring后端日志中看到这个错误消息:
org.springframework.web.multipart.MultipartException: 当前请求不是一个多部分请求。
你需要设置正确的ContentType头。如果你想以这种方式实现,请参考这个类似的问题来解决。

但是,如果你切换到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版本相同的组件版本。否则,您可能会看到不可预测的结果。为什么在Spring控制器中有注释@ResponseBody,当方法是void时?您不需要那个。


这有助于解决问题。我改用http4了。 - Naveen Kumar
请注意,http4组件在3.x及以上版本中已更名。请查看此链接:https://camel.apache.org/manual/latest/camel-3-migration-guide.html。 - Serhat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接