骆驼发送多部分/表单数据请求

2022-01-19 00:00:00 java apache-camel

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

I have an app, that sends a multipart/form-data & an app that recieves multipart/form-data request.

在接收器中处理:

    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 发送工作正常.我在将骆驼发送器与骆驼接收器集成时遇到了麻烦.

Parsing a request, send from Postman/Swagger works fine. I have troubles integrating my camel-sender with my camel reciever.

这里是发送者路由:

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.

在查找了同样的问题后,我找到了将 body 更改为的建议:

After looking up for the same issue, I found up a recommendation to change out body to:

 exchange.getOut().setBody(multipartEntityBuilder.build().getContent());

但是,它会导致接收方出现问题:

However,it causes issues on reciever side:

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

我读过这些帖子:

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

Apache Camel 多部分路由

但找不到解决办法

骆驼版:org.apache.camel:camel-cxf:2.21.0.000033-fuse-000001-redhat-1

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

推荐答案

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

You need to set Content-Type header with correct boundary, which is generated in MultipartEntityBuilder. Modify your sender processor to set this header.

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

相关文章