Commit 055d34a1 by 高晓磊

把萤石云工具类camera移到common里边

parent b9534287
package com.xyst.dinas.camera.util;
package com.beecode.inz.common.util;
import com.xyst.dinas.camera.constant.CameraPlayConstant;
import com.xyst.dinas.camera.util.PTZVo;
import com.beecode.inz.common.constant.CameraPlayConstant;
import org.apache.http.HttpException;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
/**
* @author scol
......@@ -25,161 +21,6 @@ public class CameraContrUtil implements CameraPlayConstant {
return "camera_pic_key" + deviceSerial + "_" + channelNo;
}
/**
* 向指定URL发送GET方法的请求
*
* @param url 发送请求的URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param) throws Exception {
StringBuilder result = new StringBuilder();
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 遍历所有的响应头字段
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
throw new Exception("发送GET请求出现异常");
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result.toString();
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式,outformate,out流中文的格式。
* @return 所代表远程资源的响应结果
* @author zouli
* @date 2019年7月22日 下午1:47:41
*/
public static String sendPost(String url, String param, String outformate) throws Exception {
OutputStreamWriter out = null;
BufferedReader in = null;
String result;
try {
URLConnection conn = getUrlConnection(url);
// 获取URLConnection对象对应的输出流
out = new OutputStreamWriter(conn.getOutputStream(), outformate);
// 发送请求参数
out.write(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String lines;
StringBuilder sb = new StringBuilder();
while ((lines = in.readLine()) != null) {
lines = new String(lines.getBytes(), outformate);
sb.append(lines);
}
result = sb.toString();
} catch (Exception e) {
throw new Exception("发送 POST 请求出现异常!");
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
private static URLConnection getUrlConnection(String url) throws IOException {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
return conn;
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) throws Exception {
PrintWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try {
URLConnection conn = getUrlConnection(url);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
throw new Exception("发送请求异常");
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result.toString();
}
/**
* @return PTZVo
......@@ -195,7 +36,7 @@ public class CameraContrUtil implements CameraPlayConstant {
channelNo = 1;
}
String postParam = "accessToken=" + accessToken + "&deviceSerial=" + deviceSerial;
String sendPost = sendPost(CAMERA_LIST_URL, postParam, "utf-8");
String sendPost = HttpSendUtil.sendPost(CameraPlayConstant.CAMERA_LIST_URL, postParam, "utf-8");
JSONObject postStr = new JSONObject(sendPost);
String code = postStr.get("code").toString();
......@@ -233,7 +74,7 @@ public class CameraContrUtil implements CameraPlayConstant {
channelNo = 1;
}
String postParam = "accessToken=" + accessToken + "&deviceSerial=" + deviceSerial + "&recType=" + recType + "&startTime=" + startTime + "&endTime=" + endTime;
String sendPost = sendPost(VIDEO_FILE_URL, postParam, "utf-8");
String sendPost = HttpSendUtil.sendPost(CameraPlayConstant.VIDEO_FILE_URL, postParam, "utf-8");
JSONObject postStr = new JSONObject(sendPost);
String code = postStr.get("code").toString();
......@@ -269,7 +110,7 @@ public class CameraContrUtil implements CameraPlayConstant {
}
try {
String postParam = "accessToken=" + accessToken + "&deviceSerial=" + deviceSerial + "&channelNo=" + channelNo;
String sendPost = sendPost(CAPTURE_URL, postParam, "utf-8");
String sendPost = HttpSendUtil.sendPost(CameraPlayConstant.CAPTURE_URL, postParam, "utf-8");
JSONObject postStr = new JSONObject(sendPost);
String code = postStr.get("code").toString();
if ("200".equals(code)) {
......@@ -303,7 +144,7 @@ public class CameraContrUtil implements CameraPlayConstant {
try {
String postParam = "accessToken=" + accessToken + "&deviceSerial=" + deviceSerial + "&channelNo=" + channelNo + "&index="
+ index;
doSendPost(pztVo, postParam, MOVE_PRESET_URL);
doSendPost(pztVo, postParam, CameraPlayConstant.MOVE_PRESET_URL);
} catch (Exception ex) {
ex.printStackTrace();
......@@ -313,7 +154,7 @@ public class CameraContrUtil implements CameraPlayConstant {
}
private static void doSendPost(PTZVo pztVo, String postParam, String movePresetUrl) throws Exception {
String sendPost = sendPost(movePresetUrl, postParam, "utf-8");
String sendPost = HttpSendUtil.sendPost(movePresetUrl, postParam, "utf-8");
JSONObject postStr = new JSONObject(sendPost);
String code = postStr.get("code").toString();
if ("200".equals(code)) {
......@@ -337,7 +178,7 @@ public class CameraContrUtil implements CameraPlayConstant {
pztVo.setIsSuccess(false);
try {
String postParam = "accessToken=" + accessToken + "&deviceSerial=" + deviceSerial + "&channelNo=1";
String sendPost = sendPost(ADD_PRESET_URL, postParam, "utf-8");
String sendPost = HttpSendUtil.sendPost(CameraPlayConstant.ADD_PRESET_URL, postParam, "utf-8");
JSONObject postStr = new JSONObject(sendPost);
String code = postStr.get("code").toString();
if ("200".equals(code)) {
......@@ -371,7 +212,7 @@ public class CameraContrUtil implements CameraPlayConstant {
try {
String postParam = "accessToken=" + accessToken + "&deviceSerial=" + deviceSerial + "&channelNo=1&index="
+ index;
doSendPost(pztVo, postParam, CLEAR_PRESET_URL);
doSendPost(pztVo, postParam, CameraPlayConstant.CLEAR_PRESET_URL);
} catch (Exception ex) {
ex.printStackTrace();
......@@ -419,7 +260,7 @@ public class CameraContrUtil implements CameraPlayConstant {
pztVo.setIsSuccess(false);
try {
String postParam = "accessToken=" + accessToken + "&deviceSerial=" + deviceSerial + "&channelNo=" + channelNo;
String sendPost = sendPost(CAPTURE_URL, postParam, "utf-8");
String sendPost = HttpSendUtil.sendPost(CameraPlayConstant.CAPTURE_URL, postParam, "utf-8");
JSONObject postStr = new JSONObject(sendPost);
String code = postStr.get("code").toString();
if ("200".equals(code)) {
......@@ -431,7 +272,7 @@ public class CameraContrUtil implements CameraPlayConstant {
//移动
postParam = "accessToken=" + accessToken + "&deviceSerial=" + deviceSerial + "&channelNo=" + channelNo + "&index="
+ index;
sendPost = sendPost(MOVE_PRESET_URL, postParam, "utf-8");
sendPost = HttpSendUtil.sendPost(CameraPlayConstant.MOVE_PRESET_URL, postParam, "utf-8");
postStr = new JSONObject(sendPost);
pztVo.setIsSuccess(true);
}
......@@ -464,7 +305,7 @@ public class CameraContrUtil implements CameraPlayConstant {
try {
String postParam = "accessToken=" + accessToken + "&deviceSerial=" + deviceSerial + "&channelNo=" + channelNo + "&direction="
+ direction + "&speed=" + speed;
doSendPost(pztVo, postParam, CONTR_PIZ_START_URL);
doSendPost(pztVo, postParam, CameraPlayConstant.CONTR_PIZ_START_URL);
} catch (Exception ex) {
ex.printStackTrace();
......@@ -489,7 +330,7 @@ public class CameraContrUtil implements CameraPlayConstant {
try {
String postParam = "accessToken=" + accessToken + "&deviceSerial=" + deviceSerial + "&channelNo=" + channelNo + "&direction="
+ direction;
doSendPost(pztVo, postParam, CONTR_PIZ_STOP_URL);
doSendPost(pztVo, postParam, CameraPlayConstant.CONTR_PIZ_STOP_URL);
} catch (Exception ex) {
ex.printStackTrace();
......@@ -507,7 +348,7 @@ public class CameraContrUtil implements CameraPlayConstant {
public static JSONObject getToken(String appKey, String appSecret) throws Exception {
String postParam = "appKey=" + appKey + "&appSecret=" + appSecret;
String sendPost = sendPost(TOKEN_URL, postParam, "utf-8");
String sendPost = HttpSendUtil.sendPost(CameraPlayConstant.TOKEN_URL, postParam, "utf-8");
JSONObject postStr = new JSONObject(sendPost);
String code = postStr.get("code").toString();
if ("200".equals(code)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment