-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from kareanyi/develop
上传图文消息内的图片获取URL
- Loading branch information
Showing
6 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMediaImgUploadResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...n-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MediaImgUploadRequestExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...va-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMediaImgUploadResultGsonAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters