package me.chanjar.weixin.mp.util.http;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.WxMpMaterial;
import me.chanjar.weixin.mp.bean.result.WxMpMaterialUploadResult;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.*;
import java.util.Map;

public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> {

  public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
      RequestConfig response = RequestConfig.custom().setProxy(httpProxy).build();
      httpPost.setConfig(response);
    }

    if (material != null) {
      File file = material.getFile();
      if (file == null || !file.exists()) {
        throw new FileNotFoundException();
      }
      BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
      MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
      multipartEntityBuilder
          .addPart("media", new InputStreamBody(bufferedInputStream, material.getName()))
          .setMode(HttpMultipartMode.RFC6532);
      Map<String, String> form = material.getForm();
      if (material.getForm() != null) {
        multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form));
      }
      httpPost.setEntity(multipartEntityBuilder.build());
      httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
    }

    CloseableHttpResponse response = httpclient.execute(httpPost);
    String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
    WxError error = WxError.fromJson(responseContent);
    if (error.getErrorCode() != 0) {
      throw new WxErrorException(error);
    } else {
      return WxMpMaterialUploadResult.fromJson(responseContent);
    }
  }

}

@@ -848,68 +848,8 @@
 er;%0A
-import org.apache.http.entity.mime.content.InputStreamBody;%0A
 impo
@@ -900,16 +900,16 @@
 Client;%0A
+
 %0Aimport 
@@ -1055,16 +1055,28 @@
 ial%3E %7B%0A%0A
+  @Override%0A
   public
@@ -1623,108 +1623,9 @@
   %7D%0A
-      BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
 %0A
+
     
@@ -1754,82 +1754,32 @@
 .add
-Part(%22media%22, new InputStreamBody(bufferedInputStream, material.getName())
+BinaryBody(%22media%22, file
 )%0A  
@@ -2175,16 +2175,21 @@
  %7D%0A%0A    
+try (
 Closeabl
@@ -2241,18 +2241,22 @@
 ttpPost)
-;%0A
+) %7B%0A  
     Stri
@@ -2335,16 +2335,18 @@
 e);%0A    
+  
 WxError 
@@ -2384,24 +2384,26 @@
 seContent);%0A
+  
     if (erro
@@ -2429,24 +2429,26 @@
  0) %7B%0A      
+  
 throw new Wx
@@ -2474,16 +2474,18 @@
 r);%0A    
+  
 %7D else %7B
@@ -2485,16 +2485,18 @@
  else %7B%0A
+  
       re
@@ -2548,24 +2548,32 @@
 seContent);%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A%7D

修复上传永久素材报“Connection reset”错误的问题