Camel发送multipart / form-data请求

时间:2018-06-19 10:04:44

标签: java apache-camel

我有一个应用程序,它发送一个multipart / form-data&一个收到multipart / form-data请求的应用程序。

收货人处理:

    InputStreamCache postBody = exchange.getIn().getBody(InputStreamCache.class);
    MultipartUploadContext mux = new MultipartUploadContext(postBody, exchange.getIn().getHeader("Content-Type",String.class));
    Map<String,Object> params = mux.parseRequest();

解析请求,从Postman / Swagger发送工作正常。 我将骆驼发送器与骆驼接收器集成在一起很麻烦。

这是发件人路线:

from("direct:uploadFileToRest").routeId("uploadFileToRest").
    process("uploadFileRequestProcessor").
    setHeader(Exchange.HTTP_METHOD, simple("POST")).
    setHeader(Exchange.CONTENT_TYPE, constant("multipart/form-data")).
    to("myrecieverapp/full_upload").
    id("fileUploadRestSending").
    log(LoggingLevel.DEBUG, "RESPONSE BODY ${body}").
end();

发件人处理器:

MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder
                .create()
                .addTextBody("name", "SomeName")
                .addBinaryBody("content", array_with_byte_content_here);
exchange.getOut().setBody(multipartEntityBuilder.build());

发生以下错误:

 No body available of type: java.io.InputStream but has value: org.apache.http.entity.mime.MultipartFormEntity@35315261 of type: org.apache.http.entity.mime.MultipartFormEntity on: Message[ID-VRN26-1529401997491-0-3]. Caused by: No type converter available to convert from type: org.apache.http.entity.mime.MultipartFormEntity to the required type: java.io.InputStream with value org.apache.http.entity.mime.MultipartFormEntity@35315261.

在查找同样的问题之后,我找到了将身体改为:

的建议
 exchange.getOut().setBody(multipartEntityBuilder.build().getContent());

然而,它会导致接收方出现问题:

the request was rejected because no multipart boundary was found\r\n\tat org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:990)\r\n\tat org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:310)\r\n\tat org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:334)\r\n\tat ru.cinimex.abb.fileuploader.util.MultipartUploadContext.parseRequest(MultipartUploadContext.java:29)\r\n\tat ru.cinimex.abb.fileuploader.camel.processors.ValidateFullUploadRequestProcessor.process(ValidateFullUploadRequestProcessor.java:33)\r\n\tat org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)\r\n\tat org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)\r\n\tat 

我已经阅读了这些帖子:

http://hilton.org.uk/blog/camel-multipart-form-data

Apache Camel multipart route

但无法找到解决方案

Camel版本:org.apache.camel:camel-cxf:2.21.0.000033-fuse-000001-redhat-1

1 个答案:

答案 0 :(得分:4)

您需要使用Content-Type中生成的正确边界设置MultipartEntityBuilder标头。修改发件人处理器以设置此标头。

HttpEntity resultEntity = multipartEntityBuilder.build();
exchange.getOut().setHeader(Exchange.CONTENT_TYPE, resultEntity.getContentType().getValue());
exchange.getOut().setBody(resultEntity.getContent());