Skip to content

Commit d8f2850

Browse files
authored
上传文件接口支持自定义meta字段 (#103)
* 去除comment_on_pr action * 增加uploadFile接口,支持自定义meta
1 parent 65ef133 commit d8f2850

File tree

3 files changed

+49
-32
lines changed

3 files changed

+49
-32
lines changed

.github/workflows/comment_on_pr.yml

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/com/wechat/pay/contrib/apache/httpclient/WechatPayUploadHttpPost.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,59 @@ public static class Builder {
3131

3232
private final URI uri;
3333
private String fileName;
34-
private String fileSha256;
3534
private InputStream fileInputStream;
3635
private ContentType fileContentType;
36+
private String meta;
3737

3838
public Builder(URI uri) {
39+
if (uri == null) {
40+
throw new IllegalArgumentException("上传文件接口URL为空");
41+
}
3942
this.uri = uri;
4043
}
4144

4245
public Builder withImage(String fileName, String fileSha256, InputStream inputStream) {
46+
if (fileSha256 == null || fileSha256.isEmpty()) {
47+
throw new IllegalArgumentException("文件摘要为空");
48+
}
49+
meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", fileName, fileSha256);
50+
return withFile(fileName, meta, inputStream);
51+
}
52+
53+
public Builder withFile(String fileName, String meta, InputStream inputStream) {
4354
this.fileName = fileName;
44-
this.fileSha256 = fileSha256;
4555
this.fileInputStream = inputStream;
46-
4756
String mimeType = URLConnection.guessContentTypeFromName(fileName);
4857
if (mimeType == null) {
4958
// guess this is a video uploading
5059
this.fileContentType = ContentType.APPLICATION_OCTET_STREAM;
5160
} else {
5261
this.fileContentType = ContentType.create(mimeType);
5362
}
63+
this.meta = meta;
5464
return this;
5565
}
5666

5767
public WechatPayUploadHttpPost build() {
58-
if (fileName == null || fileSha256 == null || fileInputStream == null) {
59-
throw new IllegalArgumentException("缺少待上传图片文件信息");
68+
if (fileName == null || fileName.isEmpty()) {
69+
throw new IllegalArgumentException("文件名称为空");
6070
}
61-
62-
if (uri == null) {
63-
throw new IllegalArgumentException("缺少上传图片接口URL");
71+
if (fileInputStream == null) {
72+
throw new IllegalArgumentException("文件为空");
73+
}
74+
if (fileContentType == null) {
75+
throw new IllegalArgumentException("文件类型为空");
76+
}
77+
if (meta == null || meta.isEmpty()) {
78+
throw new IllegalArgumentException("媒体文件元信息为空");
6479
}
65-
66-
String meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", fileName, fileSha256);
6780
WechatPayUploadHttpPost request = new WechatPayUploadHttpPost(uri, meta);
68-
6981
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
7082
entityBuilder.setMode(HttpMultipartMode.RFC6532)
7183
.addBinaryBody("file", fileInputStream, fileContentType, fileName)
7284
.addTextBody("meta", meta, APPLICATION_JSON);
73-
7485
request.setEntity(entityBuilder.build());
7586
request.addHeader(ACCEPT, APPLICATION_JSON.toString());
76-
7787
return request;
7888
}
7989
}

src/test/java/com/wechat/pay/contrib/apache/httpclient/CertificatesManagerTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,30 @@ public void uploadImageTest() throws Exception {
108108
}
109109
}
110110
}
111+
112+
@Test
113+
public void uploadFileTest() throws Exception {
114+
String filePath = "/your/home/test.png";
115+
116+
URI uri = new URI("https://api.mch.weixin.qq.com/v3/merchant/media/upload");
117+
118+
File file = new File(filePath);
119+
try (FileInputStream fileIs = new FileInputStream(file)) {
120+
String sha256 = DigestUtils.sha256Hex(fileIs);
121+
String meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", file.getName(), sha256);
122+
try (InputStream is = new FileInputStream(file)) {
123+
WechatPayUploadHttpPost request = new WechatPayUploadHttpPost.Builder(uri)
124+
.withFile(file.getName(), meta, is)
125+
.build();
126+
try (CloseableHttpResponse response = httpClient.execute(request)) {
127+
assertEquals(SC_OK, response.getStatusLine().getStatusCode());
128+
HttpEntity entity = response.getEntity();
129+
// do something useful with the response body
130+
// and ensure it is fully consumed
131+
String s = EntityUtils.toString(entity);
132+
System.out.println(s);
133+
}
134+
}
135+
}
136+
}
111137
}

0 commit comments

Comments
 (0)