-
-
Notifications
You must be signed in to change notification settings - Fork 9k
支持upload方法在上传文件时添加额外表单字段 #3864
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
Open
Copilot
wants to merge
4
commits into
develop
Choose a base branch
from
copilot/fix-upload-method-json-data
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
支持upload方法在上传文件时添加额外表单字段 #3864
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| # CommonUploadParam 额外表单字段功能使用示例 | ||
|
|
||
| ## 背景 | ||
|
|
||
| 微信公众号在上传永久视频素材时,需要在POST请求中同时提交文件和一个名为`description`的表单字段,该字段包含视频的描述信息(JSON格式)。 | ||
|
|
||
| 根据微信公众号文档: | ||
| > 在上传视频素材时需要POST另一个表单,id为description,包含素材的描述信息,内容格式为JSON,格式如下: | ||
| > ```json | ||
| > { | ||
| > "title": "VIDEO_TITLE", | ||
| > "introduction": "INTRODUCTION" | ||
| > } | ||
| > ``` | ||
| ## 解决方案 | ||
| `CommonUploadParam` 类已经扩展支持额外的表单字段,可以在上传文件的同时提交其他表单数据。 | ||
| ## 使用示例 | ||
| ### 1. 基本用法 - 上传永久视频素材 | ||
| ```java | ||
| import me.chanjar.weixin.common.bean.CommonUploadParam; | ||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||
| import me.chanjar.weixin.mp.api.WxMpService; | ||
| import java.io.File; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| public class VideoMaterialUploadExample { | ||
| public void uploadVideoMaterial(WxMpService wxMpService) throws Exception { | ||
| // 准备视频文件 | ||
| File videoFile = new File("/path/to/video.mp4"); | ||
| // 创建上传参数 | ||
| CommonUploadParam uploadParam = CommonUploadParam.fromFile("media", videoFile); | ||
| // 准备视频描述信息(JSON格式) | ||
| Map<String, String> description = new HashMap<>(); | ||
| description.put("title", "我的视频标题"); | ||
| description.put("introduction", "这是一个精彩的视频介绍"); | ||
| String descriptionJson = WxGsonBuilder.create().toJson(description); | ||
| // 添加description表单字段 | ||
| uploadParam.addFormField("description", descriptionJson); | ||
| // 调用微信API上传 | ||
| String url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=video"; | ||
| String response = wxMpService.upload(url, uploadParam); | ||
| System.out.println("上传成功:" + response); | ||
| } | ||
| } | ||
| ``` | ||
| ### 2. 链式调用风格 | ||
|
|
||
| ```java | ||
| import me.chanjar.weixin.common.bean.CommonUploadParam; | ||
| import com.google.gson.JsonObject; | ||
|
|
||
| public class ChainStyleExample { | ||
|
|
||
| public void uploadWithChainStyle(WxMpService wxMpService) throws Exception { | ||
| File videoFile = new File("/path/to/video.mp4"); | ||
|
|
||
| // 准备描述信息 | ||
| JsonObject description = new JsonObject(); | ||
| description.addProperty("title", "视频标题"); | ||
| description.addProperty("introduction", "视频介绍"); | ||
|
|
||
| // 使用链式调用 | ||
| String response = wxMpService.upload( | ||
| "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=video", | ||
| CommonUploadParam.fromFile("media", videoFile) | ||
| .addFormField("description", description.toString()) | ||
| ); | ||
|
|
||
| System.out.println("上传成功:" + response); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### 3. 多个额外表单字段 | ||
|
|
||
| ```java | ||
| import me.chanjar.weixin.common.bean.CommonUploadParam; | ||
|
|
||
| public class MultipleFormFieldsExample { | ||
|
|
||
| public void uploadWithMultipleFields(WxMpService wxMpService) throws Exception { | ||
| File file = new File("/path/to/file.jpg"); | ||
|
|
||
| // 可以添加多个表单字段 | ||
| CommonUploadParam uploadParam = CommonUploadParam.fromFile("media", file) | ||
| .addFormField("field1", "value1") | ||
| .addFormField("field2", "value2") | ||
| .addFormField("field3", "value3"); | ||
|
|
||
| String response = wxMpService.upload("https://api.weixin.qq.com/some/upload/url", uploadParam); | ||
|
|
||
| System.out.println("上传成功:" + response); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### 4. 从字节数组上传并添加表单字段 | ||
|
|
||
| ```java | ||
| import me.chanjar.weixin.common.bean.CommonUploadParam; | ||
|
|
||
| public class ByteArrayUploadExample { | ||
|
|
||
| public void uploadFromBytes(WxMpService wxMpService) throws Exception { | ||
| // 从字节数组创建上传参数 | ||
| byte[] fileBytes = getFileBytes(); | ||
|
|
||
| CommonUploadParam uploadParam = CommonUploadParam | ||
| .fromBytes("media", "video.mp4", fileBytes) | ||
| .addFormField("description", "{\"title\":\"标题\",\"introduction\":\"介绍\"}"); | ||
|
|
||
| String response = wxMpService.upload( | ||
| "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=video", | ||
| uploadParam | ||
| ); | ||
|
|
||
| System.out.println("上传成功:" + response); | ||
| } | ||
|
|
||
| private byte[] getFileBytes() { | ||
| // 获取文件字节数组的逻辑 | ||
| return new byte[0]; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## API 说明 | ||
|
|
||
| ### CommonUploadParam 类 | ||
|
|
||
| #### 构造方法 | ||
| - `fromFile(String name, File file)` - 从文件创建上传参数 | ||
| - `fromBytes(String name, String fileName, byte[] bytes)` - 从字节数组创建上传参数 | ||
|
|
||
| #### 方法 | ||
| - `addFormField(String fieldName, String fieldValue)` - 添加额外的表单字段,返回当前对象支持链式调用 | ||
| - `getFormFields()` - 获取所有额外的表单字段(Map类型) | ||
| - `setFormFields(Map<String, String> formFields)` - 设置额外的表单字段 | ||
|
|
||
| #### 属性 | ||
| - `name` - 文件对应的接口参数名称(如:media) | ||
| - `data` - 上传数据(CommonUploadData对象) | ||
| - `formFields` - 额外的表单字段(可选,Map<String, String>类型) | ||
|
|
||
| ## 注意事项 | ||
|
|
||
| 1. **表单字段是可选的**:如果不需要额外的表单字段,可以不调用`addFormField`方法 | ||
| 2. **JSON格式**:对于需要JSON格式的表单字段(如description),需要先将对象转换为JSON字符串 | ||
| 3. **编码**:表单字段值会使用UTF-8编码 | ||
| 4. **所有HTTP客户端支持**:该功能在所有HTTP客户端实现中都得到支持(OkHttp、Apache HttpClient、HttpComponents、JoddHttp) | ||
|
|
||
| ## 兼容性 | ||
|
|
||
| 该功能向后兼容,现有的不使用额外表单字段的代码无需修改即可继续工作。 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,6 +10,8 @@ | |||||||||||||||||||||||||||||||||
| import java.io.ByteArrayInputStream; | ||||||||||||||||||||||||||||||||||
| import java.io.File; | ||||||||||||||||||||||||||||||||||
| import java.io.Serializable; | ||||||||||||||||||||||||||||||||||
| import java.util.HashMap; | ||||||||||||||||||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * 通用文件上传参数 | ||||||||||||||||||||||||||||||||||
|
|
@@ -34,6 +36,13 @@ public class CommonUploadParam implements Serializable { | |||||||||||||||||||||||||||||||||
| @NotNull | ||||||||||||||||||||||||||||||||||
| private CommonUploadData data; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * 额外的表单字段,用于在上传文件的同时提交其他表单数据 | ||||||||||||||||||||||||||||||||||
| * 例如:上传视频素材时需要提交description字段(JSON格式的视频描述信息) | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| @Nullable | ||||||||||||||||||||||||||||||||||
| private Map<String, String> formFields; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| /** | |
| /** | |
| * 为保持向后兼容保留的 2 参数构造函数。 | |
| * <p> | |
| * 仅设置文件参数名和上传数据,额外表单字段将为 {@code null}。 | |
| * | |
| * @param name 参数名,如:media | |
| * @param data 上传数据 | |
| * @deprecated 请使用包含 formFields 参数的构造函数或静态工厂方法 {@link #fromFile(String, File)}、{@link #fromBytes(String, String, byte[])} | |
| */ | |
| @Deprecated | |
| public CommonUploadParam(@NotNull String name, @NotNull CommonUploadData data) { | |
| this(name, data, null); | |
| } | |
| /** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
文档声明"该功能向后兼容",但实际上存在 API 破坏性变更。由于添加了新字段
formFields,Lombok 的@AllArgsConstructor将 2 参数构造函数替换为 3 参数构造函数,导致直接使用new CommonUploadParam(name, data)的现有代码无法编译。建议更新文档说明这一变更,或者修改
CommonUploadParam类以保持真正的向后兼容性(例如添加 2 参数构造函数)。