@@ -936,4 +936,15 @@ public interface WxMpService { | |||||
* @throws WxErrorException | * @throws WxErrorException | ||||
*/ | */ | ||||
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception; | public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception; | ||||
/* | |||||
* <pre> | |||||
* 上传图文消息内的图片获取URL | |||||
* 详情请见: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 | |||||
* </pre> | |||||
* @param file | |||||
* @return WxMediaImgUploadResult 返回图片url | |||||
* @throws WxErrorException | |||||
*/ | |||||
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException; | |||||
} | } |
@@ -1266,4 +1266,10 @@ public class WxMpServiceImpl implements WxMpService { | |||||
return WxMpMassSendResult.fromJson(responseContent); | return WxMpMassSendResult.fromJson(responseContent); | ||||
} | } | ||||
@Override | |||||
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException { | |||||
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg"; | |||||
return execute(new MediaImgUploadRequestExecutor(), url, file); | |||||
} | |||||
} | } |
@@ -0,0 +1,24 @@ | |||||
package me.chanjar.weixin.mp.bean.result; | |||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||||
import java.io.Serializable; | |||||
/** | |||||
* @author miller | |||||
*/ | |||||
public class WxMediaImgUploadResult implements Serializable { | |||||
private String url; | |||||
public static WxMediaImgUploadResult fromJson(String json) { | |||||
return WxMpGsonBuilder.create().fromJson(json, WxMediaImgUploadResult.class); | |||||
} | |||||
public String getUrl() { | |||||
return url; | |||||
} | |||||
public void setUrl(String url) { | |||||
this.url = url; | |||||
} | |||||
} |
@@ -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); | |||||
} | |||||
} | |||||
} |
@@ -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; | |||||
} | |||||
} |
@@ -41,6 +41,7 @@ public class WxMpGsonBuilder { | |||||
INSTANCE.registerTypeAdapter(WxMpCardResult.class, new WxMpCardResultGsonAdapter()); | INSTANCE.registerTypeAdapter(WxMpCardResult.class, new WxMpCardResultGsonAdapter()); | ||||
INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter()); | INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter()); | ||||
INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter()); | INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter()); | ||||
INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter()); | |||||
} | } | ||||
public static Gson create() { | public static Gson create() { | ||||