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

GraphQL文件上传报错:HttpPostRequestEncoder异常求助

GraphQL图片上传报错:FileNotFoundException 排查与解决

我在使用Karate框架通过GraphQL上传图片bird.jpeg时,遇到如下错误:

io.netty.handler.codec.http.multipart.HttpPostRequestEncoder$ErrorDataEncoderException: java.io.FileNotFoundException

相关代码信息

传入数据

{
  "file":"bird.jpeg"
}

GraphQL查询语句

mutation UpdateProfilePicture ($file: Upload!) {
    singleUpload(file: $file){
    done
}
}

Karate Feature文件代码

Feature: get parent players

    Background:
    * url baseUrl
    Scenario: get parent players details

    * def result = call read(loginPath)
    * def accessToken = result.accessToken
    * print "Access Token : ", accessToken
    And header Authorization = 'Bearer ' + accessToken

    Given def query = read(gqlBase +'updateProfilePicture.gql')
    And request { query: '#(query)'}
    * multipart file file = {read:'bird.jpeg', filename:'bird.jpeg',Content-type:'image/jpeg'}
    When method post
    Then status 200
    * print 'response: ', response.data

排查与解决步骤

  1. 核对文件路径
    • 确保bird.jpeg在Karate可读取的路径下:如果用相对路径,基准是当前Feature文件所在目录;也可以直接使用绝对路径明确指定位置,比如read:'src/test/resources/bird.jpeg'。
  2. 确认文件存在性
    • 手动检查目标路径下是否真的存在bird.jpeg,注意文件名大小写(部分系统区分大小写,Bird.jpeg和bird.jpeg会被识别为不同文件)。
  3. 修正Multipart参数格式
    • Karate中multipart file的Content-type键名需改为contentType(小写开头,无连字符),修正后代码:
      * multipart file file = { read: 'bird.jpeg', filename: 'bird.jpeg', contentType: 'image/jpeg' }
      
  4. 调整GraphQL上传请求结构
    • GraphQL文件上传需遵循multipart/form-data规范,需将查询语句、变量映射与文件分开传递,示例代码:
      * def query = read(gqlBase +'updateProfilePicture.gql')
      * def operations = { query: '#(query)', variables: { file: null } }
      * def map = { "0": ["variables.file"] }
      * multipart field operations = operations
      * multipart field map = map
      * multipart file 0 = { read: 'bird.jpeg', filename: 'bird.jpeg', contentType: 'image/jpeg' }
      

内容的提问来源于stack exchange,提问作者Dhamotharan R

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 02:33:17