Browse Source

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

master
Binary Wang 9 years ago
parent
commit
39c2f09008
1 changed files with 11 additions and 10 deletions
  1. +11
    -10
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java

+ 11
- 10
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java View File

@@ -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,13 +48,14 @@ 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);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialUploadResult.fromJson(responseContent);
try (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);
}
} }
} }




Loading…
Cancel
Save