From d26576f83c60b6cfee6e88bb422fae092b56404f Mon Sep 17 00:00:00 2001 From: lianup <769025704@qq.com> Date: Wed, 12 Jan 2022 10:06:03 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=8E=BB=E9=99=A4comment=5Fon=5Fpr=20actio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/comment_on_pr.yml | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 .github/workflows/comment_on_pr.yml 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 " From 8ee28b5469e59a456410718f0333fece900d4a99 Mon Sep 17 00:00:00 2001 From: lianup <769025704@qq.com> Date: Wed, 12 Jan 2022 16:46:23 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0uploadFile=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?meta?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../httpclient/WechatPayUploadHttpPost.java | 41 +++++++++++++++---- .../httpclient/CertificatesManagerTest.java | 26 ++++++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) 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..dcc2cea 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 @@ -34,16 +34,49 @@ public static class Builder { private String fileSha256; private InputStream fileInputStream; private ContentType fileContentType; + private String meta; public Builder(URI uri) { this.uri = uri; } public Builder withImage(String fileName, String fileSha256, InputStream inputStream) { + if (fileName == null || fileName.isEmpty()) { + throw new IllegalArgumentException("文件名称为空"); + } + if (fileSha256 == null || fileSha256.isEmpty()) { + throw new IllegalArgumentException("文件摘要为空"); + } + if (inputStream == null) { + throw new IllegalArgumentException("图片文件为空"); + } + this.fileName = fileName; this.fileSha256 = fileSha256; this.fileInputStream = inputStream; + setFileContentType(fileName); + this.meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", fileName, fileSha256); + return this; + } + public Builder withFile(String fileName, String meta, InputStream inputStream) { + if (fileName == null || fileName.isEmpty()) { + throw new IllegalArgumentException("文件名称为空"); + } + if (meta == null) { + throw new IllegalArgumentException("媒体文件元信息为空"); + } + if (inputStream == null) { + throw new IllegalArgumentException("文件为空"); + } + this.fileName = fileName; + this.fileInputStream = inputStream; + setFileContentType(fileName); + this.meta = meta; + return this; + } + + private void setFileContentType(String fileName) { String mimeType = URLConnection.guessContentTypeFromName(fileName); if (mimeType == null) { // guess this is a video uploading @@ -51,29 +84,21 @@ public Builder withImage(String fileName, String fileSha256, InputStream inputSt } else { this.fileContentType = ContentType.create(mimeType); } - return this; } public WechatPayUploadHttpPost build() { - if (fileName == null || fileSha256 == null || fileInputStream == null) { - throw new IllegalArgumentException("缺少待上传图片文件信息"); - } - if (uri == null) { throw new IllegalArgumentException("缺少上传图片接口URL"); } - 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); + } + } + } + } } From 92f1823d2deee29e3818218c6ca08e0c1057f428 Mon Sep 17 00:00:00 2001 From: lianup <769025704@qq.com> Date: Wed, 12 Jan 2022 17:41:57 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=A0=B9=E6=8D=AEreview=E5=BB=BA=E8=AE=AE?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/httpclient/WechatPayUploadHttpPost.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) 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 dcc2cea..b7c2754 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,12 +31,14 @@ 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; } @@ -52,7 +54,6 @@ public Builder withImage(String fileName, String fileSha256, InputStream inputSt } this.fileName = fileName; - this.fileSha256 = fileSha256; this.fileInputStream = inputStream; setFileContentType(fileName); this.meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", fileName, fileSha256); @@ -87,10 +88,6 @@ private void setFileContentType(String fileName) { } public WechatPayUploadHttpPost build() { - if (uri == null) { - throw new IllegalArgumentException("缺少上传图片接口URL"); - } - WechatPayUploadHttpPost request = new WechatPayUploadHttpPost(uri, meta); MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); From 0a029bca476da9caba9001dc3c9d4e97b96014b6 Mon Sep 17 00:00:00 2001 From: lianup <769025704@qq.com> Date: Thu, 13 Jan 2022 11:29:35 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=EF=BC=9B=E5=A2=9E=E5=8A=A0=E5=8F=82=E6=95=B0=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../contrib/apache/httpclient/WechatPayUploadHttpPost.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 b7c2754..0208eda 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 @@ -37,7 +37,7 @@ public static class Builder { public Builder(URI uri) { if (uri == null) { - throw new IllegalArgumentException("缺少上传图片接口URL"); + throw new IllegalArgumentException("上传文件接口URL为空"); } this.uri = uri; } @@ -88,6 +88,9 @@ private void setFileContentType(String fileName) { } public WechatPayUploadHttpPost build() { + if (meta == null || meta.isEmpty()) { + throw new IllegalArgumentException("媒体文件元信息为空"); + } WechatPayUploadHttpPost request = new WechatPayUploadHttpPost(uri, meta); MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); From cbe54be663db8dbc8c3fe112681488a21f1f1c27 Mon Sep 17 00:00:00 2001 From: lianup <769025704@qq.com> Date: Thu, 13 Jan 2022 15:49:51 +0800 Subject: [PATCH 5/6] =?UTF-8?q?withImage=E8=B0=83=E7=94=A8withFile?= =?UTF-8?q?=EF=BC=9B=E5=8E=BB=E9=99=A4setFileContentType?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../httpclient/WechatPayUploadHttpPost.java | 22 ++++--------------- 1 file changed, 4 insertions(+), 18 deletions(-) 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 0208eda..64a0936 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 @@ -43,21 +43,11 @@ public Builder(URI uri) { } public Builder withImage(String fileName, String fileSha256, InputStream inputStream) { - if (fileName == null || fileName.isEmpty()) { - throw new IllegalArgumentException("文件名称为空"); - } if (fileSha256 == null || fileSha256.isEmpty()) { throw new IllegalArgumentException("文件摘要为空"); } - if (inputStream == null) { - throw new IllegalArgumentException("图片文件为空"); - } - - this.fileName = fileName; - this.fileInputStream = inputStream; - setFileContentType(fileName); - this.meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", fileName, fileSha256); - return this; + meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", fileName, fileSha256); + return withFile(fileName, meta, inputStream); } public Builder withFile(String fileName, String meta, InputStream inputStream) { @@ -72,12 +62,6 @@ public Builder withFile(String fileName, String meta, InputStream inputStream) { } this.fileName = fileName; this.fileInputStream = inputStream; - setFileContentType(fileName); - this.meta = meta; - return this; - } - - private void setFileContentType(String fileName) { String mimeType = URLConnection.guessContentTypeFromName(fileName); if (mimeType == null) { // guess this is a video uploading @@ -85,6 +69,8 @@ private void setFileContentType(String fileName) { } else { this.fileContentType = ContentType.create(mimeType); } + this.meta = meta; + return this; } public WechatPayUploadHttpPost build() { From 6db19d79b5aaaaaaf06978a387952fc9a9deca1e Mon Sep 17 00:00:00 2001 From: lianup <769025704@qq.com> Date: Thu, 13 Jan 2022 16:26:32 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../httpclient/WechatPayUploadHttpPost.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) 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 64a0936..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 @@ -51,15 +51,6 @@ public Builder withImage(String fileName, String fileSha256, InputStream inputSt } public Builder withFile(String fileName, String meta, InputStream inputStream) { - if (fileName == null || fileName.isEmpty()) { - throw new IllegalArgumentException("文件名称为空"); - } - if (meta == null) { - throw new IllegalArgumentException("媒体文件元信息为空"); - } - if (inputStream == null) { - throw new IllegalArgumentException("文件为空"); - } this.fileName = fileName; this.fileInputStream = inputStream; String mimeType = URLConnection.guessContentTypeFromName(fileName); @@ -74,11 +65,19 @@ public Builder withFile(String fileName, String meta, InputStream inputStream) { } public WechatPayUploadHttpPost build() { + if (fileName == null || fileName.isEmpty()) { + throw new IllegalArgumentException("文件名称为空"); + } + if (fileInputStream == null) { + throw new IllegalArgumentException("文件为空"); + } + if (fileContentType == null) { + throw new IllegalArgumentException("文件类型为空"); + } if (meta == null || meta.isEmpty()) { throw new IllegalArgumentException("媒体文件元信息为空"); } WechatPayUploadHttpPost request = new WechatPayUploadHttpPost(uri, meta); - MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); entityBuilder.setMode(HttpMultipartMode.RFC6532) .addBinaryBody("file", fileInputStream, fileContentType, fileName)