You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache NiFi中如何将SOAP响应属性的二进制内容保存为PDF文件?

解决NiFi中SOAP返回BINFILE二进制属性转PDF文件的问题

核心问题

PutFile处理器默认写入的是FlowFile的Content内容,而你的PDF二进制数据存在BINFILE属性里,直接用PutFile肯定生成空或损坏的PDF。之前用UpdateAttribute、ReplaceText没用,是因为它们要么只改属性,要么只能处理文本内容,没法把属性里的二进制转移到FlowFile Content中。

解决方案步骤

1. 将BINFILE属性的二进制数据转移到FlowFile Content

用ExecuteScript处理器(选Groovy脚本,处理二进制最可靠),脚本如下:

def flowFile = session.get()
if (!flowFile) return

// 读取BINFILE属性的二进制数据,用ISO-8859-1确保字节完整不丢失
def binData = flowFile.getAttribute('BINFILE')?.getBytes('ISO-8859-1')
if (binData) {
    // 把二进制数据写入FlowFile的Content
    flowFile = session.write(flowFile, { outputStream ->
        outputStream.write(binData)
    } as OutputStreamCallback)
    // 可选:删除原属性,避免后续流程干扰
    flowFile = session.removeAttribute(flowFile, 'BINFILE')
}

session.transfer(flowFile, REL_SUCCESS)

2. (可选)按需求先转Base64再解码

如果必须先将二进制转成Base64字符串,再解码回二进制保存,可添加以下两步:

  • 第一步:生成Base64属性
    用UpdateAttribute处理器,新增属性base64_pdf,值设为:
    ${file.attribute('BINFILE'):toBase64()}
    
  • 第二步:Base64解码回二进制写入Content
    再用一个ExecuteScript处理器,脚本如下:
    def flowFile = session.get()
    if (!flowFile) return
    
    def base64Str = flowFile.getAttribute('base64_pdf')
    if (base64Str) {
        def binData = Base64.getDecoder().decode(base64Str)
        flowFile = session.write(flowFile, { outputStream ->
            outputStream.write(binData)
        } as OutputStreamCallback)
    }
    
    session.transfer(flowFile, REL_SUCCESS)
    

3. 用PutFile保存PDF

配置PutFile处理器:

  • 设置Directory为目标保存路径
  • Filename可设为output.pdf或用表达式${uuid}.pdf生成唯一文件名
  • 冲突策略选Overwrite(根据需求调整)

为什么之前的方法无效?

  • UpdateAttribute:仅修改FlowFile的属性,不会改变Content内容,PutFile还是写入原始空/无关Content
  • ReplaceText:仅用于文本内容的替换,无法处理二进制数据,且操作对象是Content而非属性

内容的提问来源于stack exchange,提问作者Luiz Felipe Burgatt Jolo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 22:03:27