使用不同的文件名改进2个多部分文件上载

时间:2016-07-07 02:26:16

标签: android

这是我的代码。它运作正常。但我想上传其他图像类型,如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());
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

尝试这个:

@POST("upload")
fun upload(@BODY parts: MultipartBody): Call<ResponseBody>

和客户端看起来像这样:

val parts = MultipartBody.Builder()
                    .addFormDataPart(name = "name",filename = "yourDynamicFileName", RequestBody.create(...))
                    .build()

api.upload(parts).execute()