25
25
import org .apache .http .protocol .HTTP ;
26
26
27
27
import java .io .File ;
28
- import java .io .FileInputStream ;
29
28
import java .io .FileNotFoundException ;
29
+ import java .io .IOException ;
30
30
import java .io .InputStream ;
31
31
import java .io .UnsupportedEncodingException ;
32
32
import java .util .ArrayList ;
56
56
*/
57
57
public class RequestParams {
58
58
59
+ private static final String TAG = "RequestParams" ;
60
+
59
61
protected ConcurrentHashMap <String , String > urlParams ;
62
+ protected ConcurrentHashMap <String , StreamWrapper > streamParams ;
60
63
protected ConcurrentHashMap <String , FileWrapper > fileParams ;
61
64
protected ConcurrentHashMap <String , ArrayList <String >> urlParamsWithArray ;
62
65
@@ -128,7 +131,20 @@ public void put(String key, String value){
128
131
* @param file the file to add.
129
132
*/
130
133
public void put (String key , File file ) throws FileNotFoundException {
131
- put (key , new FileInputStream (file ), file .getName ());
134
+ put (key , file , null );
135
+ }
136
+
137
+ /**
138
+ * Adds a file to the request.
139
+ *
140
+ * @param key the key name for the new param.
141
+ * @param file the file to add.
142
+ * @param contentType the content type of the file, eg. application/json
143
+ */
144
+ public void put (String key , File file , String contentType ) throws FileNotFoundException {
145
+ if (key != null && file != null ) {
146
+ fileParams .put (key , new FileWrapper (file , contentType ));
147
+ }
132
148
}
133
149
134
150
/**
@@ -155,22 +171,22 @@ public void put(String key, InputStream stream) {
155
171
* Adds an input stream to the request.
156
172
* @param key the key name for the new param.
157
173
* @param stream the input stream to add.
158
- * @param fileName the name of the file .
174
+ * @param name the name of the stream .
159
175
*/
160
- public void put (String key , InputStream stream , String fileName ) {
161
- put (key , stream , fileName , null );
176
+ public void put (String key , InputStream stream , String name ) {
177
+ put (key , stream , name , null );
162
178
}
163
179
164
180
/**
165
181
* Adds an input stream to the request.
166
182
* @param key the key name for the new param.
167
183
* @param stream the input stream to add.
168
- * @param fileName the name of the file .
184
+ * @param name the name of the stream .
169
185
* @param contentType the content type of the file, eg. application/json
170
186
*/
171
- public void put (String key , InputStream stream , String fileName , String contentType ) {
187
+ public void put (String key , InputStream stream , String name , String contentType ) {
172
188
if (key != null && stream != null ) {
173
- fileParams .put (key , new FileWrapper (stream , fileName , contentType ));
189
+ streamParams .put (key , new StreamWrapper (stream , name , contentType ));
174
190
}
175
191
}
176
192
@@ -180,6 +196,7 @@ public void put(String key, InputStream stream, String fileName, String contentT
180
196
*/
181
197
public void remove (String key ){
182
198
urlParams .remove (key );
199
+ streamParams .remove (key );
183
200
fileParams .remove (key );
184
201
urlParamsWithArray .remove (key );
185
202
}
@@ -196,10 +213,19 @@ public String toString() {
196
213
result .append (entry .getValue ());
197
214
}
198
215
199
- for (ConcurrentHashMap .Entry <String , FileWrapper > entry : fileParams .entrySet ()) {
216
+ for (ConcurrentHashMap .Entry <String , StreamWrapper > entry : streamParams .entrySet ()) {
200
217
if (result .length () > 0 )
201
218
result .append ("&" );
202
219
220
+ result .append (entry .getKey ());
221
+ result .append ("=" );
222
+ result .append ("STREAM" );
223
+ }
224
+
225
+ for (ConcurrentHashMap .Entry <String , FileWrapper > entry : fileParams .entrySet ()) {
226
+ if (result .length () > 0 )
227
+ result .append ("&" );
228
+
203
229
result .append (entry .getKey ());
204
230
result .append ("=" );
205
231
result .append ("FILE" );
@@ -222,14 +248,16 @@ public String toString() {
222
248
return result .toString ();
223
249
}
224
250
225
- /**
251
+ /**
226
252
* Returns an HttpEntity containing all request parameters
253
+ *
254
+ * @throws IOException if one of the streams cannot be read
227
255
*/
228
- public HttpEntity getEntity () {
229
- if (!fileParams .isEmpty ()) {
230
- return createMultipartEntity ();
231
- } else {
256
+ public HttpEntity getEntity () throws IOException {
257
+ if (streamParams .isEmpty () && fileParams .isEmpty ()) {
232
258
return createFormEntity ();
259
+ } else {
260
+ return createMultipartEntity ();
233
261
}
234
262
}
235
263
@@ -241,7 +269,7 @@ private HttpEntity createFormEntity() {
241
269
}
242
270
}
243
271
244
- private HttpEntity createMultipartEntity () {
272
+ private HttpEntity createMultipartEntity () throws IOException {
245
273
SimpleMultipartEntity entity = new SimpleMultipartEntity ();
246
274
247
275
// Add string params
@@ -258,25 +286,27 @@ private HttpEntity createMultipartEntity() {
258
286
}
259
287
}
260
288
289
+ // Add stream params
290
+ for (ConcurrentHashMap .Entry <String , StreamWrapper > entry : streamParams .entrySet ()) {
291
+ StreamWrapper stream = entry .getValue ();
292
+ if (stream .inputStream != null ) {
293
+ entity .addPart (entry .getKey (), stream .name , stream .inputStream ,
294
+ stream .contentType );
295
+ }
296
+ }
297
+
261
298
// Add file params
262
299
for (ConcurrentHashMap .Entry <String , FileWrapper > entry : fileParams .entrySet ()) {
263
- FileWrapper file = entry .getValue ();
264
- if (file .inputStream != null ) {
265
- if (file .contentType != null ) {
266
- entity .addPart (entry .getKey (), file .getFileName (),
267
- file .inputStream , file .contentType );
268
- } else {
269
- entity .addPart (entry .getKey (), file .getFileName (),
270
- file .inputStream );
271
- }
272
- }
300
+ FileWrapper fileWrapper = entry .getValue ();
301
+ entity .addPart (entry .getKey (), fileWrapper .file , fileWrapper .contentType );
273
302
}
274
303
275
304
return entity ;
276
305
}
277
306
278
307
private void init (){
279
308
urlParams = new ConcurrentHashMap <String , String >();
309
+ streamParams = new ConcurrentHashMap <String , StreamWrapper >();
280
310
fileParams = new ConcurrentHashMap <String , FileWrapper >();
281
311
urlParamsWithArray = new ConcurrentHashMap <String , ArrayList <String >>();
282
312
}
@@ -303,22 +333,24 @@ protected String getParamString() {
303
333
}
304
334
305
335
private static class FileWrapper {
306
- public InputStream inputStream ;
307
- public String fileName ;
336
+ public File file ;
308
337
public String contentType ;
309
338
310
- public FileWrapper (InputStream inputStream , String fileName , String contentType ) {
311
- this .inputStream = inputStream ;
312
- this .fileName = fileName ;
339
+ public FileWrapper (File file , String contentType ) {
340
+ this .file = file ;
313
341
this .contentType = contentType ;
314
342
}
343
+ }
315
344
316
- public String getFileName () {
317
- if (fileName != null ) {
318
- return fileName ;
319
- } else {
320
- return "nofilename" ;
321
- }
345
+ private static class StreamWrapper {
346
+ public InputStream inputStream ;
347
+ public String name ;
348
+ public String contentType ;
349
+
350
+ public StreamWrapper (InputStream inputStream , String name , String contentType ) {
351
+ this .inputStream = inputStream ;
352
+ this .name = name ;
353
+ this .contentType = contentType ;
322
354
}
323
355
}
324
356
}
0 commit comments