73
73
* String[] colors = { "blue", "yellow" }; // Ordered collection
74
74
* params.put("colors", colors); // url params: "colors[]=blue&colors[]=yellow"
75
75
*
76
+ * File[] files = { new File("pic.jpg"), new File("pic1.jpg") }; // Ordered collection
77
+ * params.put("files", files); // url params: "files[]=pic.jpg&files[]=pic1.jpg"
78
+ *
76
79
* List<Map<String, String>> listOfMaps = new ArrayList<Map<String,
77
80
* String>>();
78
81
* Map<String, String> user1 = new HashMap<String, String>();
@@ -106,14 +109,15 @@ public class RequestParams implements Serializable {
106
109
protected final ConcurrentHashMap <String , String > urlParams = new ConcurrentHashMap <String , String >();
107
110
protected final ConcurrentHashMap <String , StreamWrapper > streamParams = new ConcurrentHashMap <String , StreamWrapper >();
108
111
protected final ConcurrentHashMap <String , FileWrapper > fileParams = new ConcurrentHashMap <String , FileWrapper >();
112
+ protected final ConcurrentHashMap <String , List <FileWrapper > >fileArrayParams = new ConcurrentHashMap <String , List <FileWrapper >>();
109
113
protected final ConcurrentHashMap <String , Object > urlParamsWithObjects = new ConcurrentHashMap <String , Object >();
110
114
protected String contentEncoding = HTTP .UTF_8 ;
111
115
112
116
/**
113
117
* Sets content encoding for return value of {@link #getParamString()} and {@link
114
118
* #createFormEntity()} <p> </p> Default encoding is "UTF-8"
115
119
*
116
- * @param encoding String constant from {@link org.apache.http.protocol. HTTP}
120
+ * @param encoding String constant from {@link HTTP}
117
121
*/
118
122
public void setContentEncoding (final String encoding ) {
119
123
if (encoding != null ) {
@@ -200,12 +204,47 @@ public void put(String key, String value) {
200
204
}
201
205
}
202
206
207
+ /**
208
+ * Adds files array to the request.
209
+ *
210
+ * @param key the key name for the new param.
211
+ * @param files the files array to add.
212
+ * @throws FileNotFoundException
213
+ */
214
+ public void put (String key , File files []) throws FileNotFoundException {
215
+ put (key , files , null , null );
216
+ }
217
+
218
+ /**
219
+ *
220
+ * Adds files array to the request with both custom provided file content-type and files name
221
+ *
222
+ * @param key the key name for the new param.
223
+ * @param files the files array to add.
224
+ * @param contentType the content type of the file, eg. application/json
225
+ * @param customFileName file name to use instead of real file name
226
+ * @throws FileNotFoundException throws if wrong File argument was passed
227
+ */
228
+ public void put (String key , File files [], String contentType , String customFileName ) throws FileNotFoundException {
229
+
230
+ if (key != null ){
231
+ List <FileWrapper > fileWrappers = new ArrayList <FileWrapper >();
232
+ for (int i =0 ;i <files .length ;i ++){
233
+ if (files [i ] == null || !files [i ].exists ()){
234
+ throw new FileNotFoundException ();
235
+ }
236
+ fileWrappers .add (new FileWrapper (files [i ], contentType , customFileName ));
237
+ }
238
+ fileArrayParams .put (key ,fileWrappers );
239
+ }
240
+ }
241
+
203
242
/**
204
243
* Adds a file to the request.
205
244
*
206
245
* @param key the key name for the new param.
207
246
* @param file the file to add.
208
- * @throws java.io. FileNotFoundException throws if wrong File argument was passed
247
+ * @throws FileNotFoundException throws if wrong File argument was passed
209
248
*/
210
249
public void put (String key , File file ) throws FileNotFoundException {
211
250
put (key , file , null , null );
@@ -217,7 +256,7 @@ public void put(String key, File file) throws FileNotFoundException {
217
256
* @param key the key name for the new param.
218
257
* @param file the file to add.
219
258
* @param customFileName file name to use instead of real file name
220
- * @throws java.io. FileNotFoundException throws if wrong File argument was passed
259
+ * @throws FileNotFoundException throws if wrong File argument was passed
221
260
*/
222
261
public void put (String key , String customFileName , File file ) throws FileNotFoundException {
223
262
put (key , file , null , customFileName );
@@ -229,7 +268,7 @@ public void put(String key, String customFileName, File file) throws FileNotFoun
229
268
* @param key the key name for the new param.
230
269
* @param file the file to add.
231
270
* @param contentType the content type of the file, eg. application/json
232
- * @throws java.io. FileNotFoundException throws if wrong File argument was passed
271
+ * @throws FileNotFoundException throws if wrong File argument was passed
233
272
*/
234
273
public void put (String key , File file , String contentType ) throws FileNotFoundException {
235
274
put (key , file , contentType , null );
@@ -242,7 +281,7 @@ public void put(String key, File file, String contentType) throws FileNotFoundEx
242
281
* @param file the file to add.
243
282
* @param contentType the content type of the file, eg. application/json
244
283
* @param customFileName file name to use instead of real file name
245
- * @throws java.io. FileNotFoundException throws if wrong File argument was passed
284
+ * @throws FileNotFoundException throws if wrong File argument was passed
246
285
*/
247
286
public void put (String key , File file , String contentType , String customFileName ) throws FileNotFoundException {
248
287
if (file == null || !file .exists ()) {
@@ -369,6 +408,7 @@ public void remove(String key) {
369
408
streamParams .remove (key );
370
409
fileParams .remove (key );
371
410
urlParamsWithObjects .remove (key );
411
+ fileArrayParams .remove (key );
372
412
}
373
413
374
414
/**
@@ -381,7 +421,8 @@ public boolean has(String key) {
381
421
return urlParams .get (key ) != null ||
382
422
streamParams .get (key ) != null ||
383
423
fileParams .get (key ) != null ||
384
- urlParamsWithObjects .get (key ) != null ;
424
+ urlParamsWithObjects .get (key ) != null ||
425
+ fileArrayParams .get (key ) != null ;
385
426
}
386
427
387
428
@ Override
@@ -414,6 +455,15 @@ public String toString() {
414
455
result .append ("FILE" );
415
456
}
416
457
458
+ for (ConcurrentHashMap .Entry <String , List <FileWrapper >> entry : fileArrayParams .entrySet ()) {
459
+ if (result .length () > 0 )
460
+ result .append ("&" );
461
+
462
+ result .append (entry .getKey ());
463
+ result .append ("=" );
464
+ result .append ("FILE" );
465
+ }
466
+
417
467
List <BasicNameValuePair > params = getParamsList (null , urlParamsWithObjects );
418
468
for (BasicNameValuePair kv : params ) {
419
469
if (result .length () > 0 )
@@ -469,7 +519,7 @@ public void setAutoCloseInputStreams(boolean flag) {
469
519
public HttpEntity getEntity (ResponseHandlerInterface progressHandler ) throws IOException {
470
520
if (useJsonStreamer ) {
471
521
return createJsonStreamerEntity (progressHandler );
472
- } else if (!forceMultipartEntity && streamParams .isEmpty () && fileParams .isEmpty ()) {
522
+ } else if (!forceMultipartEntity && streamParams .isEmpty () && fileParams .isEmpty () && fileArrayParams . isEmpty () ) {
473
523
return createFormEntity ();
474
524
} else {
475
525
return createMultipartEntity (progressHandler );
@@ -553,6 +603,14 @@ private HttpEntity createMultipartEntity(ResponseHandlerInterface progressHandle
553
603
entity .addPart (entry .getKey (), fileWrapper .file , fileWrapper .contentType , fileWrapper .customFileName );
554
604
}
555
605
606
+ // Add file collection
607
+ for (ConcurrentHashMap .Entry <String , List <FileWrapper >> entry : fileArrayParams .entrySet ()) {
608
+ List <FileWrapper > fileWrapper = entry .getValue ();
609
+ for (FileWrapper fw :fileWrapper ){
610
+ entity .addPart (entry .getKey (), fw .file , fw .contentType , fw .customFileName );
611
+ }
612
+ }
613
+
556
614
return entity ;
557
615
}
558
616
0 commit comments