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..615cbbff5f --- /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 copilot + */ +@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..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 @@ -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..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,13 +50,21 @@ public Builder withImage(String fileName, String fileSha256, InputStream inputSt return this; } + public Builder withImage(String fileName, String fileSha256, InputStream inputStream) { + return withMedia(fileName, fileSha256, inputStream); + } + + public Builder withVideo(String fileName, String fileSha256, InputStream inputStream) { + return withMedia(fileName, fileSha256, inputStream); + } + 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..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 @@ -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,45 @@ 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); + + VideoUploadResult videoUploadResult2 = merchantMediaService.videoUploadV3(file); + String mediaId2 = videoUploadResult2.getMediaId(); + + 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); + } + + } }