mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-04 06:07:59 +08:00 
			
		
		
		
	修复上传永久素材报“Connection reset”错误的问题
This commit is contained in:
		@ -15,7 +15,6 @@ import org.apache.http.client.methods.HttpPost;
 | 
				
			|||||||
import org.apache.http.entity.ContentType;
 | 
					import org.apache.http.entity.ContentType;
 | 
				
			||||||
import org.apache.http.entity.mime.HttpMultipartMode;
 | 
					import org.apache.http.entity.mime.HttpMultipartMode;
 | 
				
			||||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
 | 
					import org.apache.http.entity.mime.MultipartEntityBuilder;
 | 
				
			||||||
import org.apache.http.entity.mime.content.InputStreamBody;
 | 
					 | 
				
			||||||
import org.apache.http.impl.client.CloseableHttpClient;
 | 
					import org.apache.http.impl.client.CloseableHttpClient;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.*;
 | 
					import java.io.*;
 | 
				
			||||||
@ -23,6 +22,7 @@ import java.util.Map;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> {
 | 
					public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Override
 | 
				
			||||||
  public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, ClientProtocolException, IOException {
 | 
					  public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, ClientProtocolException, IOException {
 | 
				
			||||||
    HttpPost httpPost = new HttpPost(uri);
 | 
					    HttpPost httpPost = new HttpPost(uri);
 | 
				
			||||||
    if (httpProxy != null) {
 | 
					    if (httpProxy != null) {
 | 
				
			||||||
@ -35,10 +35,10 @@ public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMateri
 | 
				
			|||||||
      if (file == null || !file.exists()) {
 | 
					      if (file == null || !file.exists()) {
 | 
				
			||||||
        throw new FileNotFoundException();
 | 
					        throw new FileNotFoundException();
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
 | 
					
 | 
				
			||||||
      MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
 | 
					      MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
 | 
				
			||||||
      multipartEntityBuilder
 | 
					      multipartEntityBuilder
 | 
				
			||||||
          .addPart("media", new InputStreamBody(bufferedInputStream, material.getName()))
 | 
					          .addBinaryBody("media", file)
 | 
				
			||||||
          .setMode(HttpMultipartMode.RFC6532);
 | 
					          .setMode(HttpMultipartMode.RFC6532);
 | 
				
			||||||
      Map<String, String> form = material.getForm();
 | 
					      Map<String, String> form = material.getForm();
 | 
				
			||||||
      if (material.getForm() != null) {
 | 
					      if (material.getForm() != null) {
 | 
				
			||||||
@ -48,7 +48,7 @@ public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMateri
 | 
				
			|||||||
      httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
 | 
					      httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    CloseableHttpResponse response = httpclient.execute(httpPost);
 | 
					    try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
 | 
				
			||||||
      String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
 | 
					      String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
 | 
				
			||||||
      WxError error = WxError.fromJson(responseContent);
 | 
					      WxError error = WxError.fromJson(responseContent);
 | 
				
			||||||
      if (error.getErrorCode() != 0) {
 | 
					      if (error.getErrorCode() != 0) {
 | 
				
			||||||
@ -57,5 +57,6 @@ public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMateri
 | 
				
			|||||||
        return WxMpMaterialUploadResult.fromJson(responseContent);
 | 
					        return WxMpMaterialUploadResult.fromJson(responseContent);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user