开发者社区> 问答> 正文

ImageAsyncScanRequest使用时"msg":"[task.url] is not security" 求解答啊

/*

  • Licensed to the Apache Software Foundation (ASF) under one
  • or more contributor license agreements. See the NOTICE file
  • distributed with this work for additional information
  • regarding copyright ownership. The ASF licenses this file
  • to you under the Apache License, Version 2.0 (the
  • "License"); you may not use this file except in compliance
  • with the License. You may obtain a copy of the License at
    *
  • http://www.apache.org/licenses/LICENSE-2.0
    *
  • Unless required by applicable law or agreed to in writing,
  • software distributed under the License is distributed on an
  • "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  • KIND, either express or implied. See the License for the
  • specific language governing permissions and limitations
  • under the License.
    */

package com.ssh.acm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20170112.ImageAsyncScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
/**

  • Created by liuhai.lh on 2017/2/17.
  • 图片异步检测接口
  • @author liuhai.lh
  • @date 2017/02/17
    */

public class PicturetoWord {

public static void main(String[] args) throws Exception {
    //请替换成你自己的accessKeyId、accessKeySecret
    IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAIzuajFMA4u8jD", "KCgSEAApd3jI9pC4xEGkq6mOh2YXc9");
    DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    IAcsClient client = new DefaultAcsClient(profile);
    ImageAsyncScanRequest imageAsyncScanRequest = new ImageAsyncScanRequest();
    imageAsyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
    imageAsyncScanRequest.setContentType(FormatType.JSON);
    imageAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
    imageAsyncScanRequest.setEncoding("utf-8");
    imageAsyncScanRequest.setRegionId("cn-shanghai");
    List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
    Map<String, Object> task1 = new LinkedHashMap<String, Object>();
    task1.put("dataId", UUID.randomUUID().toString());
    task1.put("url", "https://localhost:8888/SSH/images/lixiang@163.com/111.jpg");
    task1.put("time", new Date());
    tasks.add(task1);
    JSONObject data = new JSONObject();
    /**
     * porn: 色情
     * terrorism: 暴恐
     * qrcode: 二维码
     * ad: 图片广告
     * ocr: 文字识别
     */
    data.put("scenes", Arrays.asList("ocr"));
    data.put("tasks", tasks);
    imageAsyncScanRequest.setContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    /**
     * 请务必设置超时时间
     */
    imageAsyncScanRequest.setConnectTimeout(3000);
    imageAsyncScanRequest.setReadTimeout(6000);
    try {
        HttpResponse httpResponse = client.doAction(imageAsyncScanRequest);
        if(httpResponse.isSuccess()){
            JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getContent(), "UTF-8"));
            System.out.println(JSON.toJSONString(scrResponse, true));
            if (200 == scrResponse.getInteger("code")) {
                JSONArray taskResults = scrResponse.getJSONArray("data");
                for (Object taskResult : taskResults) {
                    if(200 == ((JSONObject)taskResult).getInteger("code")){
                        String taskId = ((JSONObject)taskResult).getString("taskId");
                        // 将taskId 保存下来,间隔一段时间来轮询结果, 参照ImageAsyncScanResultsRequest
                        System.out.println("args = [" + taskId + "]");
                    }else{
                        System.out.println("task process fail:" + ((JSONObject)taskResult).getInteger("code"));
                    }
                }
            } else {
                System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
            }
        }else{
            System.out.println("response not success. status:" + httpResponse.getStatus());
        }
    } catch (ServerException e) {
        e.printStackTrace();
    } catch (ClientException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

展开
收起
CoolerWu 2017-07-27 20:53:21 6641 0
1 条回答
写回答
取消 提交回答
  • https://localhost:8888 这个地址是你本地的地址,如果是测试服务一般是 HTTP 协议,可以尝试将 https 改成http,或者换成图片实际的地址。

    2020-03-18 00:10:05
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载