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

如何使用NiFi将文本文件转为ByteArray并通过REST API发送

问题:将文本文件转为ByteArray并通过REST API发送的实现方案

我有一个文本文件,需要将其转换为ByteArray并通过REST API发送,请问该如何实现?

我尝试了多种将文件转为ByteArray的方法,最后一种是使用NiFi的ExecuteScript处理器执行以下Python代码:

import base64
import codecs

with codecs.open('/path/to/input/file.txt', 'r', encoding='utf-8') as file:
    file_content = file.read()
file_bytes = file_content.encode('utf-8')
encoded_bytes = base64.b64encode(file_bytes)
encoded_string = encoded_bytes.decode('utf-8')

flowFile = session.putAttribute(flowFile, 'fileContent', encoded_string)
session.transfer(flowFile, REL_SUCCESS)

但仍出现错误:

'utf-8' codec can't decode byte 0xff in position 0: unexpected code byte in

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 08:57:05