Commit 9dd532eb0b040b0170c1aacfef221a6c8606067d
Committed by
GitHub
Merge pull request #175 from chenparty/wvp-28181-2.0
修复快照无法生成的BUG
Showing
1 changed file
with
20 additions
and
20 deletions
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
| ... | ... | @@ -89,32 +89,29 @@ public class ZLMRESTfulUtils { |
| 89 | 89 | return responseJSON; |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - | |
| 93 | - public void sendPostForImg(MediaServerItem mediaServerItem, String api, Map<String, Object> param, String targetPath, String fileName) { | |
| 94 | - OkHttpClient client = new OkHttpClient(); | |
| 95 | - String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api); | |
| 96 | - JSONObject responseJSON = null; | |
| 92 | + public void sendGetForImg(MediaServerItem mediaServerItem, String api, Map<String, Object> params, String targetPath, String fileName) { | |
| 93 | + String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api); | |
| 97 | 94 | logger.debug(url); |
| 95 | + HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder(); | |
| 98 | 96 | |
| 99 | - FormBody.Builder builder = new FormBody.Builder(); | |
| 100 | - builder.add("secret",mediaServerItem.getSecret()); | |
| 101 | - if (param != null && param.keySet().size() > 0) { | |
| 102 | - for (String key : param.keySet()){ | |
| 103 | - if (param.get(key) != null) { | |
| 104 | - builder.add(key, param.get(key).toString()); | |
| 105 | - } | |
| 97 | + httpBuilder.addQueryParameter("secret", mediaServerItem.getSecret()); | |
| 98 | + if (params != null) { | |
| 99 | + for (Map.Entry<String, Object> param : params.entrySet()) { | |
| 100 | + httpBuilder.addQueryParameter(param.getKey(), param.getValue().toString()); | |
| 106 | 101 | } |
| 107 | 102 | } |
| 108 | 103 | |
| 109 | - FormBody body = builder.build(); | |
| 110 | - | |
| 111 | 104 | Request request = new Request.Builder() |
| 112 | - .post(body) | |
| 113 | - .url(url) | |
| 105 | + .url(httpBuilder.build()) | |
| 114 | 106 | .build(); |
| 107 | + logger.info(request.toString()); | |
| 115 | 108 | try { |
| 109 | + OkHttpClient client = new OkHttpClient.Builder() | |
| 110 | + .readTimeout(10, TimeUnit.SECONDS) | |
| 111 | + .build(); | |
| 116 | 112 | Response response = client.newCall(request).execute(); |
| 117 | 113 | if (response.isSuccessful()) { |
| 114 | + logger.info("response body contentType: " + Objects.requireNonNull(response.body()).contentType()); | |
| 118 | 115 | if (targetPath != null) { |
| 119 | 116 | File snapFolder = new File(targetPath); |
| 120 | 117 | if (!snapFolder.exists()) { |
| ... | ... | @@ -124,18 +121,21 @@ public class ZLMRESTfulUtils { |
| 124 | 121 | FileOutputStream outStream = new FileOutputStream(snapFile); |
| 125 | 122 | outStream.write(response.body().bytes()); |
| 126 | 123 | outStream.close(); |
| 124 | + } else { | |
| 125 | + logger.error(String.format("[ %s ]请求失败: %s %s", url, response.code(), response.message())); | |
| 127 | 126 | } |
| 127 | + response.body().close(); | |
| 128 | + } else { | |
| 129 | + logger.error(String.format("[ %s ]请求失败: %s %s", url, response.code(), response.message())); | |
| 128 | 130 | } |
| 129 | 131 | } catch (ConnectException e) { |
| 130 | 132 | logger.error(String.format("连接ZLM失败: %s, %s", e.getCause().getMessage(), e.getMessage())); |
| 131 | 133 | logger.info("请检查media配置并确认ZLM已启动..."); |
| 132 | - }catch (IOException e) { | |
| 134 | + } catch (IOException e) { | |
| 133 | 135 | logger.error(String.format("[ %s ]请求失败: %s", url, e.getMessage())); |
| 134 | 136 | } |
| 135 | - | |
| 136 | 137 | } |
| 137 | 138 | |
| 138 | - | |
| 139 | 139 | public JSONObject getMediaList(MediaServerItem mediaServerItem, String app, String stream, String schema, RequestCallback callback){ |
| 140 | 140 | Map<String, Object> param = new HashMap<>(); |
| 141 | 141 | if (app != null) param.put("app",app); |
| ... | ... | @@ -252,6 +252,6 @@ public class ZLMRESTfulUtils { |
| 252 | 252 | param.put("url", flvUrl); |
| 253 | 253 | param.put("timeout_sec", timeout_sec); |
| 254 | 254 | param.put("expire_sec", expire_sec); |
| 255 | - sendPostForImg(mediaServerItem, "getSnap",param, targetPath, fileName); | |
| 255 | + sendGetForImg(mediaServerItem, "getSnap", param, targetPath, fileName); | |
| 256 | 256 | } |
| 257 | 257 | } | ... | ... |