上传文件与改造2 [英] Upload file with Retrofit 2

查看:163
本文介绍了上传文件与改造2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做几天,我真的做了所有的事情。
以下是Postman中的请求:





我相信所有的GET参数都能正确写入。我想如何发送文件上传的问题。

 映射< 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参数
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 )
调用< ResponseBody>上传(
@Header(Authorization)字符串授权,
/ * GET params * / @QueryMap Map< String,String> params,
@ PartMap地图< Strin g,RequestBody>地图
);

任何人都可以帮忙吗?

解决方案我花了大量的时间进行搜索,如何发送文件作为字节流,因为所有的答案在Web解释上传通过RequestBody,但它不适用于我的情况。
所以,这里是解决方案:

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

// GET参数
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);

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

接口:

  @POST(api / MediaFiles / AddMediaFile)
呼叫< ResponseBody>上传(
@Header(Authorization)字符串授权,@ Header(Content-Disposition)字符串content_disposition,@Body RequestBody photo,
/ * GET params * / @QueryMap Map< String,String> ; params
);


I trying to do that a few days, and I really did everything.. Here is how request looks in Postman:

I am sure, that all GET parameters were writing correctly. The problem in how I sending a file to upload, I suppose.

            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()....

Interface:

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

Could anyone help me ?

解决方案

I spent a lot of time for search, how to send file as byte-stream, because all answers in web explain upload via RequestBody, but it's not working for my case. So, here is the solution:

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);

Interface:

 @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
    );

这篇关于上传文件与改造2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆