Skip to content

Commit 95aa369

Browse files
committed
优化调整代码
1 parent 7e21f46 commit 95aa369

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

‎weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MediaImgUploadRequestExecutor.java‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,32 @@
2424
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, File> {
2525
@Override
2626
public WxMediaImgUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File data) throws WxErrorException, IOException {
27+
if (data == null) {
28+
throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build());
29+
}
30+
2731
HttpPost httpPost = new HttpPost(uri);
2832
if (httpProxy != null) {
2933
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
3034
httpPost.setConfig(config);
3135
}
32-
if (data != null) {
33-
HttpEntity entity = MultipartEntityBuilder
34-
.create()
35-
.addBinaryBody("media", data)
36-
.setMode(HttpMultipartMode.RFC6532)
37-
.build();
38-
httpPost.setEntity(entity);
39-
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
40-
}
36+
37+
HttpEntity entity = MultipartEntityBuilder
38+
.create()
39+
.addBinaryBody("media", data)
40+
.setMode(HttpMultipartMode.RFC6532)
41+
.build();
42+
httpPost.setEntity(entity);
43+
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
44+
4145
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
4246
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
4347
WxError error = WxError.fromJson(responseContent);
4448
if (error.getErrorCode() != 0) {
4549
throw new WxErrorException(error);
4650
}
51+
4752
return WxMediaImgUploadResult.fromJson(responseContent);
4853
}
4954
}
50-
}
55+
}

0 commit comments

Comments
 (0)