使用Apache-Camel ESB,尝试将xlsx文件上传到Spring Rest Web应用程序。从apache-camel ESB上传失败。但是从Postman上传效果很好。下面共享代码段。
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();
<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>
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)
@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"); } }
您可能会在Spring后端日志中看到此错误消息:
org.springframework.web.multipart.MultipartException:当前请求不是多部分请求。
您需要设置正确的ContentType标题。如果您想以这种方式实现,请参考这个类似的问题以寻求解决方案。
ContentType
但是,如果您切换co camel- http4组件(您在pom.xml中已经具有此组件),则可以消除混乱。该组件包含用于转换HttpEntity为的逻辑InputStream。然后可以HttpEntity直接设置为交换主体。
camel- http4
HttpEntity
InputStream
然后,您的路线将如下所示:
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?你不需要那个。
@ResponseBody
void