如何在 ColdFusion 中读取多部分响应 [英] How to Read Multipart Response in ColdFusion
问题描述
我正在向 Web 服务发布 CFHTTP 帖子,该服务返回两部分(多部分),即 XML 和 PDF.我希望只获得 PDF.我的 cfhttp.filecontent
是 java.io.ByteArrayOutputStream
类型.当我执行 toString()
时,我得到以下信息
I am doing a CFHTTP post to a web service that is returning two parts (multipart), a XML and PDF. I am looking to get only the PDF. My cfhttp.filecontent
is a java.io.ByteArrayOutputStream
type. When I do a toString()
I get the following
第 1 部分
Content-Type: application/xop+xml; type="text/xml"; charset=utf-8
Content-Transfer-Encoding: 8bit
第 2 部分
Content-Type: application/pdf
Content-Transfer-Encoding: binary
我在 cfhttp.fileContent 中得到响应,数据如下所示
I get the response in cfhttp.fileContent and the data looks like the following
--MIME_Boundary
Content-ID: <aa82dfa.N51ec355b.3.15b86044531.59d6>
Content-Type: application/xop+xml; type="text/xml"; charset=utf-8
Content-Transfer-Encoding: 8bit
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">....</soapenv:Envelope>
--MIME_Boundary
Content-Id: <2958beaa-dd72-4879-9d80-cc19876b2c2a@example.jaxws.sun.com>
Content-Type: application/pdf
Content-Transfer-Encoding: binary
%PDF-1.4
%ÈÁÄ×
<content removed>
25081
%%EOF
--MIME_Boundary--
我尝试删除所有与 PDF 无关的数据,但它仍然不是有效的二进制文件.
I tried to remove all the data that's not related to the PDF but it's still not a valid binary file.
有什么想法吗?
来自评论
当我对 fileContent
执行 cfdump
时,我得到以下信息:
When I do a cfdump
on the fileContent
I get the following:
Class Name: java.io.ByteArrayOutputStream
Methods:
close() returns void
reset() returns void
size() returns int
toByteArray() returns byte[]
toString(java.lang.String) returns java.lang.String
toString() returns java.lang.String
toString(int) returns java.lang.String
write(byte[], int, int) returns void
write(int) returns void
writeTo(java.io.OutputStream) returns void
当我调用 toByteArray()
时,我得到二进制数据.然后我将数据保存到一个文件中,我可以看到文件的 XML 和 PDF 部分.
When I invoke toByteArray()
I get binary data. I then save the data to a file and I see both XML and PDF parts of the file.
推荐答案
解决方案需要进行两项更改:将接受的编码值设置为 gzip、deflate 以及使用 java 处理二进制数据.
The workaround required two changes: a change to set the accepted encoding value to gzip,deflate and to work with binary data using java.
<cfhttpparam type="HEADER" name="Accept-Encoding" value="gzip,deflate">
其次,我需要使用二进制方法来操作响应.
Second I needed to manipulate the response using binary methods.
binResponse = result.fileContent.toByteArray();
接下来我使用了 Ben Nadel 的实用程序,Binary.cfc,它具有我需要的所有二进制操作.我使用方法 binarySlice()
来提取二进制文件的开始和结束部分.切片数据包含我需要的确切格式的二进制文件.它不是 base64 或任何其他类型,它是二进制的.
Next I used a utility from Ben Nadel, Binary.cfc, that has all the binary manipulation I needed. I used the method binarySlice()
to extract the start and end part of the binary. The sliced data contains the binary in the exact format that I needed. It was not base64 or any another type, it was binary.
sliced = binNadel.binarySlice( binResponse, <int posistion to start slice>, <int length of binary>));
这个解决方案有效,但它已经成熟,存在潜在问题,例如响应的顺序可能会切换,边界名称可能会改变等.因此这将需要大量的错误处理以确保顺利进行.
This solution works, but it's ripe with potential issues, for example the order of the response could switch, the boundary name could change, etc. So this will require a lot of error handling to ensure smooth sailing.
更新:
接下来我研究了 Leigh 的例子 看看我是否可以简化我的代码.他们建议使用 Java 的 MimeMultipart 类,它支持解析 MTOM 多部分响应.这是最终的工作代码:
Next I looked into Leigh's example to see if I could simplify my code. They suggested using Java's MimeMultipart class which supports parsing an MTOM multipart response. Here is the final working code:
<cfscript>
// Modify path as needed
saveToDirec = "c: emp";
// Hard coded "boundary" value for DEMO purposes. It MUST match actual value used in cfhttp response
// Best to use cfhttp.responseHeader.content-Type so [if] the service changes your code won't break.
contentType = "multipart/related; boundary=MIME_Boundary;";
// Load and parse ByteArrayOutputStream returned by CFHTTP
dataSource = createObject("java", "javax.mail.util.ByteArrayDataSource").init(m_strSoapResponse.fileContent.toByteArray(), javaCast( "string", contentType));
mimeParts = createObject("java", "javax.mail.internet.MimeMultipart").init(dataSource);
for (i = 0; i < mimeParts.getCount(); i++) {
writeOutput("<br>Processing part["& i &"]");
bp = mimeParts.getBodyPart( javacast("int", i));
// If this part is a PDF, save it to a file.
if (!isNull(bp) && bp.isMimeType("application/pdf")) {
outputFile = createObject("java", "java.io.File").init(saveToDirec &"demo_savedfile_"& i &".pdf");
bp.saveFile(outputFile);
writeOutput("<br>Saved: "& outputFile.getAbsolutePath());
}
}
</cfscript>
感谢大家的意见!
这篇关于如何在 ColdFusion 中读取多部分响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!