From 8610c0f0af8ddb4ea1dd8cee8e2974e548e07ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=A4=E7=8B=90=E5=86=B2?= <597478495@qq.com> Date: Mon, 1 Jan 2024 22:49:02 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20#3203=20=E3=80=90=E5=85=AC=E4=BC=97?= =?UTF-8?q?=E5=8F=B7=E3=80=91=E4=BF=AE=E5=A4=8D=E4=BD=BF=E7=94=A8okhttp=20?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E6=B0=B8=E4=B9=85=E7=B4=A0=E6=9D=90=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=8E=A5=E5=8F=A3=E5=AD=98=E5=9C=A8=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...oiceAndImageDownloadApacheHttpRequestExecutor.java | 2 +- ...lVoiceAndImageDownloadJoddHttpRequestExecutor.java | 2 +- ...ialVoiceAndImageDownloadOkhttpRequestExecutor.java | 11 +++++------ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadApacheHttpRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadApacheHttpRequestExecutor.java index 3c08b1346..d11591edf 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadApacheHttpRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadApacheHttpRequestExecutor.java @@ -46,7 +46,7 @@ public class MaterialVoiceAndImageDownloadApacheHttpRequestExecutor extends Mate // 下载媒体文件出错 byte[] responseContent = IOUtils.toByteArray(inputStream); String responseContentString = new String(responseContent, StandardCharsets.UTF_8); - if (responseContentString.length() < 100) { + if (responseContentString.length() <= 215) { try { WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); if (wxError.getErrorCode() != 0) { diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadJoddHttpRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadJoddHttpRequestExecutor.java index e4da2004e..c946e9b4b 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadJoddHttpRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadJoddHttpRequestExecutor.java @@ -42,7 +42,7 @@ public class MaterialVoiceAndImageDownloadJoddHttpRequestExecutor extends Materi // 下载媒体文件出错 byte[] responseContent = IOUtils.toByteArray(inputStream); String responseContentString = new String(responseContent, StandardCharsets.UTF_8); - if (responseContentString.length() < 100) { + if (responseContentString.length() <= 215) { try { WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); if (wxError.getErrorCode() != 0) { diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadOkhttpRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadOkhttpRequestExecutor.java index ba2f36967..b77958a4e 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadOkhttpRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadOkhttpRequestExecutor.java @@ -10,6 +10,7 @@ import me.chanjar.weixin.common.util.json.WxGsonBuilder; import okhttp3.*; import okio.BufferedSink; import okio.Okio; +import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,14 +36,12 @@ public class MaterialVoiceAndImageDownloadOkhttpRequestExecutor extends Material Request request = new Request.Builder().url(uri).get().post(requestBody).build(); Response response = client.newCall(request).execute(); String contentTypeHeader = response.header("Content-Type"); - if ("text/plain".equals(contentTypeHeader) || "application/json; charset=utf-8".equals(contentTypeHeader)) { + if ("text/plain".equals(contentTypeHeader) || "application/json; charset=utf-8".equals(contentTypeHeader) + || "application/json; encoding=utf-8".equals(contentTypeHeader)) { String responseContent = response.body().string(); throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP)); } - - try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BufferedSink sink = Okio.buffer(Okio.sink(outputStream))) { - sink.writeAll(response.body().source()); - return new ByteArrayInputStream(outputStream.toByteArray()); - } + byte[] responseContent = IOUtils.toByteArray(response.body().source().inputStream()); + return new ByteArrayInputStream(responseContent); } }