Skip to content

Commit 1b4293c

Browse files
committed
Merge pull request #3 from kareanyi/develop
上传图文消息内的图片获取URL
2 parents c0f1632 + ba54ff5 commit 1b4293c

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,4 +936,15 @@ public void markCardCode(String code, String cardId, String openId, boolean isMa
936936
* @throws WxErrorException
937937
*/
938938
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception;
939+
940+
/*
941+
* <pre>
942+
* 上传图文消息内的图片获取URL
943+
* 详情请见:http://mp.weixin.qq.com/wiki/15/40b6865b893947b764e2de8e4a1fb55f.html#.E4.B8.8A.E4.BC.A0.E5.9B.BE.E6.96.87.E6.B6.88.E6.81.AF.E5.86.85.E7.9A.84.E5.9B.BE.E7.89.87.E8.8E.B7.E5.8F.96URL.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8E.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.9D.87.E5.8F.AF.E7.94.A8.E3.80.91
944+
* </pre>
945+
* @param file
946+
* @return WxMediaImgUploadResult 返回图片url
947+
* @throws WxErrorException
948+
*/
949+
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException;
939950
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,4 +1266,10 @@ public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPrev
12661266
return WxMpMassSendResult.fromJson(responseContent);
12671267
}
12681268

1269+
@Override
1270+
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException {
1271+
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg";
1272+
return execute(new MediaImgUploadRequestExecutor(), url, file);
1273+
}
1274+
12691275
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package me.chanjar.weixin.mp.bean.result;
2+
3+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* @author miller
9+
*/
10+
public class WxMediaImgUploadResult implements Serializable {
11+
private String url;
12+
13+
public static WxMediaImgUploadResult fromJson(String json) {
14+
return WxMpGsonBuilder.create().fromJson(json, WxMediaImgUploadResult.class);
15+
}
16+
17+
public String getUrl() {
18+
return url;
19+
}
20+
21+
public void setUrl(String url) {
22+
this.url = url;
23+
}
24+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package me.chanjar.weixin.mp.util.http;
2+
3+
import me.chanjar.weixin.common.bean.result.WxError;
4+
import me.chanjar.weixin.common.exception.WxErrorException;
5+
import me.chanjar.weixin.common.util.http.RequestExecutor;
6+
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
7+
import me.chanjar.weixin.mp.bean.result.WxMediaImgUploadResult;
8+
import org.apache.http.HttpEntity;
9+
import org.apache.http.HttpHost;
10+
import org.apache.http.client.ClientProtocolException;
11+
import org.apache.http.client.config.RequestConfig;
12+
import org.apache.http.client.methods.CloseableHttpResponse;
13+
import org.apache.http.client.methods.HttpPost;
14+
import org.apache.http.entity.ContentType;
15+
import org.apache.http.entity.mime.HttpMultipartMode;
16+
import org.apache.http.entity.mime.MultipartEntityBuilder;
17+
import org.apache.http.impl.client.CloseableHttpClient;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
/**
23+
* @author miller
24+
*/
25+
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, File> {
26+
@Override
27+
public WxMediaImgUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File data) throws WxErrorException, ClientProtocolException, IOException {
28+
HttpPost httpPost = new HttpPost(uri);
29+
if (httpProxy != null) {
30+
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
31+
httpPost.setConfig(config);
32+
}
33+
if (data != null) {
34+
HttpEntity entity = MultipartEntityBuilder
35+
.create()
36+
.addBinaryBody("media", data)
37+
.setMode(HttpMultipartMode.RFC6532)
38+
.build();
39+
httpPost.setEntity(entity);
40+
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
41+
}
42+
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
43+
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
44+
WxError error = WxError.fromJson(responseContent);
45+
if (error.getErrorCode() != 0) {
46+
throw new WxErrorException(error);
47+
}
48+
return WxMediaImgUploadResult.fromJson(responseContent);
49+
}
50+
}
51+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package me.chanjar.weixin.mp.util.json;
2+
3+
import com.google.gson.*;
4+
import me.chanjar.weixin.common.util.json.GsonHelper;
5+
import me.chanjar.weixin.mp.bean.result.WxMediaImgUploadResult;
6+
7+
import java.lang.reflect.Type;
8+
9+
/**
10+
* @author miller
11+
*/
12+
public class WxMediaImgUploadResultGsonAdapter implements JsonDeserializer<WxMediaImgUploadResult> {
13+
@Override
14+
public WxMediaImgUploadResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
15+
WxMediaImgUploadResult wxMediaImgUploadResult = new WxMediaImgUploadResult();
16+
JsonObject jsonObject = json.getAsJsonObject();
17+
if (null != jsonObject.get("url") && !jsonObject.get("url").isJsonNull()) {
18+
wxMediaImgUploadResult.setUrl(GsonHelper.getAsString(jsonObject.get("url")));
19+
}
20+
return wxMediaImgUploadResult;
21+
}
22+
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class WxMpGsonBuilder {
4141
INSTANCE.registerTypeAdapter(WxMpCardResult.class, new WxMpCardResultGsonAdapter());
4242
INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter());
4343
INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter());
44+
INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter());
4445
}
4546

4647
public static Gson create() {

0 commit comments

Comments
 (0)