-
-
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.
- Loading branch information
Showing
5 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...a-common/src/main/java/me/chanjar/weixin/common/bean/result/WxAuditMediaUploadResult.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,31 @@ | ||
package me.chanjar.weixin.common.bean.result; | ||
|
||
import java.io.Serializable; | ||
|
||
import lombok.Data; | ||
import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||
|
||
/** | ||
* 小程序 提审素材上传接口 | ||
* | ||
* @author yangyh22 | ||
* @since 2020/11/14 | ||
*/ | ||
@Data | ||
public class WxAuditMediaUploadResult implements Serializable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private String type; | ||
private String mediaid; | ||
|
||
public static WxAuditMediaUploadResult fromJson(String json) { | ||
return WxGsonBuilder.create().fromJson(json, WxAuditMediaUploadResult.class); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return WxGsonBuilder.create().toJson(this); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...mon/src/main/java/me/chanjar/weixin/common/util/http/AuditMediaUploadRequestExecutor.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,47 @@ | ||
package me.chanjar.weixin.common.util.http; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import me.chanjar.weixin.common.bean.result.WxAuditMediaUploadResult; | ||
import me.chanjar.weixin.common.enums.WxType; | ||
import me.chanjar.weixin.common.error.WxErrorException; | ||
import me.chanjar.weixin.common.util.http.apache.ApacheAuditMediaUploadRequestExecutor; | ||
import me.chanjar.weixin.common.util.http.jodd.JoddHttpAuditMediaUploadRequestExecutor; | ||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpAuditMediaUploadRequestExecutor; | ||
|
||
/** | ||
* 小程序 提审素材上传接口 | ||
* 上传媒体文件请求执行器. | ||
* 请求的参数是File, 返回的结果是String | ||
* | ||
* @author yangyh22 | ||
* @since 2020/11/14 | ||
*/ | ||
public abstract class AuditMediaUploadRequestExecutor<H, P> implements RequestExecutor<WxAuditMediaUploadResult, File> { | ||
|
||
protected RequestHttp<H, P> requestHttp; | ||
|
||
public AuditMediaUploadRequestExecutor(RequestHttp requestHttp) { | ||
this.requestHttp = requestHttp; | ||
} | ||
|
||
@Override | ||
public void execute(String uri, File data, ResponseHandler<WxAuditMediaUploadResult> handler, WxType wxType) throws WxErrorException, IOException { | ||
handler.handle(this.execute(uri, data, wxType)); | ||
} | ||
|
||
public static RequestExecutor<WxAuditMediaUploadResult, File> create(RequestHttp requestHttp) { | ||
switch (requestHttp.getRequestType()) { | ||
case APACHE_HTTP: | ||
return new ApacheAuditMediaUploadRequestExecutor(requestHttp); | ||
case JODD_HTTP: | ||
return new JoddHttpAuditMediaUploadRequestExecutor(requestHttp); | ||
case OK_HTTP: | ||
return new OkHttpAuditMediaUploadRequestExecutor(requestHttp); | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
...java/me/chanjar/weixin/common/util/http/apache/ApacheAuditMediaUploadRequestExecutor.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,58 @@ | ||
package me.chanjar.weixin.common.util.http.apache; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import me.chanjar.weixin.common.bean.result.WxAuditMediaUploadResult; | ||
import me.chanjar.weixin.common.enums.WxType; | ||
import me.chanjar.weixin.common.error.WxError; | ||
import me.chanjar.weixin.common.error.WxErrorException; | ||
import me.chanjar.weixin.common.util.http.AuditMediaUploadRequestExecutor; | ||
import me.chanjar.weixin.common.util.http.RequestHttp; | ||
|
||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpHost; | ||
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.mime.HttpMultipartMode; | ||
import org.apache.http.entity.mime.MultipartEntityBuilder; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
|
||
/** | ||
* @author yangyh22 | ||
* @since 2020/11/14 | ||
*/ | ||
public class ApacheAuditMediaUploadRequestExecutor extends AuditMediaUploadRequestExecutor<CloseableHttpClient, HttpHost> { | ||
|
||
public ApacheAuditMediaUploadRequestExecutor(RequestHttp requestHttp) { | ||
super(requestHttp); | ||
} | ||
|
||
@Override | ||
public WxAuditMediaUploadResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException { | ||
HttpPost httpPost = new HttpPost(uri); | ||
if (requestHttp.getRequestHttpProxy() != null) { | ||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | ||
httpPost.setConfig(config); | ||
} | ||
if (file != null) { | ||
HttpEntity entity = MultipartEntityBuilder | ||
.create() | ||
.addBinaryBody("media", file) | ||
.setMode(HttpMultipartMode.RFC6532) | ||
.build(); | ||
httpPost.setEntity(entity); | ||
} | ||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { | ||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | ||
WxError error = WxError.fromJson(responseContent, wxType); | ||
if (error.getErrorCode() != 0) { | ||
throw new WxErrorException(error); | ||
} | ||
return WxAuditMediaUploadResult.fromJson(responseContent); | ||
} finally { | ||
httpPost.releaseConnection(); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...java/me/chanjar/weixin/common/util/http/jodd/JoddHttpAuditMediaUploadRequestExecutor.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,46 @@ | ||
package me.chanjar.weixin.common.util.http.jodd; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import jodd.http.HttpConnectionProvider; | ||
import jodd.http.HttpRequest; | ||
import jodd.http.HttpResponse; | ||
import jodd.http.ProxyInfo; | ||
import me.chanjar.weixin.common.bean.result.WxAuditMediaUploadResult; | ||
import me.chanjar.weixin.common.enums.WxType; | ||
import me.chanjar.weixin.common.error.WxError; | ||
import me.chanjar.weixin.common.error.WxErrorException; | ||
import me.chanjar.weixin.common.util.http.AuditMediaUploadRequestExecutor; | ||
import me.chanjar.weixin.common.util.http.RequestHttp; | ||
|
||
/** | ||
* @author yangyh22 | ||
* @since 2020/11/14 | ||
*/ | ||
public class JoddHttpAuditMediaUploadRequestExecutor extends AuditMediaUploadRequestExecutor<HttpConnectionProvider, ProxyInfo> { | ||
|
||
public JoddHttpAuditMediaUploadRequestExecutor(RequestHttp requestHttp) { | ||
super(requestHttp); | ||
} | ||
|
||
@Override | ||
public WxAuditMediaUploadResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException { | ||
HttpRequest request = HttpRequest.post(uri); | ||
if (requestHttp.getRequestHttpProxy() != null) { | ||
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy()); | ||
} | ||
request.withConnectionProvider(requestHttp.getRequestHttpClient()); | ||
request.form("media", file); | ||
HttpResponse response = request.send(); | ||
response.charset(StandardCharsets.UTF_8.name()); | ||
|
||
String responseContent = response.bodyText(); | ||
WxError error = WxError.fromJson(responseContent, wxType); | ||
if (error.getErrorCode() != 0) { | ||
throw new WxErrorException(error); | ||
} | ||
return WxAuditMediaUploadResult.fromJson(responseContent); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...java/me/chanjar/weixin/common/util/http/okhttp/OkHttpAuditMediaUploadRequestExecutor.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,49 @@ | ||
package me.chanjar.weixin.common.util.http.okhttp; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import me.chanjar.weixin.common.bean.result.WxAuditMediaUploadResult; | ||
import me.chanjar.weixin.common.enums.WxType; | ||
import me.chanjar.weixin.common.error.WxError; | ||
import me.chanjar.weixin.common.error.WxErrorException; | ||
import me.chanjar.weixin.common.util.http.AuditMediaUploadRequestExecutor; | ||
import me.chanjar.weixin.common.util.http.RequestHttp; | ||
import okhttp3.MediaType; | ||
import okhttp3.MultipartBody; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
|
||
/** | ||
* @author yangyh22 | ||
* @since 2020/11/14 | ||
*/ | ||
public class OkHttpAuditMediaUploadRequestExecutor extends AuditMediaUploadRequestExecutor<OkHttpClient, OkHttpProxyInfo> { | ||
|
||
public OkHttpAuditMediaUploadRequestExecutor(RequestHttp requestHttp) { | ||
super(requestHttp); | ||
} | ||
|
||
@Override | ||
public WxAuditMediaUploadResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException { | ||
|
||
RequestBody body = new MultipartBody.Builder() | ||
.setType(MediaType.parse("multipart/form-data")) | ||
.addFormDataPart("media", | ||
file.getName(), | ||
RequestBody.create(MediaType.parse("application/octet-stream"), file)) | ||
.build(); | ||
Request request = new Request.Builder().url(uri).post(body).build(); | ||
|
||
Response response = requestHttp.getRequestHttpClient().newCall(request).execute(); | ||
String responseContent = response.body().string(); | ||
WxError error = WxError.fromJson(responseContent, wxType); | ||
if (error.getErrorCode() != 0) { | ||
throw new WxErrorException(error); | ||
} | ||
return WxAuditMediaUploadResult.fromJson(responseContent); | ||
} | ||
|
||
} |