上传图文消息内的图片获取URL

This commit is contained in:
miller
2016-06-01 11:17:53 +08:00
parent 83e443b02f
commit ba54ff5004
6 changed files with 115 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
package me.chanjar.weixin.mp.util.http;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
import me.chanjar.weixin.mp.bean.result.WxMediaImgUploadResult;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.File;
import java.io.IOException;
/**
* @author miller
*/
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, File> {
@Override
public WxMediaImgUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File data) throws WxErrorException, ClientProtocolException, IOException {
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());
}
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);
}
}
}

View File

@@ -0,0 +1,22 @@
package me.chanjar.weixin.mp.util.json;
import com.google.gson.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.result.WxMediaImgUploadResult;
import java.lang.reflect.Type;
/**
* @author miller
*/
public class WxMediaImgUploadResultGsonAdapter implements JsonDeserializer<WxMediaImgUploadResult> {
@Override
public WxMediaImgUploadResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
WxMediaImgUploadResult wxMediaImgUploadResult = new WxMediaImgUploadResult();
JsonObject jsonObject = json.getAsJsonObject();
if (null != jsonObject.get("url") && !jsonObject.get("url").isJsonNull()) {
wxMediaImgUploadResult.setUrl(GsonHelper.getAsString(jsonObject.get("url")));
}
return wxMediaImgUploadResult;
}
}

View File

@@ -41,6 +41,7 @@ public class WxMpGsonBuilder {
INSTANCE.registerTypeAdapter(WxMpCardResult.class, new WxMpCardResultGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter());
}
public static Gson create() {