-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
企微上传素材,支持字符串格式url #2533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
企微上传素材,支持字符串格式url #2533
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/InputStreamData.java
This file contains hidden or 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,27 @@ | ||
| package me.chanjar.weixin.common.util.http; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.experimental.Accessors; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.io.Serializable; | ||
|
|
||
| /** | ||
| * 输入流数据. | ||
| * <p/> | ||
| * InputStreamData | ||
| * | ||
| * @author zichuan.zhou91@gmail.com | ||
| * @date 2022/2/15 | ||
| */ | ||
| @Data | ||
| @Accessors(chain = true) | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class InputStreamData implements Serializable { | ||
| private static final long serialVersionUID = -4627006604779378520L; | ||
| private InputStream inputStream; | ||
| private String filename; | ||
| } |
43 changes: 43 additions & 0 deletions
43
...c/main/java/me/chanjar/weixin/common/util/http/MediaInputStreamUploadRequestExecutor.java
This file contains hidden or 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,43 @@ | ||
| package me.chanjar.weixin.common.util.http; | ||
|
|
||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | ||
| import me.chanjar.weixin.common.enums.WxType; | ||
| import me.chanjar.weixin.common.error.WxErrorException; | ||
| import me.chanjar.weixin.common.util.http.apache.ApacheMediaInputStreamUploadRequestExecutor; | ||
| import me.chanjar.weixin.common.util.http.jodd.JoddHttpMediaInputStreamUploadRequestExecutor; | ||
| import me.chanjar.weixin.common.util.http.okhttp.OkHttpMediaInputStreamUploadRequestExecutor; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * 上传媒体文件请求执行器. | ||
| * 请求的参数是File, 返回的结果是String | ||
| * | ||
| * @author Daniel Qian | ||
| */ | ||
| public abstract class MediaInputStreamUploadRequestExecutor<H, P> implements RequestExecutor<WxMediaUploadResult, InputStreamData> { | ||
| protected RequestHttp<H, P> requestHttp; | ||
|
|
||
| public MediaInputStreamUploadRequestExecutor(RequestHttp requestHttp) { | ||
| this.requestHttp = requestHttp; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(String uri, InputStreamData data, ResponseHandler<WxMediaUploadResult> handler, WxType wxType) throws WxErrorException, IOException { | ||
| handler.handle(this.execute(uri, data, wxType)); | ||
| } | ||
|
|
||
| public static RequestExecutor<WxMediaUploadResult, InputStreamData> create(RequestHttp requestHttp) { | ||
| switch (requestHttp.getRequestType()) { | ||
| case APACHE_HTTP: | ||
| return new ApacheMediaInputStreamUploadRequestExecutor(requestHttp); | ||
| case JODD_HTTP: | ||
| return new JoddHttpMediaInputStreamUploadRequestExecutor(requestHttp); | ||
| case OK_HTTP: | ||
| return new OkHttpMediaInputStreamUploadRequestExecutor(requestHttp); | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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
59 changes: 59 additions & 0 deletions
59
...e/chanjar/weixin/common/util/http/apache/ApacheMediaInputStreamUploadRequestExecutor.java
This file contains hidden or 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,59 @@ | ||
| package me.chanjar.weixin.common.util.http.apache; | ||
|
|
||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | ||
| 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.InputStreamData; | ||
| import me.chanjar.weixin.common.util.http.MediaInputStreamUploadRequestExecutor; | ||
| 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.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.IOException; | ||
|
|
||
| /** | ||
| * 文件输入流上传. | ||
| * | ||
| * @author meiqin.zhou91@gmail.com | ||
| * @date 2022/02/15 | ||
| */ | ||
| public class ApacheMediaInputStreamUploadRequestExecutor extends MediaInputStreamUploadRequestExecutor<CloseableHttpClient, HttpHost> { | ||
| public ApacheMediaInputStreamUploadRequestExecutor(RequestHttp requestHttp) { | ||
| super(requestHttp); | ||
| } | ||
|
|
||
| @Override | ||
| public WxMediaUploadResult execute(String uri, InputStreamData data, 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 (data != null) { | ||
| HttpEntity entity = MultipartEntityBuilder | ||
| .create() | ||
| .addBinaryBody("media", data.getInputStream(), ContentType.DEFAULT_BINARY, data.getFilename()) | ||
| .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 WxMediaUploadResult.fromJson(responseContent); | ||
| } finally { | ||
| httpPost.releaseConnection(); | ||
| } | ||
| } | ||
| } |
61 changes: 61 additions & 0 deletions
61
...e/chanjar/weixin/common/util/http/jodd/JoddHttpMediaInputStreamUploadRequestExecutor.java
This file contains hidden or 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,61 @@ | ||
| package me.chanjar.weixin.common.util.http.jodd; | ||
|
|
||
| import jodd.http.HttpConnectionProvider; | ||
| import jodd.http.HttpRequest; | ||
| import jodd.http.HttpResponse; | ||
| import jodd.http.ProxyInfo; | ||
| import jodd.http.up.ByteArrayUploadable; | ||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | ||
| 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.InputStreamData; | ||
| import me.chanjar.weixin.common.util.http.MediaInputStreamUploadRequestExecutor; | ||
| import me.chanjar.weixin.common.util.http.RequestHttp; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| /** | ||
| * 文件输入流上传. | ||
| * | ||
| * @author meiqin.zhou91@gmail.com | ||
| * @date 2022/02/15 | ||
| */ | ||
| public class JoddHttpMediaInputStreamUploadRequestExecutor extends MediaInputStreamUploadRequestExecutor<HttpConnectionProvider, ProxyInfo> { | ||
| public JoddHttpMediaInputStreamUploadRequestExecutor(RequestHttp requestHttp) { | ||
| super(requestHttp); | ||
| } | ||
|
|
||
| @Override | ||
| public WxMediaUploadResult execute(String uri, InputStreamData data, 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", new ByteArrayUploadable(this.toByteArray(data.getInputStream()), data.getFilename())); | ||
| 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 WxMediaUploadResult.fromJson(responseContent); | ||
| } | ||
|
|
||
| public byte[] toByteArray(InputStream input) throws IOException { | ||
| try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { | ||
| byte[] buffer = new byte[4096]; | ||
| int n = 0; | ||
| while (-1 != (n = input.read(buffer))) { | ||
| output.write(buffer, 0, n); | ||
| } | ||
| return output.toByteArray(); | ||
| } | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
...e/chanjar/weixin/common/util/http/okhttp/OkHttpMediaInputStreamUploadRequestExecutor.java
This file contains hidden or 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,56 @@ | ||
| package me.chanjar.weixin.common.util.http.okhttp; | ||
|
|
||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | ||
| 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.InputStreamData; | ||
| import me.chanjar.weixin.common.util.http.MediaInputStreamUploadRequestExecutor; | ||
| import me.chanjar.weixin.common.util.http.RequestHttp; | ||
| import okhttp3.*; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
|
|
||
| /** | ||
| * 文件输入流上传. | ||
| * | ||
| * @author meiqin.zhou91@gmail.com | ||
| * @date 2022/02/15 | ||
| */ | ||
| public class OkHttpMediaInputStreamUploadRequestExecutor extends MediaInputStreamUploadRequestExecutor<OkHttpClient, OkHttpProxyInfo> { | ||
| public OkHttpMediaInputStreamUploadRequestExecutor(RequestHttp requestHttp) { | ||
| super(requestHttp); | ||
| } | ||
|
|
||
| @Override | ||
| public WxMediaUploadResult execute(String uri, InputStreamData data, WxType wxType) throws WxErrorException, IOException { | ||
|
|
||
| RequestBody body = new MultipartBody.Builder() | ||
| .setType(MediaType.parse("multipart/form-data")) | ||
| .addFormDataPart("media", data.getFilename(), RequestBody.create(this.toByteArray(data.getInputStream()), MediaType.parse("application/octet-stream"))) | ||
| .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 WxMediaUploadResult.fromJson(responseContent); | ||
| } | ||
|
|
||
|
|
||
| public byte[] toByteArray(InputStream input) throws IOException { | ||
| try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { | ||
| byte[] buffer = new byte[4096]; | ||
| int n = 0; | ||
| while (-1 != (n = input.read(buffer))) { | ||
| output.write(buffer, 0, n); | ||
| } | ||
| return output.toByteArray(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.