本文介绍了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\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-表单数据

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());

这篇关于Camel 发送多部分/表单数据请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 10:25