Coldfusion 如何将 PDF 响应流保存到文件

Saj*_*kar 5 coldfusion

我们有一个 REST API,它返回内容类型应用程序/pdf 的流。我只想将它保存到服务器上的文件中。

<cfhttp url="#ApiPath#" method="post" result="res">
     <cfhttpparam type="header" name="Content-Type" value="application/json" />
     <cfhttpparam type="body" value="#payload#" />
</cfhttp>

<cffile  action = "write" file = "#FileName#" output = "#res.fileContent#">
Run Code Online (Sandbox Code Playgroud)

我已经生成了一个空白的 PDF,有什么想法吗?(我尝试了 cfdocument/cfpdf 的各种组合,但没有运气)

这是 REST 响应的转储:

在此处输入图片说明

Saj*_*kar 6

我想我已经明白了:

解决方案一:

<cfhttp url="#url#" method="post" result="res">
    <cfhttpparam type="header" name="Content-Type" value="application/json" />
    <cfhttpparam type="body" value="#payload#" />
</cfhttp>
<cfset bytes  = res.FileContent.toByteArray()>
<cfscript>    
    fos = createObject("java", "java.io.FileOutputStream").init("myfile.pdf");
    fos.write(bytes);
    fos.flush();
    fos.close();
</cfscript>
Run Code Online (Sandbox Code Playgroud)

编辑:基于 SOS 的解决方案,这也有效:

解决方案2:

<cfhttp url="#url#" method="post" result="res" getAsBinary="Auto">
    <cfhttpparam type="header" name="Content-Type" value="application/json" />
    <cfhttpparam type="body" value="#payload#" />
</cfhttp>

<cfset fileName = listlast(res["responseHeader"]["content-disposition"],";=")>
<cffile action="write" file="#path#\#fileName#" output="#res.FileContent#">
Run Code Online (Sandbox Code Playgroud)