|
3 | 3 | import lombok.RequiredArgsConstructor;
|
4 | 4 | import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
5 | 5 | import me.chanjar.weixin.common.error.WxErrorException;
|
| 6 | +import me.chanjar.weixin.common.error.WxRuntimeException; |
6 | 7 | import me.chanjar.weixin.common.util.fs.FileUtils;
|
7 | 8 | import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
|
8 | 9 | import me.chanjar.weixin.common.util.http.InputStreamData;
|
|
16 | 17 | import java.io.InputStream;
|
17 | 18 | import java.net.HttpURLConnection;
|
18 | 19 | import java.net.URL;
|
| 20 | +import java.nio.file.Files; |
19 | 21 | import java.util.UUID;
|
20 | 22 |
|
21 | 23 | import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Media.*;
|
@@ -67,6 +69,28 @@ public WxMediaUploadResult upload(String mediaType, String filename, String url)
|
67 | 69 | }
|
68 | 70 | }
|
69 | 71 |
|
| 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 | + |
70 | 94 | @Override
|
71 | 95 | public WxMediaUploadResult upload(String mediaType, File file) throws WxErrorException {
|
72 | 96 | return this.mainService.execute(MediaUploadRequestExecutor.create(this.mainService.getRequestHttp()),
|
|
0 commit comments