Skip to content

Commit 5552406

Browse files
committed
Support File objects directly in RequestParams
1 parent eaf6800 commit 5552406

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import java.io.InputStream;
2222
import java.io.File;
23+
import java.io.FileInputStream;
24+
import java.io.FileNotFoundException;
2325
import java.io.UnsupportedEncodingException;
2426
import java.util.LinkedList;
2527
import java.util.List;
@@ -89,30 +91,39 @@ public void put(String key, String value){
8991
* @param key the key name for the new param.
9092
* @param filedata the file contents to add.
9193
*/
92-
public void put(String key, InputStream filedata) {
93-
put(key, filedata, null, null);
94+
public void put(String key, File file) throws FileNotFoundException {
95+
put(key, new FileInputStream(file), file.getName());
96+
}
97+
98+
/**
99+
* Adds an input stream to the request.
100+
* @param key the key name for the new param.
101+
* @param stream the input stream to add.
102+
*/
103+
public void put(String key, InputStream stream) {
104+
put(key, stream, null);
94105
}
95106

96107
/**
97108
* Adds a file to the request.
98109
* @param key the key name for the new param.
99-
* @param filedata the file contents to add.
100-
* @param filename the name of the file.
110+
* @param stream the input stream to add.
111+
* @param fileName the name of the file.
101112
*/
102-
public void put(String key, InputStream filedata, String filename) {
103-
put(key, filedata, filename, null);
113+
public void put(String key, InputStream stream, String fileName) {
114+
put(key, stream, fileName, null);
104115
}
105116

106117
/**
107118
* Adds a file to the request.
108119
* @param key the key name for the new param.
109-
* @param filedata the file contents to add.
110-
* @param filename the name of the file.
120+
* @param stream the input stream to add.
121+
* @param fileName the name of the file.
111122
* @param contentType the content type of the file, eg. application/json
112123
*/
113-
public void put(String key, InputStream filedata, String filename, String contentType) {
114-
if(key != null && filedata != null) {
115-
fileParams.put(key, new FileWrapper(filedata, filename, contentType));
124+
public void put(String key, InputStream stream, String fileName, String contentType) {
125+
if(key != null && stream != null) {
126+
fileParams.put(key, new FileWrapper(stream, fileName, contentType));
116127
}
117128
}
118129

0 commit comments

Comments
 (0)