1
0
mirror of synced 2026-02-04 08:18:53 +08:00

🆕 #3871 【微信支付】增加视频上传接口

This commit is contained in:
Copilot
2026-01-31 00:53:44 +08:00
committed by GitHub
parent dec6792333
commit 780c24bda0
5 changed files with 150 additions and 4 deletions

View File

@@ -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
* <p>
* 微信返回的媒体文件标识Id。
* 示例值6uqyGjGrCf2GtyXP8bxrbuH9-aAoTjH-rKeSl3Lf4_So6kdkQu4w8BYVP3bzLtvR38lxt4PjtCDXsQpzqge_hQEovHzOhsLleGFQVRF-U_0
*/
@SerializedName("media_id")
private String mediaId;
}

View File

@@ -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,34 @@ public interface MerchantMediaService {
*/
ImageUploadResult imageUploadV3(InputStream inputStream, String fileName) throws WxPayException, IOException;
/**
* <pre>
* 通用接口-视频上传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
* </pre>
*
* @param videoFile 需要上传的视频文件
* @return VideoUploadResult 微信返回的媒体文件标识Id。示例值6uqyGjGrCf2GtyXP8bxrbuH9-aAoTjH-rKeSl3Lf4_So6kdkQu4w8BYVP3bzLtvR38lxt4PjtCDXsQpzqge_hQEovHzOhsLleGFQVRF-U_0
* @throws WxPayException the wx pay exception
* @throws IOException the io exception
*/
VideoUploadResult videoUploadV3(File videoFile) throws WxPayException, IOException;
/**
* <pre>
* 通用接口-视频上传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
* 注意此方法会将整个视频流读入内存计算SHA256后再上传大文件可能导致OOM建议大文件使用File方式上传
* </pre>
*
* @param inputStream 需要上传的视频文件流
* @param fileName 需要上传的视频文件名
* @return VideoUploadResult 微信返回的媒体文件标识Id。示例值6uqyGjGrCf2GtyXP8bxrbuH9-aAoTjH-rKeSl3Lf4_So6kdkQu4w8BYVP3bzLtvR38lxt4PjtCDXsQpzqge_hQEovHzOhsLleGFQVRF-U_0
* @throws WxPayException the wx pay exception
* @throws IOException the io exception
*/
VideoUploadResult videoUploadV3(InputStream inputStream, String fileName) throws WxPayException, IOException;
}

View File

@@ -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;
@@ -40,7 +41,7 @@ public class MerchantMediaServiceImpl implements MerchantMediaService {
@Override
public ImageUploadResult imageUploadV3(InputStream inputStream, String fileName) throws WxPayException, IOException {
String url = String.format("%s/v3/merchant/media/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) {
@@ -57,4 +58,40 @@ public class MerchantMediaServiceImpl implements MerchantMediaService {
}
}
@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);
}
}
}

View File

@@ -35,7 +35,7 @@ public class WechatPayUploadHttpPost extends HttpPost {
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 class WechatPayUploadHttpPost extends HttpPost {
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);

View File

@@ -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 class MerchantMediaServiceImplTest {
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("视频上传成功2mediaId2[{}]", 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);
}
}
}