From 585b721827af92b9a935bf0fa497786d49b393cb Mon Sep 17 00:00:00 2001 From: BinaryWang Date: Thu, 1 Sep 2016 09:57:03 +0800 Subject: [PATCH] fix potential resource leak --- ...lVoiceAndImageDownloadRequestExecutor.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java index 81bc2253..c40366ca 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java @@ -1,10 +1,12 @@ package me.chanjar.weixin.mp.util.http; -import me.chanjar.weixin.common.bean.result.WxError; -import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.common.util.http.InputStreamResponseHandler; -import me.chanjar.weixin.common.util.http.RequestExecutor; -import me.chanjar.weixin.common.util.json.WxGsonBuilder; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + import org.apache.commons.io.IOUtils; import org.apache.http.HttpHost; import org.apache.http.client.config.RequestConfig; @@ -13,12 +15,11 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; +import me.chanjar.weixin.common.bean.result.WxError; +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.util.http.InputStreamResponseHandler; +import me.chanjar.weixin.common.util.http.RequestExecutor; +import me.chanjar.weixin.common.util.json.WxGsonBuilder; public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor { @@ -44,9 +45,9 @@ public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExec Map params = new HashMap<>(); params.put("media_id", materialId); httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); - try(CloseableHttpResponse response = httpclient.execute(httpPost)){ + try (CloseableHttpResponse response = httpclient.execute(httpPost); + InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);){ // 下载媒体文件出错 - InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response); byte[] responseContent = IOUtils.toByteArray(inputStream); String responseContentString = new String(responseContent, "UTF-8"); if (responseContentString.length() < 100) {