From b90de5ec06c6715a3fb942812220b87db2c6fc80 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Jan 2026 14:37:48 +0000 Subject: [PATCH 1/6] Initial plan From b733aa466c4266cad282c2300da1d20d04f0464b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Jan 2026 14:43:12 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E6=94=AF=E4=BB=98=E8=A7=86=E9=A2=91=E4=B8=8A=E4=BC=A0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com> --- .../wxpay/bean/media/VideoUploadResult.java | 29 +++++++++++++++ .../wxpay/service/MerchantMediaService.java | 27 ++++++++++++++ .../impl/MerchantMediaServiceImpl.java | 37 +++++++++++++++++++ .../wxpay/v3/WechatPayUploadHttpPost.java | 18 ++++++++- .../impl/MerchantMediaServiceImplTest.java | 25 +++++++++++++ 5 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/media/VideoUploadResult.java diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/media/VideoUploadResult.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/media/VideoUploadResult.java new file mode 100644 index 0000000000..41d827359e --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/media/VideoUploadResult.java @@ -0,0 +1,29 @@ +package com.github.binarywang.wxpay.bean.media; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.NoArgsConstructor; +import me.chanjar.weixin.common.util.json.WxGsonBuilder; + +/** + * 视频文件上传返回结果对象 + * + * @author Binary Wang + */ +@NoArgsConstructor +@Data +public class VideoUploadResult { + + public static VideoUploadResult fromJson(String json) { + return WxGsonBuilder.create().fromJson(json, VideoUploadResult.class); + } + + /** + * 媒体文件标识 Id + *

+ * 微信返回的媒体文件标识Id。 + * 示例值:6uqyGjGrCf2GtyXP8bxrbuH9-aAoTjH-rKeSl3Lf4_So6kdkQu4w8BYVP3bzLtvR38lxt4PjtCDXsQpzqge_hQEovHzOhsLleGFQVRF-U_0 + */ + @SerializedName("media_id") + private String mediaId; +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/MerchantMediaService.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/MerchantMediaService.java index 0e35dbb68b..7e63e4e198 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/MerchantMediaService.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/MerchantMediaService.java @@ -1,6 +1,7 @@ package com.github.binarywang.wxpay.service; import com.github.binarywang.wxpay.bean.media.ImageUploadResult; +import com.github.binarywang.wxpay.bean.media.VideoUploadResult; import com.github.binarywang.wxpay.exception.WxPayException; import java.io.File; @@ -42,5 +43,31 @@ public interface MerchantMediaService { */ ImageUploadResult imageUploadV3(InputStream inputStream, String fileName) throws WxPayException, IOException; + /** + *

+   * 通用接口-视频上传API
+   * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/tool/chapter3_2.shtml
+   * 接口链接:https://api.mch.weixin.qq.com/v3/merchant/media/video_upload
+   * 
+ * + * @param videoFile 需要上传的视频文件 + * @return VideoUploadResult 微信返回的媒体文件标识Id。示例值:6uqyGjGrCf2GtyXP8bxrbuH9-aAoTjH-rKeSl3Lf4_So6kdkQu4w8BYVP3bzLtvR38lxt4PjtCDXsQpzqge_hQEovHzOhsLleGFQVRF-U_0 + * @throws WxPayException the wx pay exception + */ + VideoUploadResult videoUploadV3(File videoFile) throws WxPayException, IOException; + + /** + *
+   * 通用接口-视频上传API
+   * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/tool/chapter3_2.shtml
+   * 接口链接:https://api.mch.weixin.qq.com/v3/merchant/media/video_upload
+   * 
+ * + * @param inputStream 需要上传的视频文件流 + * @param fileName 需要上传的视频文件名 + * @return VideoUploadResult 微信返回的媒体文件标识Id。示例值:6uqyGjGrCf2GtyXP8bxrbuH9-aAoTjH-rKeSl3Lf4_So6kdkQu4w8BYVP3bzLtvR38lxt4PjtCDXsQpzqge_hQEovHzOhsLleGFQVRF-U_0 + * @throws WxPayException the wx pay exception + */ + VideoUploadResult videoUploadV3(InputStream inputStream, String fileName) throws WxPayException, IOException; } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImpl.java index 7952513f56..4def5cdc3e 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImpl.java @@ -1,6 +1,7 @@ package com.github.binarywang.wxpay.service.impl; import com.github.binarywang.wxpay.bean.media.ImageUploadResult; +import com.github.binarywang.wxpay.bean.media.VideoUploadResult; import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.service.MerchantMediaService; import com.github.binarywang.wxpay.service.WxPayService; @@ -57,4 +58,40 @@ public ImageUploadResult imageUploadV3(InputStream inputStream, String fileName) } } + @Override + public VideoUploadResult videoUploadV3(File videoFile) throws WxPayException, IOException { + String url = String.format("%s/v3/merchant/media/video_upload", this.payService.getPayBaseUrl()); + + try (FileInputStream s1 = new FileInputStream(videoFile)) { + String sha256 = DigestUtils.sha256Hex(s1); + try (InputStream s2 = new FileInputStream(videoFile)) { + WechatPayUploadHttpPost request = new WechatPayUploadHttpPost.Builder(URI.create(url)) + .withVideo(videoFile.getName(), sha256, s2) + .build(); + String result = this.payService.postV3(url, request); + return VideoUploadResult.fromJson(result); + } + } + } + + @Override + public VideoUploadResult videoUploadV3(InputStream inputStream, String fileName) throws WxPayException, IOException { + String url = String.format("%s/v3/merchant/media/video_upload", this.payService.getPayBaseUrl()); + try(ByteArrayOutputStream bos = new ByteArrayOutputStream()) { + byte[] buffer = new byte[2048]; + int len; + while ((len = inputStream.read(buffer)) > -1) { + bos.write(buffer, 0, len); + } + bos.flush(); + byte[] data = bos.toByteArray(); + String sha256 = DigestUtils.sha256Hex(data); + WechatPayUploadHttpPost request = new WechatPayUploadHttpPost.Builder(URI.create(url)) + .withVideo(fileName, sha256, new ByteArrayInputStream(data)) + .build(); + String result = this.payService.postV3(url, request); + return VideoUploadResult.fromJson(result); + } + } + } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WechatPayUploadHttpPost.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WechatPayUploadHttpPost.java index 5f5e52d2ff..d5c00cde9a 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WechatPayUploadHttpPost.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WechatPayUploadHttpPost.java @@ -50,13 +50,27 @@ public Builder withImage(String fileName, String fileSha256, InputStream inputSt return this; } + public Builder withVideo(String fileName, String fileSha256, InputStream inputStream) { + this.fileName = fileName; + this.fileSha256 = fileSha256; + this.fileInputStream = inputStream; + + String mimeType = URLConnection.guessContentTypeFromName(fileName); + if (mimeType == null) { + this.fileContentType = ContentType.APPLICATION_OCTET_STREAM; + } else { + this.fileContentType = ContentType.create(mimeType); + } + return this; + } + public WechatPayUploadHttpPost build() { if (fileName == null || fileSha256 == null || fileInputStream == null) { - throw new IllegalArgumentException("缺少待上传图片文件信息"); + throw new IllegalArgumentException("缺少待上传文件信息"); } if (uri == null) { - throw new IllegalArgumentException("缺少上传图片接口URL"); + throw new IllegalArgumentException("缺少上传文件接口URL"); } String meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", fileName, fileSha256); diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImplTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImplTest.java index c8dd069b44..dcc89ffac4 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImplTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImplTest.java @@ -1,6 +1,7 @@ package com.github.binarywang.wxpay.service.impl; import com.github.binarywang.wxpay.bean.media.ImageUploadResult; +import com.github.binarywang.wxpay.bean.media.VideoUploadResult; import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.service.MerchantMediaService; import com.github.binarywang.wxpay.service.WxPayService; @@ -51,4 +52,28 @@ public void testImageUploadV3() throws WxPayException, IOException { log.info("mediaId2:[{}]",mediaId2); } + + @Test + public void testVideoUploadV3() throws WxPayException, IOException { + + MerchantMediaService merchantMediaService = new MerchantMediaServiceImpl(wxPayService); + + String filePath = "你的视频文件的路径地址"; +// String filePath = "WxJava/test-video.mp4"; + + File file = new File(filePath); + + VideoUploadResult videoUploadResult = merchantMediaService.videoUploadV3(file); + String mediaId = videoUploadResult.getMediaId(); + + log.info("视频上传成功,mediaId:[{}]", mediaId); + + File file2 = new File(filePath); + + VideoUploadResult videoUploadResult2 = merchantMediaService.videoUploadV3(file2); + String mediaId2 = videoUploadResult2.getMediaId(); + + log.info("视频上传成功2,mediaId2:[{}]", mediaId2); + + } } From 16561c2fb5c2c649e16a2c84462b31f2f7a93c84 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Jan 2026 14:45:50 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=B6=88=E9=99=A4=E9=87=8D=E5=A4=8D=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E6=8F=90=E5=8F=96withMedia=E5=85=B1=E7=94=A8=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com> --- .../wxpay/v3/WechatPayUploadHttpPost.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WechatPayUploadHttpPost.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WechatPayUploadHttpPost.java index d5c00cde9a..3387f37e3d 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WechatPayUploadHttpPost.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WechatPayUploadHttpPost.java @@ -35,7 +35,7 @@ public Builder(URI uri) { this.uri = uri; } - public Builder withImage(String fileName, String fileSha256, InputStream inputStream) { + private Builder withMedia(String fileName, String fileSha256, InputStream inputStream) { this.fileName = fileName; this.fileSha256 = fileSha256; this.fileInputStream = inputStream; @@ -50,18 +50,12 @@ public Builder withImage(String fileName, String fileSha256, InputStream inputSt return this; } - public Builder withVideo(String fileName, String fileSha256, InputStream inputStream) { - this.fileName = fileName; - this.fileSha256 = fileSha256; - this.fileInputStream = inputStream; + public Builder withImage(String fileName, String fileSha256, InputStream inputStream) { + return withMedia(fileName, fileSha256, inputStream); + } - String mimeType = URLConnection.guessContentTypeFromName(fileName); - if (mimeType == null) { - this.fileContentType = ContentType.APPLICATION_OCTET_STREAM; - } else { - this.fileContentType = ContentType.create(mimeType); - } - return this; + public Builder withVideo(String fileName, String fileSha256, InputStream inputStream) { + return withMedia(fileName, fileSha256, inputStream); } public WechatPayUploadHttpPost build() { From 32e7de5d79f1e0ee28201c2fb1b6588d3bc2aad0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Jan 2026 14:48:01 +0000 Subject: [PATCH 4/6] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=A4=8D=E7=94=A8File=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com> --- .../wxpay/service/impl/MerchantMediaServiceImplTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImplTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImplTest.java index dcc89ffac4..2d1781b0ba 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImplTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImplTest.java @@ -68,9 +68,7 @@ public void testVideoUploadV3() throws WxPayException, IOException { log.info("视频上传成功,mediaId:[{}]", mediaId); - File file2 = new File(filePath); - - VideoUploadResult videoUploadResult2 = merchantMediaService.videoUploadV3(file2); + VideoUploadResult videoUploadResult2 = merchantMediaService.videoUploadV3(file); String mediaId2 = videoUploadResult2.getMediaId(); log.info("视频上传成功2,mediaId2:[{}]", mediaId2); From 0e2322601a506fc62c1a228e9f3ee7c5e7da3e3b Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Tue, 27 Jan 2026 10:48:11 +0800 Subject: [PATCH 5/6] Update weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImpl.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../binarywang/wxpay/service/impl/MerchantMediaServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImpl.java index 4def5cdc3e..188544544f 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImpl.java @@ -77,7 +77,7 @@ public VideoUploadResult videoUploadV3(File videoFile) throws WxPayException, IO @Override public VideoUploadResult videoUploadV3(InputStream inputStream, String fileName) throws WxPayException, IOException { String url = String.format("%s/v3/merchant/media/video_upload", this.payService.getPayBaseUrl()); - try(ByteArrayOutputStream bos = new ByteArrayOutputStream()) { + try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { byte[] buffer = new byte[2048]; int len; while ((len = inputStream.read(buffer)) > -1) { From 867293e73694361cfb16f5e0cd0b1fad6c23fa0a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 27 Jan 2026 02:52:25 +0000 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BD=9C=E8=80=85?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E4=B8=BAcopilot=E5=B9=B6=E6=B7=BB=E5=8A=A0In?= =?UTF-8?q?putStream=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com> --- .../wxpay/bean/media/VideoUploadResult.java | 2 +- .../impl/MerchantMediaServiceImplTest.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/media/VideoUploadResult.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/media/VideoUploadResult.java index 41d827359e..615cbbff5f 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/media/VideoUploadResult.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/media/VideoUploadResult.java @@ -8,7 +8,7 @@ /** * 视频文件上传返回结果对象 * - * @author Binary Wang + * @author copilot */ @NoArgsConstructor @Data diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImplTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImplTest.java index 2d1781b0ba..845992e43c 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImplTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/MerchantMediaServiceImplTest.java @@ -74,4 +74,23 @@ public void testVideoUploadV3() throws WxPayException, IOException { log.info("视频上传成功2,mediaId2:[{}]", mediaId2); } + + @Test + public void testVideoUploadV3WithInputStream() throws WxPayException, IOException { + + MerchantMediaService merchantMediaService = new MerchantMediaServiceImpl(wxPayService); + + String filePath = "你的视频文件的路径地址"; +// String filePath = "WxJava/test-video.mp4"; + + File file = new File(filePath); + + try (java.io.FileInputStream inputStream = new java.io.FileInputStream(file)) { + VideoUploadResult videoUploadResult = merchantMediaService.videoUploadV3(inputStream, file.getName()); + String mediaId = videoUploadResult.getMediaId(); + + log.info("通过InputStream上传视频成功,mediaId:[{}]", mediaId); + } + + } }