From fdd1c6310a6adaf0962176584056e923a575e067 Mon Sep 17 00:00:00 2001 From: yangyh22 <451561292@qq.com> Date: Sat, 14 Nov 2020 12:30:47 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20#1866=20=E3=80=90=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E3=80=91=20=E6=8F=90=E5=AE=A1=E7=B4=A0=E6=9D=90?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=8E=A5=E5=8F=A3=E6=B7=BB=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E8=AF=B7=E6=B1=82=E6=89=A7=E8=A1=8C=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bean/result/WxAuditMediaUploadResult.java | 31 ++++++++++ .../http/AuditMediaUploadRequestExecutor.java | 47 +++++++++++++++ ...ApacheAuditMediaUploadRequestExecutor.java | 58 +++++++++++++++++++ ...ddHttpAuditMediaUploadRequestExecutor.java | 46 +++++++++++++++ ...OkHttpAuditMediaUploadRequestExecutor.java | 49 ++++++++++++++++ 5 files changed, 231 insertions(+) create mode 100644 weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxAuditMediaUploadResult.java create mode 100644 weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/AuditMediaUploadRequestExecutor.java create mode 100644 weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheAuditMediaUploadRequestExecutor.java create mode 100644 weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/jodd/JoddHttpAuditMediaUploadRequestExecutor.java create mode 100644 weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/okhttp/OkHttpAuditMediaUploadRequestExecutor.java diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxAuditMediaUploadResult.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxAuditMediaUploadResult.java new file mode 100644 index 0000000000..916fe1815e --- /dev/null +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxAuditMediaUploadResult.java @@ -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); + } + +} diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/AuditMediaUploadRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/AuditMediaUploadRequestExecutor.java new file mode 100644 index 0000000000..aeb6894e0f --- /dev/null +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/AuditMediaUploadRequestExecutor.java @@ -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 implements RequestExecutor { + + protected RequestHttp requestHttp; + + public AuditMediaUploadRequestExecutor(RequestHttp requestHttp) { + this.requestHttp = requestHttp; + } + + @Override + public void execute(String uri, File data, ResponseHandler handler, WxType wxType) throws WxErrorException, IOException { + handler.handle(this.execute(uri, data, wxType)); + } + + public static RequestExecutor 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; + } + } + +} diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheAuditMediaUploadRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheAuditMediaUploadRequestExecutor.java new file mode 100644 index 0000000000..a390b9f9db --- /dev/null +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheAuditMediaUploadRequestExecutor.java @@ -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 { + + 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(); + } + } +} diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/jodd/JoddHttpAuditMediaUploadRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/jodd/JoddHttpAuditMediaUploadRequestExecutor.java new file mode 100644 index 0000000000..4747dc8438 --- /dev/null +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/jodd/JoddHttpAuditMediaUploadRequestExecutor.java @@ -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 { + + 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); + } +} diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/okhttp/OkHttpAuditMediaUploadRequestExecutor.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/okhttp/OkHttpAuditMediaUploadRequestExecutor.java new file mode 100644 index 0000000000..78f4c4af0f --- /dev/null +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/okhttp/OkHttpAuditMediaUploadRequestExecutor.java @@ -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 { + + 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); + } + +}