From 1a0d8882454ab1028dca9e64dc8998cd9907ec31 Mon Sep 17 00:00:00 2001 From: Sean Sun <1194458432@qq.com> Date: Tue, 30 Jul 2024 19:24:21 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20#3339=20=E3=80=90=E4=BC=81=E4=B8=9A?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E3=80=91=E5=A2=9E=E5=8A=A0=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E7=B4=A0=E6=9D=90=E7=9A=84=E9=87=8D=E8=BD=BD?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/cp/api/WxCpMediaService.java | 26 +++++++++++++++++++ .../cp/api/impl/WxCpMediaServiceImpl.java | 24 +++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java index d0d4b661b..82f6db917 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java @@ -53,6 +53,32 @@ public interface WxCpMediaService { WxMediaUploadResult upload(String mediaType, String filename, String url) throws WxErrorException, IOException; + /** + *
+ * 上传多媒体文件. + *+ * + * @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts} + * @param file 文件对象, 上传的文件内容 + * @param filename 上传内容的实际文件名.例如:wework.txt + * @return wx media upload result + * @throws WxErrorException the wx error exception + */ + WxMediaUploadResult upload(String mediaType, File file, String filename) throws WxErrorException; + + /** + *
+ * 上传多媒体文件. + *+ * + * @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts} + * @param inputStream 上传的文件内容 + * @param filename 上传内容的实际文件名.例如:wework.txt + * @return wx media upload result + * @throws WxErrorException the wx error exception + */ + WxMediaUploadResult upload(String mediaType, InputStream inputStream, String filename) throws WxErrorException; + /** * 上传多媒体文件. * diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java index 7953d69e3..863dd7c1d 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java @@ -3,6 +3,7 @@ package me.chanjar.weixin.cp.api.impl; import lombok.RequiredArgsConstructor; import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.common.error.WxRuntimeException; import me.chanjar.weixin.common.util.fs.FileUtils; import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor; import me.chanjar.weixin.common.util.http.InputStreamData; @@ -16,6 +17,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.file.Files; import java.util.UUID; import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Media.*; @@ -67,6 +69,28 @@ public class WxCpMediaServiceImpl implements WxCpMediaService { } } + @Override + public WxMediaUploadResult upload(String mediaType, File file, String filename) throws WxErrorException { + if(!file.exists()){ + throw new WxRuntimeException("文件[" + file.getAbsolutePath() + "]不存在"); + } + try (InputStream inputStream = Files.newInputStream(file.toPath())) { + return this.mainService.execute(MediaInputStreamUploadRequestExecutor.create(this.mainService.getRequestHttp()) + , this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType), + new InputStreamData(inputStream, filename)); + } catch (IOException e) { + throw new WxRuntimeException(e); + } + } + + @Override + public WxMediaUploadResult upload(String mediaType, InputStream inputStream, String filename) throws WxErrorException{ + return this.mainService.execute(MediaInputStreamUploadRequestExecutor.create(this.mainService.getRequestHttp()) + , this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType), + new InputStreamData(inputStream, filename)); + } + + @Override public WxMediaUploadResult upload(String mediaType, File file) throws WxErrorException { return this.mainService.execute(MediaUploadRequestExecutor.create(this.mainService.getRequestHttp()),