diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HttpClientManager.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HttpClientManager.java index 7d456a6707..8777143097 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HttpClientManager.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HttpClientManager.java @@ -6,6 +6,7 @@ import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.RenException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; @@ -326,6 +327,23 @@ public class HttpClientManager { } } + public Result getMediaByteArray(String url, String json) { + + try { + HttpPost httppost = new HttpPost(url); + httppost.setConfig(requestConfig); + httppost.addHeader("Content-Type", "application/json"); + if (StringUtils.isNotEmpty(json)) { + StringEntity se = new StringEntity(json, "utf-8"); + httppost.setEntity(se); + } + return executeToByte(httppost); + } catch (Exception e) { + log.error("sendGet exception", e); + return new Result().error(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg()); + } + } + private Result executeToByte(HttpRequestBase httpMethod) { CloseableHttpResponse response = null; try { @@ -334,14 +352,16 @@ public class HttpClientManager { if (response != null && response.getStatusLine() != null) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { InputStream in = response.getEntity().getContent(); - ByteArrayOutputStream output = new ByteArrayOutputStream(); - byte[] buffer = new byte[4096]; - int n = 0; - while (-1 != (n = in.read(buffer))) { - output.write(buffer, 0, n); - } + byte[] responseContent = IOUtils.toByteArray(in); +// ByteArrayOutputStream output = new ByteArrayOutputStream(); +// byte[] buffer = new byte[4096]; +// int n = 0; +// while (-1 != (n = in.read(buffer))) { +// output.write(buffer, 0, n); +// } // return new Result().ok(ArrayUtils.toObject(output.toByteArray())); - return new Result().ok(output.toByteArray()); +// return new Result().ok(output.toByteArray()); + return new Result().ok(responseContent); } else { log.warn("execute http method fail,httpStatus:{0}", response.getStatusLine().getStatusCode()); } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/impl/WxMaCodeServiceImpl.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/impl/WxMaCodeServiceImpl.java index 34c1931c88..7b09a3e440 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/impl/WxMaCodeServiceImpl.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/impl/WxMaCodeServiceImpl.java @@ -204,7 +204,7 @@ public class WxMaCodeServiceImpl implements WxMaCodeService { public WxResult getMaterial(String accessToken, WxMaNewsReq request) { WxResult result = new WxResult<>(); String url = WxMaCodeConstant.GET_MATERIAL_URL + "?" + "access_token=" + accessToken; - Result statusResult = HttpClientManager.getInstance().getByteArray(url, toMap(request)); + Result statusResult = HttpClientManager.getInstance().getMediaByteArray(url, toJson(request)); if (!statusResult.success()) { result.setErrorCode(statusResult.getCode()); result.setErrorMsg(statusResult.getMsg());