7373 * String[] colors = { "blue", "yellow" }; // Ordered collection
7474 * params.put("colors", colors); // url params: "colors[]=blue&colors[]=yellow"
7575 *
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+ *
7679 * List<Map<String, String>> listOfMaps = new ArrayList<Map<String,
7780 * String>>();
7881 * Map<String, String> user1 = new HashMap<String, String>();
@@ -106,14 +109,15 @@ public class RequestParams implements Serializable {
106109 protected final ConcurrentHashMap <String , String > urlParams = new ConcurrentHashMap <String , String >();
107110 protected final ConcurrentHashMap <String , StreamWrapper > streamParams = new ConcurrentHashMap <String , StreamWrapper >();
108111 protected final ConcurrentHashMap <String , FileWrapper > fileParams = new ConcurrentHashMap <String , FileWrapper >();
112+ protected final ConcurrentHashMap <String , List <FileWrapper > >fileArrayParams = new ConcurrentHashMap <String , List <FileWrapper >>();
109113 protected final ConcurrentHashMap <String , Object > urlParamsWithObjects = new ConcurrentHashMap <String , Object >();
110114 protected String contentEncoding = HTTP .UTF_8 ;
111115
112116 /**
113117 * Sets content encoding for return value of {@link #getParamString()} and {@link
114118 * #createFormEntity()} <p> </p> Default encoding is "UTF-8"
115119 *
116- * @param encoding String constant from {@link org.apache.http.protocol. HTTP}
120+ * @param encoding String constant from {@link HTTP}
117121 */
118122 public void setContentEncoding (final String encoding ) {
119123 if (encoding != null ) {
@@ -200,12 +204,47 @@ public void put(String key, String value) {
200204 }
201205 }
202206
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+
203242 /**
204243 * Adds a file to the request.
205244 *
206245 * @param key the key name for the new param.
207246 * @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
209248 */
210249 public void put (String key , File file ) throws FileNotFoundException {
211250 put (key , file , null , null );
@@ -217,7 +256,7 @@ public void put(String key, File file) throws FileNotFoundException {
217256 * @param key the key name for the new param.
218257 * @param file the file to add.
219258 * @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
221260 */
222261 public void put (String key , String customFileName , File file ) throws FileNotFoundException {
223262 put (key , file , null , customFileName );
@@ -229,7 +268,7 @@ public void put(String key, String customFileName, File file) throws FileNotFoun
229268 * @param key the key name for the new param.
230269 * @param file the file to add.
231270 * @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
233272 */
234273 public void put (String key , File file , String contentType ) throws FileNotFoundException {
235274 put (key , file , contentType , null );
@@ -242,7 +281,7 @@ public void put(String key, File file, String contentType) throws FileNotFoundEx
242281 * @param file the file to add.
243282 * @param contentType the content type of the file, eg. application/json
244283 * @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
246285 */
247286 public void put (String key , File file , String contentType , String customFileName ) throws FileNotFoundException {
248287 if (file == null || !file .exists ()) {
@@ -369,6 +408,7 @@ public void remove(String key) {
369408 streamParams .remove (key );
370409 fileParams .remove (key );
371410 urlParamsWithObjects .remove (key );
411+ fileArrayParams .remove (key );
372412 }
373413
374414 /**
@@ -381,7 +421,8 @@ public boolean has(String key) {
381421 return urlParams .get (key ) != null ||
382422 streamParams .get (key ) != null ||
383423 fileParams .get (key ) != null ||
384- urlParamsWithObjects .get (key ) != null ;
424+ urlParamsWithObjects .get (key ) != null ||
425+ fileArrayParams .get (key ) != null ;
385426 }
386427
387428 @ Override
@@ -414,6 +455,15 @@ public String toString() {
414455 result .append ("FILE" );
415456 }
416457
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+
417467 List <BasicNameValuePair > params = getParamsList (null , urlParamsWithObjects );
418468 for (BasicNameValuePair kv : params ) {
419469 if (result .length () > 0 )
@@ -469,7 +519,7 @@ public void setAutoCloseInputStreams(boolean flag) {
469519 public HttpEntity getEntity (ResponseHandlerInterface progressHandler ) throws IOException {
470520 if (useJsonStreamer ) {
471521 return createJsonStreamerEntity (progressHandler );
472- } else if (!forceMultipartEntity && streamParams .isEmpty () && fileParams .isEmpty ()) {
522+ } else if (!forceMultipartEntity && streamParams .isEmpty () && fileParams .isEmpty () && fileArrayParams . isEmpty () ) {
473523 return createFormEntity ();
474524 } else {
475525 return createMultipartEntity (progressHandler );
@@ -553,6 +603,14 @@ private HttpEntity createMultipartEntity(ResponseHandlerInterface progressHandle
553603 entity .addPart (entry .getKey (), fileWrapper .file , fileWrapper .contentType , fileWrapper .customFileName );
554604 }
555605
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+
556614 return entity ;
557615 }
558616
0 commit comments