Skip to content
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
19 changes: 0 additions & 19 deletions .github/workflows/comment_on_pr.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,59 @@ public static class Builder {

private final URI uri;
private String fileName;
private String fileSha256;
private InputStream fileInputStream;
private ContentType fileContentType;
private String meta;

public Builder(URI uri) {
if (uri == null) {
throw new IllegalArgumentException("上传文件接口URL为空");
}
this.uri = uri;
}

public Builder withImage(String fileName, String fileSha256, InputStream inputStream) {
if (fileSha256 == null || fileSha256.isEmpty()) {
throw new IllegalArgumentException("文件摘要为空");
}
meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", fileName, fileSha256);
return withFile(fileName, meta, inputStream);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么不实现成

string meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", fileName, fileSha256);
return withFile(fileName, meta, inputStream);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从逻辑的角度,withImage调用wtihFile来实现更合理。已修改


public Builder withFile(String fileName, String meta, InputStream inputStream) {
this.fileName = fileName;
this.fileSha256 = fileSha256;
this.fileInputStream = inputStream;

String mimeType = URLConnection.guessContentTypeFromName(fileName);
if (mimeType == null) {
// guess this is a video uploading
this.fileContentType = ContentType.APPLICATION_OCTET_STREAM;
} else {
this.fileContentType = ContentType.create(mimeType);
}
this.meta = meta;
return this;
}

public WechatPayUploadHttpPost build() {
if (fileName == null || fileSha256 == null || fileInputStream == null) {
throw new IllegalArgumentException("缺少待上传图片文件信息");
if (fileName == null || fileName.isEmpty()) {
throw new IllegalArgumentException("文件名称为空");
}

if (uri == null) {
throw new IllegalArgumentException("缺少上传图片接口URL");
if (fileInputStream == null) {
throw new IllegalArgumentException("文件为空");
}
if (fileContentType == null) {
throw new IllegalArgumentException("文件类型为空");
}
if (meta == null || meta.isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些检查还是需要吧,除了fileSha256之外。开发者要是忘记调用withFile()或者withImage()就调用build()是要报错的哦。请再参考标准的builder实现。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在虽然我们的设计meta就意味着有其他参数,但是未来可能放开了呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有道理。之前没有考虑未来放开的场景,已修改。

throw new IllegalArgumentException("媒体文件元信息为空");
}

String meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", fileName, fileSha256);
WechatPayUploadHttpPost request = new WechatPayUploadHttpPost(uri, meta);

MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.RFC6532)
.addBinaryBody("file", fileInputStream, fileContentType, fileName)
.addTextBody("meta", meta, APPLICATION_JSON);

request.setEntity(entityBuilder.build());
request.addHeader(ACCEPT, APPLICATION_JSON.toString());

return request;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,30 @@ public void uploadImageTest() throws Exception {
}
}
}

@Test
public void uploadFileTest() throws Exception {
String filePath = "/your/home/test.png";

URI uri = new URI("https://api.mch.weixin.qq.com/v3/merchant/media/upload");

File file = new File(filePath);
try (FileInputStream fileIs = new FileInputStream(file)) {
String sha256 = DigestUtils.sha256Hex(fileIs);
String meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", file.getName(), sha256);
try (InputStream is = new FileInputStream(file)) {
WechatPayUploadHttpPost request = new WechatPayUploadHttpPost.Builder(uri)
.withFile(file.getName(), meta, is)
.build();
try (CloseableHttpResponse response = httpClient.execute(request)) {
assertEquals(SC_OK, response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
String s = EntityUtils.toString(entity);
System.out.println(s);
}
}
}
}
}