[英]How to Send Base64-Encoded String with Apache CXF JAX-RS to an External REST API that Accepts a Multipart Attachment?
我正在实施一个集成,以将带有 PDF 内容的 Base-64 编码字符串的传入消息发送到外部 REST API,该外部接受使用 HTTP 多部分/表单数据的附件。
纯文本没有问题,但在发送 Base-64 编码字符串时出现以下错误。
“将 MIME 多部分正文部分写入 output stream 时出错。”
伪代码:
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.AttachmentBuilder;
import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
WebClient client = WebClient.create("http"//External_URI");
client.type("multipart/form-data");
// String plainTextRequest = "Plain text in attachment works";
// Below string is just the first line of encoded content. Actual content has logo and other data.
String base64Request = "JVBERi0xLjQKJaqrrK0KMSAwIG9iago8PAovQ3JlYXRvciAoQXBhY2hlIEZPUCBWZXJzaW9uIDIuNykKL1Byb2R1Y2VyIChBcGFjaGUgRk9QIFZlcnNpb24gMi4................";
// No issue with below attachment with plain text
//final Attachment att = new AttachmentBuilder().object(plainTextRequest).contentDisposition(new //ContentDisposition("form-data; name="notice"; filename="notice.pdf"")).build();
// Above error when sending attachment with base64 encoded content
final Attachment att = new AttachmentBuilder().id("notice").object(base64Request).header("Content- Transfer-Encoding", "base64").mediaType("application/pdf").contentDisposition(new ContentDisposition("form- data; name="notice"; filename="notice.pdf"")).build();
client.post(att);
Apache CXF JAX-RS 在发送带有 base64 编码字符串的附件时抛出错误。
此问题已通过将允许分块策略设置为 false 来解决。
HTTPClientPolicy.setAllowChunking(false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.