使用 Retrofit 2 上传文件

Upload file with Retrofit 2

我试了几天,我真的什么都做了.. 以下是请求在 Postman 中的样子:

我确信所有 GET 参数都正确写入。我想我发送文件上传的方式有问题。

            Map<String, RequestBody> map = new HashMap<>();
            File file = new File("/storage/emulated/0/ConstructSecure/d1940b05-76d1-4d98-b4b4-b04b8247c8cb.png");
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
            String fileName = file.getName();
            map.put("attachment\"; filename=\"" + fileName + "\"", requestBody);

            //GET parameters
            Map<String, String> params = new HashMap<String, String>();
            params.put("inspectionUUID", inspectionUUID);
            params.put("noteUUID", noteUUID);
            params.put("attachmentUUID", attachmentUUID);
            params.put("noteType", noteType);
            params.put("modifiedTime", modifiedTime);

            Call<ResponseBody> call = service.upload(access_token,params,map);
            call.enqueue()....

接口:

@Multipart
    @POST("api/MediaFiles/AddMediaFile")
    Call<ResponseBody> upload(
            @Header("Authorization") String authorization,
            /* GET params */ @QueryMap Map<String, String> params,
            @PartMap Map<String, RequestBody> map
    );

谁能帮帮我?

我花了很多时间搜索如何将文件作为字节流发送,因为网络上的所有答案都解释了通过 RequestBody 上传,但它不适用于我的情况。 所以,这是解决方案:

InputStream in = new FileInputStream(file);
    byte[] buf = new byte[in.available()];
    while (in.read(buf) != -1) ;
    RequestBody requestBodyByte = RequestBody
            .create(MediaType.parse("application/octet-stream"), buf);
    String content_disposition = "attachment; filename=\"" + fileName + "\"";

    //GET parameters
    Map<String, String> params = new HashMap<String, String>();
    params.put("inspectionUUID", inspectionUUID);
    params.put("noteUUID", noteUUID);
    params.put("attachmentUUID", attachmentUUID);
    params.put("noteType", noteType);
    params.put("modifiedTime", modifiedTime);

    Call<ResponseBody> call = service.upload(access_token, content_disposition, requestBodyByte, params);

接口:

 @POST("api/MediaFiles/AddMediaFile")
    Call<ResponseBody> upload(
            @Header("Authorization") String authorization,@Header("Content-Disposition") String content_disposition, @Body RequestBody photo,
            /* GET params */ @QueryMap Map<String, String> params
    );

上传图片请看这里点击这个Link

http://mushtaq.16mb.com/retrofit_example/uploads/

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

class AppConfig {

    private static String BASE_URL = "http://mushtaq.16mb.com/";

    static Retrofit getRetrofit() {

        return new Retrofit.Builder()
                .baseUrl(AppConfig.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
}

========================================================
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;

interface ApiConfig {
    @Multipart
    @POST("retrofit_example/upload_image.php")
    Call<ServerResponse> uploadFile(@Part MultipartBody.Part file,
                                    @Part("file") RequestBody name);

    /*@Multipart
    @POST("ImageUpload")
    Call<ServerResponseKeshav> uploadFile(@Part MultipartBody.Part file,
                                    @Part("file") RequestBody name);*/

    @Multipart
    @POST("retrofit_example/upload_multiple_files.php")
    Call<ServerResponse> uploadMulFile(@Part MultipartBody.Part file1,
                                       @Part MultipartBody.Part file2);
}






https://drive.google.com/open?id=0BzBKpZ4nzNzUMnJfaklVVTJkWEk