当服务为分布式的时候,你没有该数据库的情况,去访问另一个服务并获取缩略图
获取缩略图模块
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
运行流程
//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);
}
public class Config {
@Value("${coordinator.url}")
private String workFlowServiceUrl;
}
return clientConfiguration.sendGet(serviceUrl, paramMap);
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);
}