public void getImageV40(String path, Integer buildingProjectId, HttpServletResponse response){
JSONObject param = new JSONObject();
param.put("path", path);
ApiParam apiParam = setApiParam(ApiConstant.API_GET_IMAGE_CHAONAO, param);
RabbitMessage baseResult = getBaseResult(ROUTER_KEY_CN + buildingProjectId, apiParam, buildingProjectId);
byte[] buffer = (byte[]) baseResult.getMessageData();
try {
DataOutputStream dataOutputStream = new DataOutputStream(response.getOutputStream());
response.setContentType("image/*");
dataOutputStream.write(buffer,0,buffer.length);
dataOutputStream.flush();
dataOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
前置服务
switch (type) {
case ApiConstant.API_GET_IMAGE_CHAONAO:
String path = apiParam.getParam().get("path").toString();
String decode = URLDecoder.decode(path);
try {
//本地ip
String ipAddr=InetAddress.getLocalHost().getHostAddress();
if(path.contains(ipAddr)){
HttpResponse response= Unirest.get(path).asString();
String length = response.getHeaders().get("Content-Length").get(0);
byte[] buffer = new byte[Integer.parseInt(length)];
response.getRawBody().read(buffer);
content = buffer;
}else if(path.contains(portMachineUrl)) {
content=HttpClientUtil.getImageV40(decode,portMachineUrl.replaceAll("http://",""));
} else if(path.contains(portUrl)){
content=HttpClientUtil.getImageV40(decode,portUrl.replaceAll("http://",""));
}
} catch (Exception e) {
content = "Error accessing " + api + e.getMessage();
}
break;
}
public static Object getImageV40(String url, String host) throws IOException {
Object content = null;
CloseableHttpClient httpclient = digestAuth(host);
try {
HttpGet getMethod = new HttpGet(url);
org.apache.http.HttpResponse response = httpclient.execute(getMethod);
HttpEntity httpEntity = response.getEntity();
InputStream inputStream = httpEntity.getContent();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
baos.write(buffer, 0, len); //把读取到的内容写到输出流中
}
baos.flush();
baos.close();
content = baos.toByteArray();
} catch (Exception e) {
log.debug("推送失败:" + e);
} finally {
httpclient.close();
}
return content;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)