mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-24 16:18:51 +08:00
优化调整代码
This commit is contained in:
parent
7e21f4692b
commit
95aa369058
@ -24,27 +24,32 @@ import java.io.IOException;
|
|||||||
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, File> {
|
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, File> {
|
||||||
@Override
|
@Override
|
||||||
public WxMediaImgUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File data) throws WxErrorException, IOException {
|
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);
|
HttpPost httpPost = new HttpPost(uri);
|
||||||
if (httpProxy != null) {
|
if (httpProxy != null) {
|
||||||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
|
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
|
||||||
httpPost.setConfig(config);
|
httpPost.setConfig(config);
|
||||||
}
|
}
|
||||||
if (data != null) {
|
|
||||||
HttpEntity entity = MultipartEntityBuilder
|
HttpEntity entity = MultipartEntityBuilder
|
||||||
.create()
|
.create()
|
||||||
.addBinaryBody("media", data)
|
.addBinaryBody("media", data)
|
||||||
.setMode(HttpMultipartMode.RFC6532)
|
.setMode(HttpMultipartMode.RFC6532)
|
||||||
.build();
|
.build();
|
||||||
httpPost.setEntity(entity);
|
httpPost.setEntity(entity);
|
||||||
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
|
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
|
||||||
}
|
|
||||||
try (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) {
|
||||||
throw new WxErrorException(error);
|
throw new WxErrorException(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return WxMediaImgUploadResult.fromJson(responseContent);
|
return WxMediaImgUploadResult.fromJson(responseContent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user