POST调用——文件上传

由于API网关暂不支持multipart形式的文件上传,所以需要客户端调用时将文件内容转化为二进制的Base64编码,传给网关。后端收到数据内容,也需要进行相应的Base64解码获得文件流。

1、API配置

一般包含文件上传的API接口,建议配置为“非Form表单”。非Form表单的数据不会参与签名,而是通过content-md5去防数据篡改。

2、Content-Type

1) body内容为一个JSON格式内容,Content-Type可配置为”application/json; charset=UTF-8”。

调用示例,如调用API 印刷文字识别_身份证识别

body内容为一个JSON格式,其中的参数dataValue为图片内容,传输时需要将图片内容转化为二进制的Base64编码。

    public static void main(String[] args) {
        //请求path
        String host = "https://dm-51.data.aliyun.com";
        String path = "/rest/160601/ocr/ocr_idcard.json";
        String appKey=“你自己的AppKey”;
        String appSecret=“你自己的AppSecret”

        //图片转化为二进制的Base64编码字符串
        String imgFile="/Users/wuling/Downloads/dm-51示例图片.jpg";
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String base64String=new String(Base64.encodeBase64(data));
        //Body内容
        String body = "{\"inputs\":[{\"image\":{\"dataType\":50,\"dataValue\":\"" + base64String
            + "\"},\"configure\":{\"dataType\":50,\"dataValue\":\"{\\\"side\\\":\\\"face\\\"}\"}}]}";

        Map<String, String> headers = new HashMap<String, String>();
        //(必填)根据期望的Response内容类型设置
        headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "application/json");
        //(可选)Body MD5,服务端会校验Body内容是否被篡改,//(可选)Body MD5,服务端会校验Body内容是否被篡改,建议Body非Form表单时添加此Header建议Body非Form表单时添加此Header
        headers.put(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(body));
        //(POST/PUT请求必选)请求Body内容格式
        headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, "application/json; charset=UTF-8");

        Request request = new Request(Method.POST_STRING, host, path, appKey, appSecret, Constants.DEFAULT_TIMEOUT);
        request.setHeaders(headers);
        request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);

        request.setStringBody(body);
        /**
         * 重要提示如下:
         * 代码中用到的类请下载 https://github.com/aliyun/api-gateway-demo-sign-java
         *
         * 具体调用请参考https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/test/java/com/aliyun/api/gateway/demo/Demo.java
         **/
        //调用服务端
        Response response = Client.execute(request);

        System.out.println(JSON.toJSONString(response));
    }		

2) body内容仅为文件内容,Content-Type可配置为"application/octet-stream; charset=UTF-8"。

调用示例,如调用API 九云图文档转 H5(SVG)网页-文档转换POST

	public static void main(String[] args) {
	     //请求path
        String host = "https://api.9yuntu.cn";
        String path = "/execute/PostConvert";
        String appKey=“你自己的AppKey”;
        String appSecret=“你自己的AppSecret”;

        //文件转化为二进制的Base64编码字符串
        String imgFile="/Users/wuling/Downloads/demo.docx";
        InputStream in = null;
        byte[] data = null;
        //读取字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String body=new String(Base64.encodeBase64(data));

        Map<String, String> headers = new HashMap<String, String>();
        //(必填)根据期望的Response内容类型设置
        headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "application/json");
        //(可选)Body MD5,服务端会校验Body内容是否被篡改,建议Body非Form表单时添加此Header
        headers.put(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(body));
        //(POST/PUT请求必选)请求Body内容格式
        headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, "application/octet-stream; charset=UTF-8");

        Request request = new Request(Method.POST_STRING, host, path, appKey, appSecret, Constants.DEFAULT_TIMEOUT);
        request.setHeaders(headers);
        request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);

        request.setStringBody(body);

        //请求的query
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("docName", "demo");
        querys.put("outputType", "html");
        request.setQuerys(querys);
        /**
         * 重要提示如下:
         * 代码中用到的常量请下载 https://github.com/aliyun/api-gateway-demo-sign-java
         *
         * 具体调用请参考https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/test/java/com/aliyun/api/gateway/demo/Demo.java
         **/
        //调用服务端
        Response response = Client.execute(request);

        System.out.println(JSON.toJSONString(response));
    }