Skip to content

Commit 1a0d888

Browse files
lyfucibinarywang
authored andcommitted
🆕 #3339 【企业微信】增加上传临时素材的重载方法
1 parent 9e0b87a commit 1a0d888

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java

+26
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,32 @@ WxMediaUploadResult upload(String mediaType, String fileType, InputStream inputS
5353
WxMediaUploadResult upload(String mediaType, String filename, String url)
5454
throws WxErrorException, IOException;
5555

56+
/**
57+
* <pre>
58+
* 上传多媒体文件.
59+
* </pre>
60+
*
61+
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
62+
* @param file 文件对象, 上传的文件内容
63+
* @param filename 上传内容的实际文件名.例如:wework.txt
64+
* @return wx media upload result
65+
* @throws WxErrorException the wx error exception
66+
*/
67+
WxMediaUploadResult upload(String mediaType, File file, String filename) throws WxErrorException;
68+
69+
/**
70+
* <pre>
71+
* 上传多媒体文件.
72+
* </pre>
73+
*
74+
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
75+
* @param inputStream 上传的文件内容
76+
* @param filename 上传内容的实际文件名.例如:wework.txt
77+
* @return wx media upload result
78+
* @throws WxErrorException the wx error exception
79+
*/
80+
WxMediaUploadResult upload(String mediaType, InputStream inputStream, String filename) throws WxErrorException;
81+
5682
/**
5783
* 上传多媒体文件.
5884
*

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import lombok.RequiredArgsConstructor;
44
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
55
import me.chanjar.weixin.common.error.WxErrorException;
6+
import me.chanjar.weixin.common.error.WxRuntimeException;
67
import me.chanjar.weixin.common.util.fs.FileUtils;
78
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
89
import me.chanjar.weixin.common.util.http.InputStreamData;
@@ -16,6 +17,7 @@
1617
import java.io.InputStream;
1718
import java.net.HttpURLConnection;
1819
import java.net.URL;
20+
import java.nio.file.Files;
1921
import java.util.UUID;
2022

2123
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Media.*;
@@ -67,6 +69,28 @@ public WxMediaUploadResult upload(String mediaType, String filename, String url)
6769
}
6870
}
6971

72+
@Override
73+
public WxMediaUploadResult upload(String mediaType, File file, String filename) throws WxErrorException {
74+
if(!file.exists()){
75+
throw new WxRuntimeException("文件[" + file.getAbsolutePath() + "]不存在");
76+
}
77+
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
78+
return this.mainService.execute(MediaInputStreamUploadRequestExecutor.create(this.mainService.getRequestHttp())
79+
, this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType),
80+
new InputStreamData(inputStream, filename));
81+
} catch (IOException e) {
82+
throw new WxRuntimeException(e);
83+
}
84+
}
85+
86+
@Override
87+
public WxMediaUploadResult upload(String mediaType, InputStream inputStream, String filename) throws WxErrorException{
88+
return this.mainService.execute(MediaInputStreamUploadRequestExecutor.create(this.mainService.getRequestHttp())
89+
, this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType),
90+
new InputStreamData(inputStream, filename));
91+
}
92+
93+
7094
@Override
7195
public WxMediaUploadResult upload(String mediaType, File file) throws WxErrorException {
7296
return this.mainService.execute(MediaUploadRequestExecutor.create(this.mainService.getRequestHttp()),

0 commit comments

Comments
 (0)