Skip to content

Commit eaf6800

Browse files
committed
Support any input stream in requestparams
1 parent 18629ae commit eaf6800

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/com/loopj/android/http/RequestParams.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
package com.loopj.android.http;
2020

21-
import java.io.ByteArrayInputStream;
21+
import java.io.InputStream;
2222
import java.io.File;
2323
import java.io.UnsupportedEncodingException;
2424
import java.util.LinkedList;
@@ -89,7 +89,7 @@ public void put(String key, String value){
8989
* @param key the key name for the new param.
9090
* @param filedata the file contents to add.
9191
*/
92-
public void put(String key, ByteArrayInputStream filedata) {
92+
public void put(String key, InputStream filedata) {
9393
put(key, filedata, null, null);
9494
}
9595

@@ -99,7 +99,7 @@ public void put(String key, ByteArrayInputStream filedata) {
9999
* @param filedata the file contents to add.
100100
* @param filename the name of the file.
101101
*/
102-
public void put(String key, ByteArrayInputStream filedata, String filename) {
102+
public void put(String key, InputStream filedata, String filename) {
103103
put(key, filedata, filename, null);
104104
}
105105

@@ -110,7 +110,7 @@ public void put(String key, ByteArrayInputStream filedata, String filename) {
110110
* @param filename the name of the file.
111111
* @param contentType the content type of the file, eg. application/json
112112
*/
113-
public void put(String key, ByteArrayInputStream filedata, String filename, String contentType) {
113+
public void put(String key, InputStream filedata, String filename, String contentType) {
114114
if(key != null && filedata != null) {
115115
fileParams.put(key, new FileWrapper(filedata, filename, contentType));
116116
}
@@ -171,16 +171,11 @@ HttpEntity getEntity() {
171171
// Add file params
172172
for(ConcurrentHashMap.Entry<String, FileWrapper> entry : fileParams.entrySet()) {
173173
FileWrapper file = entry.getValue();
174-
if(file.bytes != null) {
175-
String filename = file.filename;
176-
if(filename == null) {
177-
filename = "nofilename";
178-
}
179-
174+
if(file.inputStream != null) {
180175
if(file.contentType != null) {
181-
multipartEntity.addPart(entry.getKey(), filename, file.bytes, file.contentType);
176+
multipartEntity.addPart(entry.getKey(), file.getFileName(), file.inputStream, file.contentType);
182177
} else {
183-
multipartEntity.addPart(entry.getKey(), filename, file.bytes);
178+
multipartEntity.addPart(entry.getKey(), file.getFileName(), file.inputStream);
184179
}
185180
}
186181
}
@@ -213,14 +208,22 @@ private List<BasicNameValuePair> getParamsList() {
213208
}
214209

215210
private static class FileWrapper {
216-
public ByteArrayInputStream bytes;
217-
public String filename;
211+
public InputStream inputStream;
212+
public String fileName;
218213
public String contentType;
219214

220-
public FileWrapper(ByteArrayInputStream bytes, String filename, String contentType) {
221-
this.bytes = bytes;
222-
this.filename = filename;
215+
public FileWrapper(InputStream inputStream, String fileName, String contentType) {
216+
this.inputStream = inputStream;
217+
this.fileName = fileName;
223218
this.contentType = contentType;
224219
}
220+
221+
public String getFileName() {
222+
if(fileName != null) {
223+
return fileName;
224+
} else {
225+
return "nofilename";
226+
}
227+
}
225228
}
226229
}

0 commit comments

Comments
 (0)