fix potential resource leak

This commit is contained in:
BinaryWang 2016-09-01 09:57:03 +08:00
parent be444be554
commit 585b721827

View File

@ -1,10 +1,12 @@
package me.chanjar.weixin.mp.util.http; package me.chanjar.weixin.mp.util.http;
import me.chanjar.weixin.common.bean.result.WxError; import java.io.ByteArrayInputStream;
import me.chanjar.weixin.common.exception.WxErrorException; import java.io.File;
import me.chanjar.weixin.common.util.http.InputStreamResponseHandler; import java.io.IOException;
import me.chanjar.weixin.common.util.http.RequestExecutor; import java.io.InputStream;
import me.chanjar.weixin.common.util.json.WxGsonBuilder; import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import java.io.ByteArrayInputStream; import me.chanjar.weixin.common.bean.result.WxError;
import java.io.File; import me.chanjar.weixin.common.exception.WxErrorException;
import java.io.IOException; import me.chanjar.weixin.common.util.http.InputStreamResponseHandler;
import java.io.InputStream; import me.chanjar.weixin.common.util.http.RequestExecutor;
import java.util.HashMap; import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import java.util.Map;
public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor<InputStream, String> { public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor<InputStream, String> {
@ -44,9 +45,9 @@ public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExec
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("media_id", materialId); params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); 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); byte[] responseContent = IOUtils.toByteArray(inputStream);
String responseContentString = new String(responseContent, "UTF-8"); String responseContentString = new String(responseContent, "UTF-8");
if (responseContentString.length() < 100) { if (responseContentString.length() < 100) {