Merge pull request #175 from chenparty/wvp-28181-2.0

修复快照无法生成的BUG
This commit is contained in:
648540858 2021-09-26 15:51:09 +08:00 committed by GitHub
commit aaa1ed344a

View File

@ -89,32 +89,29 @@ public class ZLMRESTfulUtils {
return responseJSON; return responseJSON;
} }
public void sendGetForImg(MediaServerItem mediaServerItem, String api, Map<String, Object> params, String targetPath, String fileName) {
public void sendPostForImg(MediaServerItem mediaServerItem, String api, Map<String, Object> param, String targetPath, String fileName) {
OkHttpClient client = new OkHttpClient();
String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api); String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api);
JSONObject responseJSON = null;
logger.debug(url); logger.debug(url);
HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder();
FormBody.Builder builder = new FormBody.Builder(); httpBuilder.addQueryParameter("secret", mediaServerItem.getSecret());
builder.add("secret",mediaServerItem.getSecret()); if (params != null) {
if (param != null && param.keySet().size() > 0) { for (Map.Entry<String, Object> param : params.entrySet()) {
for (String key : param.keySet()){ httpBuilder.addQueryParameter(param.getKey(), param.getValue().toString());
if (param.get(key) != null) {
builder.add(key, param.get(key).toString());
} }
} }
}
FormBody body = builder.build();
Request request = new Request.Builder() Request request = new Request.Builder()
.post(body) .url(httpBuilder.build())
.url(url)
.build(); .build();
logger.info(request.toString());
try { try {
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(10, TimeUnit.SECONDS)
.build();
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
if (response.isSuccessful()) { if (response.isSuccessful()) {
logger.info("response body contentType: " + Objects.requireNonNull(response.body()).contentType());
if (targetPath != null) { if (targetPath != null) {
File snapFolder = new File(targetPath); File snapFolder = new File(targetPath);
if (!snapFolder.exists()) { if (!snapFolder.exists()) {
@ -124,18 +121,21 @@ public class ZLMRESTfulUtils {
FileOutputStream outStream = new FileOutputStream(snapFile); FileOutputStream outStream = new FileOutputStream(snapFile);
outStream.write(response.body().bytes()); outStream.write(response.body().bytes());
outStream.close(); outStream.close();
} else {
logger.error(String.format("[ %s ]请求失败: %s %s", url, response.code(), response.message()));
} }
response.body().close();
} else {
logger.error(String.format("[ %s ]请求失败: %s %s", url, response.code(), response.message()));
} }
} catch (ConnectException e) { } catch (ConnectException e) {
logger.error(String.format("连接ZLM失败: %s, %s", e.getCause().getMessage(), e.getMessage())); logger.error(String.format("连接ZLM失败: %s, %s", e.getCause().getMessage(), e.getMessage()));
logger.info("请检查media配置并确认ZLM已启动..."); logger.info("请检查media配置并确认ZLM已启动...");
}catch (IOException e) { } catch (IOException e) {
logger.error(String.format("[ %s ]请求失败: %s", url, e.getMessage())); logger.error(String.format("[ %s ]请求失败: %s", url, e.getMessage()));
} }
} }
public JSONObject getMediaList(MediaServerItem mediaServerItem, String app, String stream, String schema, RequestCallback callback){ public JSONObject getMediaList(MediaServerItem mediaServerItem, String app, String stream, String schema, RequestCallback callback){
Map<String, Object> param = new HashMap<>(); Map<String, Object> param = new HashMap<>();
if (app != null) param.put("app",app); if (app != null) param.put("app",app);
@ -252,6 +252,6 @@ public class ZLMRESTfulUtils {
param.put("url", flvUrl); param.put("url", flvUrl);
param.put("timeout_sec", timeout_sec); param.put("timeout_sec", timeout_sec);
param.put("expire_sec", expire_sec); param.put("expire_sec", expire_sec);
sendPostForImg(mediaServerItem, "getSnap",param, targetPath, fileName); sendGetForImg(mediaServerItem, "getSnap", param, targetPath, fileName);
} }
} }