这是我的代码。它可以正常工作。但我想上传其他类型的图片,比如png、jpeg等。因此我想改变filename=\"file1.jpeg"。
另外,我想同时发送不同数量的文件。
请帮助我解决这个问题。谢谢。
public interface FileUploadService {
@Multipart
@POST("upload")
Call<ResponseBody> upload(@Part("description") RequestBody description,@Part("file1\"; filename=\"file1.jpeg") RequestBody file1);
}
private void uploadFile() {
FileUploadService service =
ServiceGenerator.createService(FileUploadService.class);
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), new File("/path/to/mypic.jpeg"));
String descriptionString = "hello, this is description speaking";
RequestBody description =
RequestBody.create(
MediaType.parse("multipart/form-data"), descriptionString);
Call<ResponseBody> call = service.upload(description, body);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call,
Response<ResponseBody> response) {
Log.v("Upload", "success");
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("Upload error:", t.getMessage());
}
});
}