上传单个图像似乎对改造2没有问题.
但是,我无法弄清楚如何同时上传2张图片.
如果遵循文档:http: //square.github.io/retrofit/2.x/retrofit/retrofit2/http/PartMap.html
File file = new File(path, "theimage");
File file2 = new File(path2, "theimage");
RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
RequestBody requestBody2 = RequestBody.create(MediaType.parse("image/png"), file2);
Map<String, RequestBody> params = new HashMap<>();
params.put("image2", requestBody2 );
Call<ResponseBody> call = service.upload(requestBody, params);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
Log.v("Upload", "success");
}
Run Code Online (Sandbox Code Playgroud)
接口:
public interface FileUploadService {
@Multipart
@POST("/upload")
Call<ResponseBody> upload(
//@Part("image_logo\"; filename=\"image.png\" ") RequestBody file,
@Part("file") RequestBody file,
@PartMap Map<String, RequestBody> params
// @Part("description") String description
);
Run Code Online (Sandbox Code Playgroud)
这给了'上传:成功',但在服务器端我得到了胡言乱语:
CONTENT_TYPE:multipart/form-data; 边界= 50fbfeb3-3abc-4f15-B130-cdcb7e3a0e4f
内容发布:数组([文件] => PNGIHDRL alotofbinarygibberish .... ... snip [file2] => PNGIHDRL更多二进制乱码...
任何人都能指出我正确的方向吗?
单上传确实有效,这不是问题,我正在尝试上传2个或更多图像.
如果我把它改成这个:
HashMap<String, RequestBody> partMap = new HashMap<String, RequestBody>();
partMap.put("file\"; filename=\"" + file.getName(), requestBody);
partMap.put("file\"; filename=\"" + file2.getName(), requestBody);
Call<ResponseBody> call = service.upload(partMap);
Run Code Online (Sandbox Code Playgroud)
@Multipart
@POST("/upload")
Call<ResponseBody> upload(
@PartMap() Map<String, RequestBody> partMap,
Run Code Online (Sandbox Code Playgroud)
我没有乱码,但只上传了第二张图片......!
我试过这个Retrofit(2.0 beta2)Multipart文件上传不起作用的解决方案,但得到一个错误@body不能与multipart一起使用:Java.lang.IllegalArgumentException:@Body参数不能与表单或多部分编码一起使用.(参数#1)
for (String key : keys) {
Bitmap bm = selectedImages.get(key);
File f = new File(saveToInternalStorage(bm, key), key);
if (f.exists()) {
buildernew.addFormDataPart(key, key + ".png", RequestBody.create(MEDIA_TYPE, f));
}
}
RequestBody requestBody = buildernew.build();
Run Code Online (Sandbox Code Playgroud)
-
Call<ResponseBody> upload(
@Body RequestBody requestBody
Run Code Online (Sandbox Code Playgroud)
final MediaType MEDIA_TYPE=MediaType.parse("image/png");
HashMap<String,RequestBody> map=new HashMap<>(selectedImages.size());
RequestBody file=null;
File f=null;
Set<String> keys = selectedImages.keySet();
for (String key : keys) {
try {
Bitmap bitmap = selectedImages.get(key);
f = new File(saveToInternalStorage(bitmap, key), key);
FileOutputStream fos = new FileOutputStream(f);
if(bitmap!=null){
bitmap.compress(Bitmap.CompressFormat.PNG, 0 , fos);
fos.flush();
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
return;
}
file=RequestBody.create(MEDIA_TYPE, f);
map.put(""+key+"\"; filename=\""+key+".jpg",file);
Log.i("##MYLOG###", "### MAP PUT:" + key + " filename:"+key+".jpg file:" + file.toString() +" type:"+ file.contentType() );
file=null;
f = null;
}
Run Code Online (Sandbox Code Playgroud)
-
Call<ResponseBody> upload(
@PartMap() Map<String,RequestBody> mapFileAndName //for sending multiple images
Run Code Online (Sandbox Code Playgroud)
-
注意:在使用httpClient.interceptors()进行调试时,我只看到一次上传,但在检查端点本身以查看它实际得到的内容时,它会获得多次上传!