使用Coldfusion通过REST API上传PDF至GCS时遇连接故障求助
问题分析
上传PDF文件时出现连接失败错误(record_overflow或Connection reset),但PNG文件可正常上传,核心问题在于:
- 使用
type="body"传递二进制内容时,ColdFusion会尝试按字符集解析数据,破坏PDF的二进制结构,文件较大时更易触发TLS记录溢出或连接重置。 - 给二进制上传请求设置
charset="utf-8"属于冗余操作,会干扰原始二进制数据的传输。
修复后的代码
<cffunction name="uploadFileToCloudStorage" access="remote" returnformat="JSON" returntype="any"> <cfargument name="bucket_name" default="" /> <cfargument name="object_name" default="" /> <cfargument name="object_location" default=""/> <cfargument name="object_content_type" default="application/pdf" /> <cfargument name="access_token" required="true" /> <cffile action="readBinary" file="#arguments.object_location#" variable="fileContent"/> <cfset endpoint = "https://storage.googleapis.com/upload/storage/v1/b/#arguments.bucket_name#/o?name=#arguments.object_name#&uploadType=media" /> <!--- 移除字符集设置,改用binary类型传递文件内容 ---> <cfhttp result="result" method="POST" url="#endpoint#" timeout="60"> <cfhttpparam type="header" name="Content-Type" value="#arguments.object_content_type#" /> <cfhttpparam type="header" name="Authorization" value="Bearer #arguments.access_token#" /> <cfhttpparam type="binary" value="#fileContent#" /> </cfhttp> <cfreturn replace(serializeJSON(result), '//', '') /> </cffunction>
关键修改点
- 移除
cfhttp标签的charset="utf-8"属性:二进制文件传输无需字符集设置,避免ColdFusion对二进制数据做不必要的字符编码转换。 - 将
cfhttpparam的type="body"改为type="binary":直接传递原始二进制字节流,确保PDF文件结构完整无修改。
额外验证建议
- 确认PDF文件路径正确,且ColdFusion进程拥有该文件的读取权限。
- 如果PDF文件体积超过10MB,建议改用GCS的分块上传(
uploadType=resumable),避免单次请求数据量过大触发连接限制。
内容的提问来源于stack exchange,提问作者Alpasp
相关产品推荐
相关产品推荐

