七牛三方图片鉴黄

2017-05-25  本文已影响725人  匿名类

项目需要做图片鉴黄,而且目前图片没有存放在七牛,所以选择了调用七牛三方的接口
阿塔科技图片鉴黄服务,文档地址,选择方式二
https://developer.qiniu.com/dora/manual/1295/image-as-a-yellow-services-stage#pulp2-title
文档写的比较差,走了不少弯路.

开通需要先发工单请求开通
下面上干货

maven 配置

<dependencies>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.3.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>happy-dns-java</artifactId>
            <version>0.1.4</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
            <version>7.2.0</version>
        </dependency>

    </dependencies>

java核心代码

因为涉及到 qiniu/mac qbox/mac 鉴权,需要自己再组织一下原有sdk 的方法


import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.qiniu.util.StringUtils;


        String accessKey = "---ak---";
        String secretKey = "---sk---";
        Auth auth = Auth.create(accessKey, secretKey);
        Client client = new Client();
        String url = "http://pulp.ataraxia.ai/pulp";
        #官方文档图片参数写的很恶心~~~顺便鄙视一下
        String body = "image=替换成图片1url&&image=替换成图片2url";
        byte[] bodyByte = StringUtils.utf8Bytes(body);
        String mimeType = "application/x-www-form-urlencoded";
        StringMap headers = auth.authorization(url, bodyByte, mimeType);

        headers.put("Content-Type", mimeType);
        try {
            Response rep = client.post(url, bodyByte, headers, mimeType);
            System.out.println(rep.bodyString());

        } catch (Exception e) {
            e.printStackTrace();
        }

上一篇下一篇

猜你喜欢

热点阅读