Skip to content

Commit eb5e647

Browse files
committed
Option to provide File via RequestParams and/or SimpleMultipartEntity with different than real name
1 parent 0432ab9 commit eb5e647

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,23 +193,48 @@ public void put(String key, String value) {
193193
* @throws java.io.FileNotFoundException throws if wrong File argument was passed
194194
*/
195195
public void put(String key, File file) throws FileNotFoundException {
196-
put(key, file, null);
196+
put(key, file, null, null);
197197
}
198198

199199
/**
200-
* Adds a file to the request.
200+
* Adds a file to the request with custom provided file name
201+
*
202+
* @param key the key name for the new param.
203+
* @param file the file to add.
204+
* @param customFileName file name to use instead of real file name
205+
* @throws java.io.FileNotFoundException throws if wrong File argument was passed
206+
*/
207+
public void put(String key, String customFileName, File file) throws FileNotFoundException {
208+
put(key, file, null, customFileName);
209+
}
210+
211+
/**
212+
* Adds a file to the request with custom provided file content-type
201213
*
202214
* @param key the key name for the new param.
203215
* @param file the file to add.
204216
* @param contentType the content type of the file, eg. application/json
205217
* @throws java.io.FileNotFoundException throws if wrong File argument was passed
206218
*/
207219
public void put(String key, File file, String contentType) throws FileNotFoundException {
220+
put(key, file, contentType, null);
221+
}
222+
223+
/**
224+
* Adds a file to the request with both custom provided file content-type and file name
225+
*
226+
* @param key the key name for the new param.
227+
* @param file the file to add.
228+
* @param contentType the content type of the file, eg. application/json
229+
* @param customFileName file name to use instead of real file name
230+
* @throws java.io.FileNotFoundException throws if wrong File argument was passed
231+
*/
232+
public void put(String key, File file, String contentType, String customFileName) throws FileNotFoundException {
208233
if (file == null || !file.exists()) {
209234
throw new FileNotFoundException();
210235
}
211236
if (key != null) {
212-
fileParams.put(key, new FileWrapper(file, contentType));
237+
fileParams.put(key, new FileWrapper(file, contentType, customFileName));
213238
}
214239
}
215240

@@ -495,7 +520,7 @@ private HttpEntity createMultipartEntity(ResponseHandlerInterface progressHandle
495520
// Add file params
496521
for (ConcurrentHashMap.Entry<String, FileWrapper> entry : fileParams.entrySet()) {
497522
FileWrapper fileWrapper = entry.getValue();
498-
entity.addPart(entry.getKey(), fileWrapper.file, fileWrapper.contentType);
523+
entity.addPart(entry.getKey(), fileWrapper.file, fileWrapper.contentType, fileWrapper.customFileName);
499524
}
500525

501526
return entity;
@@ -561,10 +586,12 @@ protected String getParamString() {
561586
public static class FileWrapper {
562587
public final File file;
563588
public final String contentType;
589+
public final String customFileName;
564590

565-
public FileWrapper(File file, String contentType) {
591+
public FileWrapper(File file, String contentType, String customFileName) {
566592
this.file = file;
567593
this.contentType = contentType;
594+
this.customFileName = customFileName;
568595
}
569596
}
570597

library/src/main/java/com/loopj/android/http/SimpleMultipartEntity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
package com.loopj.android.http;
2525

26+
import android.text.TextUtils;
2627
import android.util.Log;
2728

2829
import org.apache.http.Header;
@@ -116,6 +117,9 @@ public void addPart(String key, File file) {
116117
public void addPart(String key, File file, String type) {
117118
fileParts.add(new FilePart(key, file, normalizeContentType(type)));
118119
}
120+
public void addPart(String key, File file, String type, String customFileName) {
121+
fileParts.add(new FilePart(key, file, normalizeContentType(type), customFileName));
122+
}
119123

120124
public void addPart(String key, String streamName, InputStream inputStream, String type)
121125
throws IOException {
@@ -172,6 +176,11 @@ private class FilePart {
172176
public File file;
173177
public byte[] header;
174178

179+
public FilePart(String key, File file, String type, String customFileName) {
180+
header = createHeader(key, TextUtils.isEmpty(customFileName) ? file.getName() : customFileName, type);
181+
this.file = file;
182+
}
183+
175184
public FilePart(String key, File file, String type) {
176185
header = createHeader(key, file.getName(), type);
177186
this.file = file;

0 commit comments

Comments
 (0)