图片上传到图片空间

图片上传到图片空间

1.请求参数:

请求URL:

POST https://kf.fw199.com/gateway/pdd/goods/file/space/image/upload

1.1 基础参数
参数名 类型 必须 说明
appid String 必填 合作伙伴AppId
timestamp String 必填 当前时间戳
seller_nick String 必填 拼多多账号, 非店铺名称
sign String 必填 如何计算生成见示例代码
1.2 业务参数
参数接口 参数类型 是否必填 说明
file File 与img_url 2选1 标准的http file上传,字段为file
img_url STRING 与file 2选1 如果img_url不为空,优先使用img_url

2. 请求示例代码(Java)

2.1 上传本地文件
     @Test
    public void PddGoodsImageUploadRequestByFile() throws Exception {

        String seller_nick = Config.PddSellerNick;  
        //业务参数
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("appid", Config.AppId);
        data.put("seller_nick", seller_nick);
        Long timestamp = System.currentTimeMillis() / 1000;
        data.put("timestamp", timestamp.toString());
        String   filePath = "/Users/miller/Downloads/千里马logo.jpg";
        data.put("sign",  Utils.SignObject(data,Config.AppSecret));
        String resp = doHttpRequest(Config.PddFileSpaceImageUploadUrl, data,filePath);
        System.out.println(resp); 
    }
    }
2.2 通过url上传文件

给定图片url上传图片文件到图片空间。

     @Test
    public void PddGoodsImageUploadRequestByUrl() throws Exception {

        String seller_nick = Config.PddSellerNick;  
        //业务参数
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("appid", Config.AppId);
        data.put("seller_nick", seller_nick);
        Long timestamp = System.currentTimeMillis() / 1000;
        data.put("timestamp", timestamp.toString());
        data.put("img_url","https://cbu01.alicdn.com/img/ibank/O1CN0177fW3T1J4b843MbF0_!!3482710975-0-cib.jpg" );
        data.put("sign",  Utils.SignObject(data,Config.AppSecret));
        String resp = doHttpRequest(Config.PddFileSpaceImageUploadUrl, data,"");
        System.out.println(resp); 
    }


2.3 上传文件的http请求示例
  private  String doHttpRequest(String url ,  Map<String, Object> data, String filePath){

        String result ="";
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        System.out.println("请求Url:" +url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            builder.addTextBody(entry.getKey(), entry.getValue().toString(), ContentType.create("text/plain", "UTF-8")); // 添加其他字段
        }
        // 如果有文件上传
        if (!"".equals(filePath)) {
            builder.addBinaryBody("file", new File(filePath), ContentType.DEFAULT_BINARY, new File(filePath).getName()); // 添加文件
        }
        // 设置请求体
        HttpEntity entity = builder.build();
        httpPost.setEntity(entity);
        //发起POST请求
        try {
            HttpResponse httpResponse = httpclient.execute(httpPost);
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result =  EntityUtils.toString(httpResponse.getEntity());
            } else {
                result =  ("doPost Error Response: " + httpResponse.getStatusLine().toString());
            }
        } catch (Exception e) {
            e.printStackTrace();

        }

        return result;
    }

3. 返回结果

返回结果如下 。

 {
  "code": 0,
  "data": {
    "file_id": 64518234124,
    "url": "https://img.pddpic.com/garner-api-open/2025-08-09/3f0e14b51f3e99bd9abd0fdcbf7bea66.jpg"
  },
  "message": "ok"
}

说明: code为0表示成功,非0为失败,message会包含失败原因。

4. 返回字段说明

参数接口 参数类型 例子 说明
file_id LONG 文件id
url STRING 上传到图片空间以后的url
文档更新时间: 2025-08-09 22:22   作者:admin