1515import java .util .Map ;
1616import java .util .concurrent .TimeUnit ;
1717
18- import static com .berry .common .Constants .* ;
18+ import static com .berry .common .Constants .JSON_MIME ;
1919
2020/**
2121 * Title HttpClient
@@ -31,11 +31,11 @@ public class HttpClient {
3131 /**
3232 * 默认 http 客户端
3333 */
34- private static final OkHttpClient CLIENT ;
34+ private static OkHttpClient CLIENT ;
3535 /**
3636 * 连接超时时间 单位秒(默认10s)
3737 */
38- private static final int CONNECT_TIMEOUT = 10 ;
38+ private static final int CONNECT_TIMEOUT = 30 ;
3939 /**
4040 * 回调超时
4141 */
@@ -63,15 +63,12 @@ public class HttpClient {
6363
6464 private static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8" ;
6565
66+ private static Dispatcher dispatcher = new Dispatcher ();
67+ private static ConnectionPool pool = new ConnectionPool (CONNECTION_POOL_MAX_IDLE_COUNT , CONNECTION_POOL_MAX_IDLE_MINUTES , TimeUnit .MINUTES );
6668
6769 static {
68- Dispatcher dispatcher = new Dispatcher ();
6970 dispatcher .setMaxRequests (DISPATCHER_MAX_REQUESTS );
7071 dispatcher .setMaxRequestsPerHost (DISPATCHER_MAX_REQUESTS_PER_HOST );
71-
72- ConnectionPool pool = new ConnectionPool (CONNECTION_POOL_MAX_IDLE_COUNT ,
73- CONNECTION_POOL_MAX_IDLE_MINUTES , TimeUnit .MINUTES );
74-
7572 CLIENT = new OkHttpClient .Builder ()
7673 .callTimeout (CALL_TIMEOUT , TimeUnit .SECONDS )
7774 .connectTimeout (CONNECT_TIMEOUT , TimeUnit .SECONDS )
@@ -81,13 +78,26 @@ public class HttpClient {
8178 .build ();
8279 }
8380
81+ public HttpClient () {
82+ }
83+
84+ public HttpClient (int timeout ) {
85+ CLIENT = new OkHttpClient .Builder ()
86+ .callTimeout (timeout , TimeUnit .SECONDS )
87+ .connectTimeout (timeout , TimeUnit .SECONDS )
88+ .readTimeout (timeout * 3 , TimeUnit .SECONDS )
89+ .dispatcher (dispatcher )
90+ .connectionPool (pool )
91+ .build ();
92+ }
93+
8494 /**
8595 * Get 请求 无参数,无请求头
8696 *
8797 * @param url 地址
8898 * @return 响应
8999 */
90- public static Response get (String url ) {
100+ public Response get (String url ) {
91101 return get (url , null , null );
92102 }
93103
@@ -98,7 +108,7 @@ public static Response get(String url) {
98108 * @param header
99109 * @return
100110 */
101- public static Response get (String url , StringMap header ) {
111+ public Response get (String url , StringMap header ) {
102112 return get (url , null , header );
103113 }
104114
@@ -110,7 +120,7 @@ public static Response get(String url, StringMap header) {
110120 * @param header 请求头 map
111121 * @return 响应
112122 */
113- public static Response get (String url , @ Nullable StringMap params , StringMap header ) {
123+ public Response get (String url , @ Nullable StringMap params , StringMap header ) {
114124 if (params != null ) {
115125 String urlParams = StringUtils .parseUrlParams (params );
116126 url = url + "?" + urlParams ;
@@ -127,7 +137,7 @@ public static Response get(String url, @Nullable StringMap params, StringMap hea
127137 * @param headers 请求头 map
128138 * @return 响应
129139 */
130- public static Response postForm (String url , StringMap params , StringMap headers ) {
140+ public Response postForm (String url , StringMap params , StringMap headers ) {
131141 final FormBody .Builder fb = new FormBody .Builder ();
132142 for (Map .Entry <String , Object > entry : params .entrySet ()) {
133143 fb .add (entry .getKey (), entry .getValue ().toString ());
@@ -138,51 +148,70 @@ public static Response postForm(String url, StringMap params, StringMap headers)
138148 /**
139149 * 请求体为 字符串, 默认媒体类型-JSON
140150 */
141- public static Response post (String url , String body , StringMap header ) {
151+ public Response post (String url , String body , StringMap header ) {
142152 return post (url , StringUtils .utf8Bytes (body ), header , JSON_MIME );
143153 }
144154
155+ /**
156+ * 复杂Map(包含字节数组)对象 以 json 格式请求,
157+ */
158+ public Response postComplex (String url , StringMap params , StringMap header ) {
159+ Gson gson = new GsonBuilder ().enableComplexMapKeySerialization ().create ();
160+ RequestBody requestBody = RequestBody .create (MediaType .get (APPLICATION_JSON_UTF8_VALUE ), gson .toJson (params .map ()));
161+ return post (url , requestBody , header );
162+ }
163+
145164 /**
146165 * 请求体为 字节数组,默认媒体类型-JSON
147166 */
148- public static Response post (String url , byte [] body , StringMap header ) {
167+ public Response post (String url , byte [] body , StringMap header ) {
149168 return post (url , body , header , JSON_MIME );
150169 }
151170
152171 /**
153172 * 请求体为 字节数组,指定 媒体类型
154173 */
155- public static Response post (String url , byte [] body , StringMap header , String contentType ) {
174+ public Response post (String url , byte [] body , StringMap header , String contentType ) {
156175 RequestBody requestBody = RequestBody .create (MediaType .parse (contentType ), body );
157176 return post (url , requestBody , header );
158177 }
159178
160179 /**
161- * 文件上传 文件体为 字节数组
180+ * 批量文件上传
162181 */
163- public static Response multipartPost (String url ,
164- StringMap fields ,
165- String name ,
166- String fileName ,
167- byte [] fileBody ,
168- String mimeType ,
169- StringMap headers ) {
170- RequestBody file = RequestBody .create (MediaType .parse (mimeType ), fileBody );
171- return multipartPost (url , fields , name , fileName , file , headers );
182+ public Response multipartPost (String url ,
183+ StringMap fields ,
184+ String name ,
185+ File [] files ,
186+ StringMap headers ) {
187+ final MultipartBody .Builder mb = new MultipartBody .Builder ();
188+ for (File file : files ) {
189+ RequestBody fileBody = RequestBody .create (MediaType .parse (Constants .MULTIPART_MIME ), file );
190+ mb .addFormDataPart (name , file .getName (), fileBody );
191+ }
192+ if (fields != null ) {
193+ for (Map .Entry <String , Object > entry : fields .entrySet ()) {
194+ mb .addFormDataPart (entry .getKey (), entry .getValue ().toString ());
195+ }
196+ }
197+ mb .setType (MediaType .get ("multipart/form-data" ));
198+ RequestBody body = mb .build ();
199+ Request .Builder requestBuilder = new Request .Builder ().url (url ).post (body );
200+ return send (requestBuilder , headers );
172201 }
173202
174203 /**
175204 * 文件上传 文件体为 file
176205 */
177- public static Response multipartPost (String url ,
178- StringMap fields ,
179- String name ,
180- String fileName ,
181- File fileBody ,
182- String mimeType ,
183- StringMap headers ) {
184- RequestBody file = RequestBody . create ( MediaType . parse ( mimeType ), fileBody );
185- return multipartPost ( url , fields , name , fileName , file , headers );
206+ public Response multipartPost (String url ,
207+ StringMap fields ,
208+ String name ,
209+ String fileName ,
210+ File fileBody ,
211+ StringMap headers ) {
212+ RequestBody file = RequestBody . create ( MediaType . parse ( Constants . MULTIPART_MIME ), fileBody );
213+ Request . Builder requestBuilder = getBuilder ( url , fields , name , fileName , file );
214+ return send ( requestBuilder , headers );
186215 }
187216
188217
@@ -197,7 +226,7 @@ public static Response multipartPost(String url,
197226 * @param contentType 请求体类型
198227 * @param cb 异步回调
199228 */
200- public static void asyncPost (String url , byte [] body , int offset , int size , StringMap header , String contentType , AsyncCallback cb ) {
229+ public void asyncPost (String url , byte [] body , int offset , int size , StringMap header , String contentType , AsyncCallback cb ) {
201230 RequestBody requestBody = RequestBody .create (MediaType .parse (contentType ), body , offset , size );
202231 Request .Builder requestBuilder = new Request .Builder ().url (url ).post (requestBody );
203232 asyncSend (requestBuilder , header , cb );
@@ -206,30 +235,28 @@ public static void asyncPost(String url, byte[] body, int offset, int size, Stri
206235 /**
207236 * 异步文件上传 文件体为 字节数组
208237 */
209- public static void asyncMultipartPost (String url ,
210- StringMap fields ,
211- String name ,
212- String fileName ,
213- byte [] fileBody ,
214- String mimeType ,
215- StringMap headers ,
216- AsyncCallback cb ) {
217- RequestBody file = RequestBody .create (MediaType .parse (mimeType ), fileBody );
238+ public void asyncMultipartPost (String url ,
239+ StringMap fields ,
240+ String name ,
241+ String fileName ,
242+ byte [] fileBody ,
243+ StringMap headers ,
244+ AsyncCallback cb ) {
245+ RequestBody file = RequestBody .create (MediaType .parse (Constants .MULTIPART_MIME ), fileBody );
218246 asyncMultipartPost (url , fields , name , fileName , file , headers , cb );
219247 }
220248
221249 /**
222250 * 异步文件上传 文件体为 file
223251 */
224- public static void asyncMultipartPost (String url ,
225- StringMap fields ,
226- String name ,
227- String fileName ,
228- File fileBody ,
229- String mimeType ,
230- StringMap headers ,
231- AsyncCallback cb ) {
232- RequestBody file = RequestBody .create (MediaType .parse (mimeType ), fileBody );
252+ public void asyncMultipartPost (String url ,
253+ StringMap fields ,
254+ String name ,
255+ String fileName ,
256+ File fileBody ,
257+ StringMap headers ,
258+ AsyncCallback cb ) {
259+ RequestBody file = RequestBody .create (MediaType .parse (Constants .MULTIPART_MIME ), fileBody );
233260 asyncMultipartPost (url , fields , name , fileName , file , headers , cb );
234261 }
235262
@@ -271,26 +298,6 @@ private static Response post(String url, RequestBody body, StringMap header) {
271298 return send (requestBuilder , header );
272299 }
273300
274- /**
275- * 文件上传
276- *
277- * @param url 地址
278- * @param fields 字段信息 data part
279- * @param name 文件接受字段名
280- * @param fileName 文件名 可为空
281- * @param file 已包装的文件请求体
282- * @param headers 请求头 可为空
283- * @return 响应
284- */
285- private static Response multipartPost (String url ,
286- StringMap fields ,
287- String name ,
288- @ Nullable String fileName ,
289- RequestBody file ,
290- StringMap headers ) {
291- Request .Builder requestBuilder = getBuilder (url , fields , name , fileName , file );
292- return send (requestBuilder , headers );
293- }
294301
295302 /**
296303 * 后去文件上传类型 build
@@ -305,7 +312,6 @@ private static Response multipartPost(String url,
305312 private static Request .Builder getBuilder (String url , StringMap fields , String name , @ Nullable String fileName , RequestBody file ) {
306313 final MultipartBody .Builder mb = new MultipartBody .Builder ();
307314 mb .addFormDataPart (name , fileName , file );
308-
309315 if (fields != null ) {
310316 for (Map .Entry <String , Object > entry : fields .entrySet ()) {
311317 mb .addFormDataPart (entry .getKey (), entry .getValue ().toString ());
@@ -334,7 +340,11 @@ private static Response send(final Request.Builder requestBuilder, @Nullable Str
334340 try {
335341 response = CLIENT .newCall (requestBuilder .build ()).execute ();
336342 if (!response .isSuccessful ()) {
337- throw new OssException (response .code (), response .message ());
343+ int code = response .code ();
344+ String resMsg = response .body () != null ? response .body ().string () : "" ;
345+ String msg = response .message () + "," + resMsg ;
346+ response .close ();
347+ throw new OssException (code , msg );
338348 }
339349 } catch (IOException e ) {
340350 e .printStackTrace ();
0 commit comments