|
|
@@ -24,27 +24,32 @@ import java.io.IOException; |
|
|
|
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, File> { |
|
|
|
@Override |
|
|
|
public WxMediaImgUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File data) throws WxErrorException, IOException { |
|
|
|
if (data == null) { |
|
|
|
throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build()); |
|
|
|
} |
|
|
|
|
|
|
|
HttpPost httpPost = new HttpPost(uri); |
|
|
|
if (httpProxy != null) { |
|
|
|
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); |
|
|
|
httpPost.setConfig(config); |
|
|
|
} |
|
|
|
if (data != null) { |
|
|
|
HttpEntity entity = MultipartEntityBuilder |
|
|
|
.create() |
|
|
|
.addBinaryBody("media", data) |
|
|
|
.setMode(HttpMultipartMode.RFC6532) |
|
|
|
.build(); |
|
|
|
httpPost.setEntity(entity); |
|
|
|
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
HttpEntity entity = MultipartEntityBuilder |
|
|
|
.create() |
|
|
|
.addBinaryBody("media", data) |
|
|
|
.setMode(HttpMultipartMode.RFC6532) |
|
|
|
.build(); |
|
|
|
httpPost.setEntity(entity); |
|
|
|
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); |
|
|
|
|
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
return WxMediaImgUploadResult.fromJson(responseContent); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |