1515import org .apache .http .entity .ContentType ;
1616import org .apache .http .entity .mime .HttpMultipartMode ;
1717import org .apache .http .entity .mime .MultipartEntityBuilder ;
18- import org .apache .http .entity .mime .content .InputStreamBody ;
1918import org .apache .http .impl .client .CloseableHttpClient ;
2019
2120import java .io .*;
2221import java .util .Map ;
2322
2423public class MaterialUploadRequestExecutor implements RequestExecutor <WxMpMaterialUploadResult , WxMpMaterial > {
2524
25+ @ Override
2626 public WxMpMaterialUploadResult execute (CloseableHttpClient httpclient , HttpHost httpProxy , String uri , WxMpMaterial material ) throws WxErrorException , ClientProtocolException , IOException {
2727 HttpPost httpPost = new HttpPost (uri );
2828 if (httpProxy != null ) {
@@ -35,10 +35,9 @@ public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost
3535 if (file == null || !file .exists ()) {
3636 throw new FileNotFoundException ();
3737 }
38- BufferedInputStream bufferedInputStream = new BufferedInputStream (new FileInputStream (file ));
3938 MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder .create ();
4039 multipartEntityBuilder
41- .addPart ("media" , new InputStreamBody ( bufferedInputStream , material . getName ()) )
40+ .addBinaryBody ("media" , file )
4241 .setMode (HttpMultipartMode .RFC6532 );
4342 Map <String , String > form = material .getForm ();
4443 if (material .getForm () != null ) {
@@ -48,7 +47,7 @@ public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost
4847 httpPost .setHeader ("Content-Type" , ContentType .MULTIPART_FORM_DATA .toString ());
4948 }
5049
51- try (CloseableHttpResponse response = httpclient .execute (httpPost )){
50+ try (CloseableHttpResponse response = httpclient .execute (httpPost )) {
5251 String responseContent = Utf8ResponseHandler .INSTANCE .handleResponse (response );
5352 WxError error = WxError .fromJson (responseContent );
5453 if (error .getErrorCode () != 0 ) {
0 commit comments