java 代码开启和关闭 h5 stream 录像功能
Posted jokerliuloss
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 代码开启和关闭 h5 stream 录像功能相关的知识,希望对你有一定的参考价值。
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.assertj.core.util.Lists;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
public class H5Service {
public static void main(String[] args) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
InputStream is = null;
String url = "http://你的路径/api/v1/ManualRecordStart?token=???&session=?????";
//封装请求参数
List<NameValuePair> params = Lists.newArrayList();
params.add(new BasicNameValuePair("cityEname", "henan"));
String str = "";
try {
//转换为键值对
str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
System.out.println("str"+str);
//创建Get请求
HttpGet httpGet = new HttpGet(url + "?" + str);
//执行Get请求,
response = httpClient.execute(httpGet);
//得到响应体
HttpEntity entity = response.getEntity();
if (entity != null) {
is = entity.getContent();
//转换为字节输入流
BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
StringBuffer stringBuffer = new StringBuffer();
String body = null;
while ((body = br.readLine()) != null) {
stringBuffer.append(body);
System.out.println(body);
}
String jsonBuffer = stringBuffer.toString();
JSONObject jo = new JSONObject(jsonBuffer);
System.out.println("路径是"+jo.get("strUrl"));
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
//关闭输入流,释放资源
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//消耗实体内容
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭相应 丢弃http连接
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// public static void main(String[] args) {
// String url ="http://114.67.96.32:8086";
//
//
// System.out.println("success"+doGet(url));
// }
@Test
public void shutdown() {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
InputStream is = null;
String url = "http://你的路径/api/v1/ManualRecordStop?token=???&session=?????????";
//封装请求参数
List<NameValuePair> params = Lists.newArrayList();
params.add(new BasicNameValuePair("cityEname", "henan"));
String str = "";
try {
//转换为键值对
str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
System.out.println("str"+str);
//创建Get请求
HttpGet httpGet = new HttpGet(url + "?" + str);
//执行Get请求,
response = httpClient.execute(httpGet);
//得到响应体
HttpEntity entity = response.getEntity();
if (entity != null) {
is = entity.getContent();
//转换为字节输入流
BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
String body = null;
while ((body = br.readLine()) != null) {
System.out.println(body);
}
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭输入流,释放资源
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//消耗实体内容
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭相应 丢弃http连接
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
以上是关于java 代码开启和关闭 h5 stream 录像功能的主要内容,如果未能解决你的问题,请参考以下文章
高效 告别996,开启java高效编程之门 3-5 Stream流概念解析
当相应的Stream关闭或我是否必须明确关闭它们时,Java FileChannel锁是否关闭?