如何在ColdFusion中读取多部分响应 [英] How to Read Multipart Response in ColdFusion

查看:111
本文介绍了如何在ColdFusion中读取多部分响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将CFHTTP发布到Web服务,该服务将返回两部分(多部分),即XML和PDF.我希望只获取PDF.我的cfhttp.filecontentjava.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.

更新:

接下来,我研究了李的例子看看我是否可以简化我的代码.他们建议使用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:\temp\";

    // 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆