Skip to content

Commit 585b721

Browse files
committed
fix potential resource leak
1 parent be444be commit 585b721

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package me.chanjar.weixin.mp.util.http;
22

3-
import me.chanjar.weixin.common.bean.result.WxError;
4-
import me.chanjar.weixin.common.exception.WxErrorException;
5-
import me.chanjar.weixin.common.util.http.InputStreamResponseHandler;
6-
import me.chanjar.weixin.common.util.http.RequestExecutor;
7-
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
3+
import java.io.ByteArrayInputStream;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
810
import org.apache.commons.io.IOUtils;
911
import org.apache.http.HttpHost;
1012
import org.apache.http.client.config.RequestConfig;
@@ -13,12 +15,11 @@
1315
import org.apache.http.entity.StringEntity;
1416
import org.apache.http.impl.client.CloseableHttpClient;
1517

16-
import java.io.ByteArrayInputStream;
17-
import java.io.File;
18-
import java.io.IOException;
19-
import java.io.InputStream;
20-
import java.util.HashMap;
21-
import java.util.Map;
18+
import me.chanjar.weixin.common.bean.result.WxError;
19+
import me.chanjar.weixin.common.exception.WxErrorException;
20+
import me.chanjar.weixin.common.util.http.InputStreamResponseHandler;
21+
import me.chanjar.weixin.common.util.http.RequestExecutor;
22+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
2223

2324
public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor<InputStream, String> {
2425

@@ -44,9 +45,9 @@ public InputStream execute(CloseableHttpClient httpclient, HttpHost httpProxy, S
4445
Map<String, String> params = new HashMap<>();
4546
params.put("media_id", materialId);
4647
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
47-
try(CloseableHttpResponse response = httpclient.execute(httpPost)){
48+
try (CloseableHttpResponse response = httpclient.execute(httpPost);
49+
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);){
4850
// 下载媒体文件出错
49-
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
5051
byte[] responseContent = IOUtils.toByteArray(inputStream);
5152
String responseContentString = new String(responseContent, "UTF-8");
5253
if (responseContentString.length() < 100) {

0 commit comments

Comments
 (0)