当服务为分布式的时候,你没有该数据库的情况,去访问另一个服务并获取图片

当服务为分布式的时候,你没有该数据库的情况,去访问另一个服务并获取缩略图

获取缩略图模块

ServiceImpl

// 写入的这个类 为工具类 调用其他服务的接口
    @Autowired
    private TaskUtil taskUtil;

@Override
    public ServiceResult queryWorkflowThumb(Integer workFlowId) {
        ServiceResult result = new ServiceResult(false);
        JSONObject jsonObject = taskUtil.queryWorkflowThumb(workFlowId.intValue());

        if (jsonObject.containsKey("image")) {
            result.success(jsonObject, "查询缩略图成功", 200);
        } else {
            result.failed(null, "查询缩略图失败", -1);
        }

        return result;
    }
补充
   JSONObject jsonObject = taskUtil.queryWorkflowThumb(workFlowId.intValue());

1.intValue()是java.lang.Number类的方法,Number是一个抽象类。Java中所有的数值类都继承它。也就是说,不单是Integer有intValue方法,Double,Long等都有此方法。
2.此方法的意思是:输出int数据。每个数值类中具体的实现是不同的。例如:
Float类和Double类的intValue方法,就是丢掉了小数位,而Long的intValue方法又不一样的

原文链接:https://blog.youkuaiyun.com/baidu_21578557/article/details/51212344
3. JSONObject https://www.cnblogs.com/jxd283465/p/11803510.html

运行流程
  • ServiceImpl 下queryWorkflowThumb方法 进入到 TaskUtil类下的queryWorkflowThumb方法
    //TaskUtils下
    //查询流程缩略图
    public JSONObject queryWorkflowThumb(int workFlowId) {
        Map<String, Object> paramMap = new HashMap<>();
        String serviceUrl = config.getWorkFlowServiceUrl() + "/metadata/workflow/" + workFlowId + "/findPicture";
        return clientConfiguration.sendGet(serviceUrl, paramMap);
    }

  • TaskUtil类的queryWorkflowThumb 又进入到 config下 获取路径
public class Config {

    @Value("${coordinator.url}")
    private String workFlowServiceUrl;
    }
  • 将获取的路径serviceUrl拼接后 ------》在返回给 clientConfiguration类下的 sendGet里
    return clientConfiguration.sendGet(serviceUrl, paramMap);
  • 调用该 sendGet方法
 public JSONObject sendGet(String url, Map<String, Object> paramMap) {
        HttpGet httpget = null;
        CloseableHttpResponse response = null;

        try {

            URIBuilder uriBuilder = new URIBuilder(url);
            List<NameValuePair> paramList = new LinkedList<>();
            for (String key : paramMap.keySet()) {
                Object value = paramMap.get(key);
                if (value != null) {
                    paramList.add(new BasicNameValuePair(key, value.toString()));
                }
            }

            uriBuilder.setParameters(paramList);
            CloseableHttpClient httpclient = this.httpclient;
            httpget = new HttpGet(uriBuilder.build());
            httpget.setConfig(this.defaultRequestConfig);
            response = httpclient.execute(httpget, HttpClientContext.create());
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("http请求" + url + "结果状态码为:" + statusCode);
            JSONObject jsonObject = null;
            if (statusCode == 200) {
                String contentType = response.getEntity().getContentType().getValue();

                //影像格式
                if (contentType.contains("image")) {
                    int imageSize = (int) response.getEntity().getContentLength();
                    if (imageSize > 0) {
                        byte imageData[] = new byte[imageSize];
                        response.getEntity().getContent().read(imageData);
                        Map<String, byte[]> tmp = new HashMap<>();
                        tmp.put("image", imageData);
                        jsonObject = JSON.parseObject(JSON.toJSONString(tmp));
                    }
                } else {
                    jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity()), Feature.OrderedField);
                }
            }

            return jsonObject;
        } catch (Exception e) {
            logger.error("catch Exception:", e);
            return null;
        } finally {
            if (response != null) {
                try {
                    response.getEntity().getContent().close();
                } catch (IOException e) {
                    logger.error("catch Exception:", e);
                }
            }
        }
    }

  • 注意sendGet方法
  • 这个CloseableHttpClient 自带的类

  • httpget = new HttpGet(uriBuilder.build()); 有setConfig参数

  • response = httpclient.execute(httpget, HttpClientContext.create()); 发出响应 的到json数据

  • statusCode 获取到状态码

        CloseableHttpClient httpclient = this.httpclient;
        httpget = new HttpGet(uriBuilder.build());                         		   httpget.setConfig(this.defaultRequestConfig);
         response = httpclient.execute(httpget,    				       	 		HttpClientContext.create());
         int statusCode = response.getStatusLine().getStatusCode();
        logger.info("http请求" + url + "结果状态码为:" + statusCode);
    
  • 注意sendGet方法2
  • 如果状态码200 说明成功 在进行如下操作

  • 分为一般json 和 图像缩略图

    ​ int imageSize = (int) response.getEntity().getContentLength(); // 获取成图像大小并转换成int型

    如果是影像 则判断图片的大小 大于0 进行如下操作

    if (imageSize > 0) {
                            byte imageData[] = new byte[imageSize];
                            response.getEntity().getContent().read(imageData);
                            Map<String, byte[]> tmp = new HashMap<>();
                            tmp.put("image", imageData);
                            jsonObject = JSON.parseObject(JSON.toJSONString(tmp));
                        }
    

    转换成字节码 并存入map中最后 以json形式发送

  if (statusCode == 200) {
                String contentType = response.getEntity().getContentType().getValue();

                //影像格式
                if (contentType.contains("image")) {
                    int imageSize = (int) response.getEntity().getContentLength();
                    if (imageSize > 0) {
                        byte imageData[] = new byte[imageSize];
                        response.getEntity().getContent().read(imageData);
                        Map<String, byte[]> tmp = new HashMap<>();
                        tmp.put("image", imageData);
                        jsonObject = JSON.parseObject(JSON.toJSONString(tmp));
                    }
                } else {
                    jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity()), Feature.OrderedField);
                }
            }

            return jsonObject;
TaskUtils

    @Autowired
    private Config config;

    @Autowired
    private ClientConfiguration clientConfiguration;

  

//查询流程缩略图
// 通过TaskUtile  TaskUtil中写入的config 下的获取路径方法 + 访问的路径+需要获取的ID+路径
// Url 是基本路径 + /metadata/workflow/ 进入的模块 + workFlowId 参数 + 路径
// 再通过 客户端配置工具类 发送请求 与 参数paramMap 条件
    public JSONObject queryWorkflowThumb(int workFlowId) {
        Map<String, Object> paramMap = new HashMap<>();
        String serviceUrl = config.getWorkFlowServiceUrl() + "/metadata/workflow/" + workFlowId + "/findPicture";
        return clientConfiguration.sendGet(serviceUrl, paramMap);
    }

Config (存储配置参数的)
clientConfiguration 工具类 客户端发送请求
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值