Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -42,5 +43,31 @@ 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
*/
VideoUploadResult videoUploadV3(File videoFile) throws WxPayException, IOException;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

该方法签名声明会抛出 IOException,但 Javadoc 的 @throws 仅写了 WxPayException,文档与代码不一致。

Other Locations
  • weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/MerchantMediaService.java:71

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎


/**
* <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 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;

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里第二次调用仍然是 videoUploadV3(file),并没有覆盖到新增的 videoUploadV3(InputStream, String) 重载,和 PR 描述“覆盖两种上传方式”不一致。

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

String mediaId2 = videoUploadResult2.getMediaId();

log.info("视频上传成功2,mediaId2:[{}]", mediaId2);

}
Comment on lines +56 to +76
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

测试方法只测试了File参数版本的videoUploadV3方法,缺少对videoUploadV3(InputStream, String)重载方法的测试覆盖。由于该重载方法使用了不同的实现逻辑(通过ByteArrayOutputStream缓冲),建议添加测试用例以确保两种上传方式都能正常工作。

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已添加testVideoUploadV3WithInputStream测试方法,覆盖InputStream方式的视频上传。提交:867293e


@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);
}

}
}