Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加临时素材上传重载 #3339

Merged
merged 1 commit into from
Jul 30, 2024
Merged
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
Expand Up @@ -53,6 +53,32 @@ WxMediaUploadResult upload(String mediaType, String fileType, InputStream inputS
WxMediaUploadResult upload(String mediaType, String filename, String url)
throws WxErrorException, IOException;

/**
* <pre>
* 上传多媒体文件.
* </pre>
*
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param file 文件对象, 上传的文件内容
* @param filename 上传内容的实际文件名.例如:wework.txt
* @return wx media upload result
* @throws WxErrorException the wx error exception
*/
WxMediaUploadResult upload(String mediaType, File file, String filename) throws WxErrorException;

/**
* <pre>
* 上传多媒体文件.
* </pre>
*
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param inputStream 上传的文件内容
* @param filename 上传内容的实际文件名.例如:wework.txt
* @return wx media upload result
* @throws WxErrorException the wx error exception
*/
WxMediaUploadResult upload(String mediaType, InputStream inputStream, String filename) throws WxErrorException;

/**
* 上传多媒体文件.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.InputStreamData;
Expand All @@ -16,6 +17,7 @@
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.util.UUID;

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

@Override
public WxMediaUploadResult upload(String mediaType, File file, String filename) throws WxErrorException {
if(!file.exists()){
throw new WxRuntimeException("文件[" + file.getAbsolutePath() + "]不存在");
}
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
return this.mainService.execute(MediaInputStreamUploadRequestExecutor.create(this.mainService.getRequestHttp())
, this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType),
new InputStreamData(inputStream, filename));
} catch (IOException e) {
throw new WxRuntimeException(e);
}
}

@Override
public WxMediaUploadResult upload(String mediaType, InputStream inputStream, String filename) throws WxErrorException{
return this.mainService.execute(MediaInputStreamUploadRequestExecutor.create(this.mainService.getRequestHttp())
, this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType),
new InputStreamData(inputStream, filename));
}


@Override
public WxMediaUploadResult upload(String mediaType, File file) throws WxErrorException {
return this.mainService.execute(MediaUploadRequestExecutor.create(this.mainService.getRequestHttp()),
Expand Down