From 26af3c52cf72965f5c34a5a07236d5fe03ec90e3 Mon Sep 17 00:00:00 2001 From: BinaryWang Date: Thu, 22 Sep 2016 17:00:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8B=E8=BD=BD=E5=A4=9A?= =?UTF-8?q?=E5=AA=92=E4=BD=93=E6=96=87=E4=BB=B6=E6=97=B6=EF=BC=8C=E5=A6=82?= =?UTF-8?q?=E6=9E=9Ctoken=E6=AD=A3=E5=A5=BD=E6=97=A0=E6=95=88=E6=97=B6?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E8=87=AA=E5=8A=A8=E5=88=B7=E6=96=B0token?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../http/MediaDownloadRequestExecutor.java | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java index 87981142..c81658e2 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java @@ -1,11 +1,9 @@ package me.chanjar.weixin.common.util.http; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - +import me.chanjar.weixin.common.bean.result.WxError; +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.util.StringUtils; +import me.chanjar.weixin.common.util.fs.FileUtils; import org.apache.http.Header; import org.apache.http.HttpHost; import org.apache.http.client.config.RequestConfig; @@ -14,14 +12,15 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.entity.ContentType; import org.apache.http.impl.client.CloseableHttpClient; -import me.chanjar.weixin.common.bean.result.WxError; -import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.common.util.StringUtils; -import me.chanjar.weixin.common.util.fs.FileUtils; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * 下载媒体文件请求执行器,请求的参数是String, 返回的结果是File - * + * 视频文件不支持下载 * @author Daniel Qian */ public class MediaDownloadRequestExecutor implements RequestExecutor { @@ -29,15 +28,12 @@ public class MediaDownloadRequestExecutor implements RequestExecutor 0) { - // 下载媒体文件出错 - if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) { + if (contentTypeHeader[0].getValue().startsWith(ContentType.APPLICATION_JSON.getMimeType())) { + // application/json; encoding=utf-8 下载媒体文件出错 String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); throw new WxErrorException(WxError.fromJson(responseContent)); } } - // 视频文件不支持下载 + String fileName = getFileName(response); if (StringUtils.isBlank(fileName)) { return null; } - String[] name_ext = fileName.split("\\."); - File localFile = FileUtils.createTmpFile(inputStream, name_ext[0], name_ext[1], this.tmpDirFile); - return localFile; + + String[] nameAndExt = fileName.split("\\."); + return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], this.tmpDirFile); } finally { httpGet.releaseConnection(); @@ -80,13 +76,18 @@ public class MediaDownloadRequestExecutor implements RequestExecutor