mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-25 00:56:51 +08:00
修复下载多媒体文件时,如果token正好无效时无法自动刷新token的问题
This commit is contained in:
parent
2907d321ec
commit
26af3c52cf
@ -1,11 +1,9 @@
|
|||||||
package me.chanjar.weixin.common.util.http;
|
package me.chanjar.weixin.common.util.http;
|
||||||
|
|
||||||
import java.io.File;
|
import me.chanjar.weixin.common.bean.result.WxError;
|
||||||
import java.io.IOException;
|
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||||
import java.io.InputStream;
|
import me.chanjar.weixin.common.util.StringUtils;
|
||||||
import java.util.regex.Matcher;
|
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
import org.apache.http.HttpHost;
|
import org.apache.http.HttpHost;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
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.entity.ContentType;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
|
||||||
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.StringUtils;
|
import java.io.InputStream;
|
||||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载媒体文件请求执行器,请求的参数是String, 返回的结果是File
|
* 下载媒体文件请求执行器,请求的参数是String, 返回的结果是File
|
||||||
*
|
* 视频文件不支持下载
|
||||||
* @author Daniel Qian
|
* @author Daniel Qian
|
||||||
*/
|
*/
|
||||||
public class MediaDownloadRequestExecutor implements RequestExecutor<File, String> {
|
public class MediaDownloadRequestExecutor implements RequestExecutor<File, String> {
|
||||||
@ -29,15 +28,12 @@ public class MediaDownloadRequestExecutor implements RequestExecutor<File, Strin
|
|||||||
private File tmpDirFile;
|
private File tmpDirFile;
|
||||||
|
|
||||||
public MediaDownloadRequestExecutor() {
|
public MediaDownloadRequestExecutor() {
|
||||||
super();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public MediaDownloadRequestExecutor(File tmpDirFile) {
|
public MediaDownloadRequestExecutor(File tmpDirFile) {
|
||||||
super();
|
|
||||||
this.tmpDirFile = tmpDirFile;
|
this.tmpDirFile = tmpDirFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException {
|
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException {
|
||||||
if (queryParam != null) {
|
if (queryParam != null) {
|
||||||
@ -59,20 +55,20 @@ public class MediaDownloadRequestExecutor implements RequestExecutor<File, Strin
|
|||||||
|
|
||||||
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
||||||
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
|
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
|
||||||
// 下载媒体文件出错
|
if (contentTypeHeader[0].getValue().startsWith(ContentType.APPLICATION_JSON.getMimeType())) {
|
||||||
if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) {
|
// application/json; encoding=utf-8 下载媒体文件出错
|
||||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||||
throw new WxErrorException(WxError.fromJson(responseContent));
|
throw new WxErrorException(WxError.fromJson(responseContent));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 视频文件不支持下载
|
|
||||||
String fileName = getFileName(response);
|
String fileName = getFileName(response);
|
||||||
if (StringUtils.isBlank(fileName)) {
|
if (StringUtils.isBlank(fileName)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String[] name_ext = fileName.split("\\.");
|
|
||||||
File localFile = FileUtils.createTmpFile(inputStream, name_ext[0], name_ext[1], this.tmpDirFile);
|
String[] nameAndExt = fileName.split("\\.");
|
||||||
return localFile;
|
return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], this.tmpDirFile);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
httpGet.releaseConnection();
|
httpGet.releaseConnection();
|
||||||
@ -80,13 +76,18 @@ public class MediaDownloadRequestExecutor implements RequestExecutor<File, Strin
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getFileName(CloseableHttpResponse response) {
|
private String getFileName(CloseableHttpResponse response) throws WxErrorException {
|
||||||
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
|
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
|
||||||
|
if(contentDispositionHeader == null || contentDispositionHeader.length == 0){
|
||||||
|
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||||
|
}
|
||||||
|
|
||||||
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
|
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
|
||||||
Matcher m = p.matcher(contentDispositionHeader[0].getValue());
|
Matcher m = p.matcher(contentDispositionHeader[0].getValue());
|
||||||
m.matches();
|
if(m.matches()){
|
||||||
String fileName = m.group(1);
|
return m.group(1);
|
||||||
return fileName;
|
}
|
||||||
|
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user