11package me .chanjar .weixin .common .util .http ;
22
3- import java .io .File ;
4- import java .io .IOException ;
5- import java .io .InputStream ;
6- import java .util .regex .Matcher ;
7- import java .util .regex .Pattern ;
8-
3+ import me .chanjar .weixin .common .bean .result .WxError ;
4+ import me .chanjar .weixin .common .exception .WxErrorException ;
5+ import me .chanjar .weixin .common .util .StringUtils ;
6+ import me .chanjar .weixin .common .util .fs .FileUtils ;
97import org .apache .http .Header ;
108import org .apache .http .HttpHost ;
119import org .apache .http .client .config .RequestConfig ;
1412import org .apache .http .entity .ContentType ;
1513import org .apache .http .impl .client .CloseableHttpClient ;
1614
17- import me .chanjar .weixin .common .bean .result .WxError ;
18- import me .chanjar .weixin .common .exception .WxErrorException ;
19- import me .chanjar .weixin .common .util .StringUtils ;
20- import me .chanjar .weixin .common .util .fs .FileUtils ;
15+ import java .io .File ;
16+ import java .io .IOException ;
17+ import java .io .InputStream ;
18+ import java .util .regex .Matcher ;
19+ import java .util .regex .Pattern ;
2120
2221/**
2322 * 下载媒体文件请求执行器,请求的参数是String, 返回的结果是File
24- *
23+ * 视频文件不支持下载
2524 * @author Daniel Qian
2625 */
2726public class MediaDownloadRequestExecutor implements RequestExecutor <File , String > {
2827
2928 private File tmpDirFile ;
3029
3130 public MediaDownloadRequestExecutor () {
32- super ();
3331 }
3432
3533 public MediaDownloadRequestExecutor (File tmpDirFile ) {
36- super ();
3734 this .tmpDirFile = tmpDirFile ;
3835 }
3936
40-
4137 @ Override
4238 public File execute (CloseableHttpClient httpclient , HttpHost httpProxy , String uri , String queryParam ) throws WxErrorException , IOException {
4339 if (queryParam != null ) {
@@ -59,34 +55,39 @@ public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String u
5955
6056 Header [] contentTypeHeader = response .getHeaders ("Content-Type" );
6157 if (contentTypeHeader != null && contentTypeHeader .length > 0 ) {
62- // 下载媒体文件出错
63- if ( ContentType . TEXT_PLAIN . getMimeType (). equals ( contentTypeHeader [ 0 ]. getValue ())) {
58+ if ( contentTypeHeader [ 0 ]. getValue (). startsWith ( ContentType . APPLICATION_JSON . getMimeType ())) {
59+ // application/json; encoding=utf-8 下载媒体文件出错
6460 String responseContent = Utf8ResponseHandler .INSTANCE .handleResponse (response );
6561 throw new WxErrorException (WxError .fromJson (responseContent ));
6662 }
6763 }
68- // 视频文件不支持下载
64+
6965 String fileName = getFileName (response );
7066 if (StringUtils .isBlank (fileName )) {
7167 return null ;
7268 }
73- String [] name_ext = fileName . split ( " \\ ." );
74- File localFile = FileUtils . createTmpFile ( inputStream , name_ext [ 0 ], name_ext [ 1 ], this . tmpDirFile );
75- return localFile ;
69+
70+ String [] nameAndExt = fileName . split ( " \\ ." );
71+ return FileUtils . createTmpFile ( inputStream , nameAndExt [ 0 ], nameAndExt [ 1 ], this . tmpDirFile ) ;
7672
7773 } finally {
7874 httpGet .releaseConnection ();
7975 }
8076
8177 }
8278
83- protected String getFileName (CloseableHttpResponse response ) {
79+ private String getFileName (CloseableHttpResponse response ) throws WxErrorException {
8480 Header [] contentDispositionHeader = response .getHeaders ("Content-disposition" );
81+ if (contentDispositionHeader == null || contentDispositionHeader .length == 0 ){
82+ throw new WxErrorException (WxError .newBuilder ().setErrorMsg ("无法获取到文件名" ).build ());
83+ }
84+
8585 Pattern p = Pattern .compile (".*filename=\" (.*)\" " );
8686 Matcher m = p .matcher (contentDispositionHeader [0 ].getValue ());
87- m .matches ();
88- String fileName = m .group (1 );
89- return fileName ;
87+ if (m .matches ()){
88+ return m .group (1 );
89+ }
90+ throw new WxErrorException (WxError .newBuilder ().setErrorMsg ("无法获取到文件名" ).build ());
9091 }
9192
9293}
0 commit comments