11package com .github .binarywang .wxpay .service .impl ;
22
33import com .github .binarywang .wxpay .bean .media .ImageUploadResult ;
4+ import com .github .binarywang .wxpay .bean .media .VideoUploadResult ;
45import com .github .binarywang .wxpay .exception .WxPayException ;
56import com .github .binarywang .wxpay .service .MerchantMediaService ;
67import com .github .binarywang .wxpay .service .WxPayService ;
@@ -40,7 +41,7 @@ public ImageUploadResult imageUploadV3(File imageFile) throws WxPayException,IOE
4041 @ Override
4142 public ImageUploadResult imageUploadV3 (InputStream inputStream , String fileName ) throws WxPayException , IOException {
4243 String url = String .format ("%s/v3/merchant/media/upload" , this .payService .getPayBaseUrl ());
43- try (ByteArrayOutputStream bos = new ByteArrayOutputStream ()) {
44+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream ()) {
4445 byte [] buffer = new byte [2048 ];
4546 int len ;
4647 while ((len = inputStream .read (buffer )) > -1 ) {
@@ -57,4 +58,40 @@ public ImageUploadResult imageUploadV3(InputStream inputStream, String fileName)
5758 }
5859 }
5960
61+ @ Override
62+ public VideoUploadResult videoUploadV3 (File videoFile ) throws WxPayException , IOException {
63+ String url = String .format ("%s/v3/merchant/media/video_upload" , this .payService .getPayBaseUrl ());
64+
65+ try (FileInputStream s1 = new FileInputStream (videoFile )) {
66+ String sha256 = DigestUtils .sha256Hex (s1 );
67+ try (InputStream s2 = new FileInputStream (videoFile )) {
68+ WechatPayUploadHttpPost request = new WechatPayUploadHttpPost .Builder (URI .create (url ))
69+ .withVideo (videoFile .getName (), sha256 , s2 )
70+ .build ();
71+ String result = this .payService .postV3 (url , request );
72+ return VideoUploadResult .fromJson (result );
73+ }
74+ }
75+ }
76+
77+ @ Override
78+ public VideoUploadResult videoUploadV3 (InputStream inputStream , String fileName ) throws WxPayException , IOException {
79+ String url = String .format ("%s/v3/merchant/media/video_upload" , this .payService .getPayBaseUrl ());
80+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream ()) {
81+ byte [] buffer = new byte [2048 ];
82+ int len ;
83+ while ((len = inputStream .read (buffer )) > -1 ) {
84+ bos .write (buffer , 0 , len );
85+ }
86+ bos .flush ();
87+ byte [] data = bos .toByteArray ();
88+ String sha256 = DigestUtils .sha256Hex (data );
89+ WechatPayUploadHttpPost request = new WechatPayUploadHttpPost .Builder (URI .create (url ))
90+ .withVideo (fileName , sha256 , new ByteArrayInputStream (data ))
91+ .build ();
92+ String result = this .payService .postV3 (url , request );
93+ return VideoUploadResult .fromJson (result );
94+ }
95+ }
96+
6097}
0 commit comments