@@ -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 }
0 commit comments