Skip to content

Commit 8610c0f

Browse files
authored
🐛 #3203 【公众号】修复使用okhttp 方式永久素材下载接口存在的问题
1 parent c68e230 commit 8610c0f

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadApacheHttpRequestExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public InputStream execute(String uri, String materialId, WxType wxType) throws
4646
// 下载媒体文件出错
4747
byte[] responseContent = IOUtils.toByteArray(inputStream);
4848
String responseContentString = new String(responseContent, StandardCharsets.UTF_8);
49-
if (responseContentString.length() < 100) {
49+
if (responseContentString.length() <= 215) {
5050
try {
5151
WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
5252
if (wxError.getErrorCode() != 0) {

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadJoddHttpRequestExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public InputStream execute(String uri, String materialId, WxType wxType) throws
4242
// 下载媒体文件出错
4343
byte[] responseContent = IOUtils.toByteArray(inputStream);
4444
String responseContentString = new String(responseContent, StandardCharsets.UTF_8);
45-
if (responseContentString.length() < 100) {
45+
if (responseContentString.length() <= 215) {
4646
try {
4747
WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
4848
if (wxError.getErrorCode() != 0) {

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadOkhttpRequestExecutor.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import okhttp3.*;
1111
import okio.BufferedSink;
1212
import okio.Okio;
13+
import org.apache.commons.io.IOUtils;
1314
import org.slf4j.Logger;
1415
import org.slf4j.LoggerFactory;
1516

@@ -35,14 +36,12 @@ public InputStream execute(String uri, String materialId, WxType wxType) throws
3536
Request request = new Request.Builder().url(uri).get().post(requestBody).build();
3637
Response response = client.newCall(request).execute();
3738
String contentTypeHeader = response.header("Content-Type");
38-
if ("text/plain".equals(contentTypeHeader) || "application/json; charset=utf-8".equals(contentTypeHeader)) {
39+
if ("text/plain".equals(contentTypeHeader) || "application/json; charset=utf-8".equals(contentTypeHeader)
40+
|| "application/json; encoding=utf-8".equals(contentTypeHeader)) {
3941
String responseContent = response.body().string();
4042
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
4143
}
42-
43-
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BufferedSink sink = Okio.buffer(Okio.sink(outputStream))) {
44-
sink.writeAll(response.body().source());
45-
return new ByteArrayInputStream(outputStream.toByteArray());
46-
}
44+
byte[] responseContent = IOUtils.toByteArray(response.body().source().inputStream());
45+
return new ByteArrayInputStream(responseContent);
4746
}
4847
}

0 commit comments

Comments
 (0)