diff --git a/.github/workflows/comment_on_pr.yml b/.github/workflows/comment_on_pr.yml deleted file mode 100644 index 23ea40c..0000000 --- a/.github/workflows/comment_on_pr.yml +++ /dev/null @@ -1,19 +0,0 @@ -name: Comment on Pull Request - -on: - pull_request: - -jobs: - # pr时自动创建待完成任务 - comment-on-pr: - runs-on: ubuntu-latest - permissions: - pull-requests: write - steps: - - uses: actions/checkout@v2 - - name: comment PR - uses: unsplash/comment-on-pr@v1.3.0 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - msg: "请完成以下事项:\n - [ ] 更新build.gradle中的version " diff --git a/src/main/java/com/wechat/pay/contrib/apache/httpclient/WechatPayUploadHttpPost.java b/src/main/java/com/wechat/pay/contrib/apache/httpclient/WechatPayUploadHttpPost.java index 52b1ee7..6b1e5e3 100644 --- a/src/main/java/com/wechat/pay/contrib/apache/httpclient/WechatPayUploadHttpPost.java +++ b/src/main/java/com/wechat/pay/contrib/apache/httpclient/WechatPayUploadHttpPost.java @@ -31,19 +31,28 @@ 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); + } + + 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 @@ -51,29 +60,30 @@ public Builder withImage(String fileName, String fileSha256, InputStream inputSt } 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()) { + 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; } } diff --git a/src/test/java/com/wechat/pay/contrib/apache/httpclient/CertificatesManagerTest.java b/src/test/java/com/wechat/pay/contrib/apache/httpclient/CertificatesManagerTest.java index 41a6916..ea8ec37 100644 --- a/src/test/java/com/wechat/pay/contrib/apache/httpclient/CertificatesManagerTest.java +++ b/src/test/java/com/wechat/pay/contrib/apache/httpclient/CertificatesManagerTest.java @@ -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); + } + } + } + } }