开发者社区> 问答> 正文

API 网关的POST如何上传文件?

API 网关的POST如何上传文件?

展开
收起
保持可爱mmm 2020-03-26 21:38:03 1836 0
1 条回答
写回答
取消 提交回答
  • 由于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非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));
    }
    
    2020-03-26 21:39:59
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Boot2.0实战Redis分布式缓存 立即下载
CUDA MATH API 立即下载
API PLAYBOOK 立即下载