上传图片到淘宝API接口调用(代码展示)

接口名称:upload_img

传入图片imgcode:base64加密后的图片内容(post方式),或者是直接上传(file方式),将图片上传至淘宝,返回状态码、图片名称、图片URL.

测试页展示

上传图片到淘宝API接口调用(代码展示)_第1张图片

调用示例-数据说明文档

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.PrintWriter;
import java.net.URLConnection;

public class Example {
    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return  sb.toString();
    }
    public static JSONObject postRequestFromUrl(String url, String body) throws IOException, JSONException {
        URL realUrl = new URL(url);
        URLConnection conn = realUrl.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(body);
        out.flush();
        InputStream instream = conn.getInputStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(instream, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            JSONObject json = new JSONObject(jsonText);
            return json;
        } finally {
            instream.close();
        }
    }
    public static JSONObject getRequestFromUrl(String url) throws IOException, JSONException {
        URL realUrl = new URL(url);
        URLConnection conn = realUrl.openConnection();
        InputStream instream = conn.getInputStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(instream, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            JSONObject json = new JSONObject(jsonText);
            return json;
        } finally {
            instream.close();
        }
    }
    public static void main(String[] args) throws IOException, JSONException {
        // 请求示例 url 默认请求参数已经URL编码处理
        String url = "https://服务器/taobao/upload_img/?key=<您自己的apiKey>&secret=<您自己的apiSecret>&imgcode=https://img14.360buyimg.com/n0/jfs/t1/52280/38/7464/140698/5d511f6bE08290bd7/f0bb32ddb47451e8.jpg";
        JSONObject json = getRequestFromUrl(url);
        System.out.println(json.toString());
    }

}

你可能感兴趣的:(淘宝API接口,数据仓库,迁移学习,数据挖掘,人工智能,爬虫)