使用带有Retrofit2的Multipart和JSON键值对上传文件 [英] Upload file using both Multipart and JSON Key value pairs with Retrofit2

查看:524
本文介绍了使用带有Retrofit2的Multipart和JSON键值对上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我们通过用简单的JSON转换String bytes来加载文件(视频,音频,文本等),包括其他一些带有键-值对的值.如下所示:

Currently we are uoloading files (Video, Audio, Text etc..) by converting String bytes with simple JSON including some other values with their Key-Value pairs. Just like below:

某些标头值:

{
    "header": {
        "geoDate": {
            "point": {
                "longitude": 77.56246948242188,
                "latitude": 12.928763389587403
            },
            "date": "2020-02-25T18:26:00Z"
        },
        "version": "1.35.00.001",
        "businessId": "178"
    }
}

和文件信息:

    JSONObject data = new JSONObject();
    data.put("name", params.name);
    data.put("mimeType", params.mimeType);
    data.put("fileSize", params.fileSize);
    data.put("inputData", params.data);

    requestJSON.put("data", data);

此处params.data是字节String bytes = Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT);

它正在工作,但我们希望通过Retrofit来实现这一点,方法是通过MultiPart将文件发送到服务器,这也将提高性能.但是问题在于JSON结构中的问题,服务器无法更改其程序,我们(app)只需执行一些操作即可使用Retrofit Multipart发送文件,包括其他值和键(也为inputData).

It's working but we want to do it through Retrofit by sending the files through MultiPart to the server and which will improve the performance as well. But the problem is as it was in the JSON structure, the server can't change its program and we (app) only have to do something which sends the file using Retrofit Multipart including other values and keys(inputData also).

我正在寻找一种方法来做到这一点.而且我想知道我们是否还可以发送消息,服务器是否需要为API结构进行任何更改,例如当前其接受字节的String,而我们将其更改为inputData的文件.

I am searching for a way to do that. And I am wondering if we are able to send also, is the server has to change anything for the API structure like currently its accepting String for the bytes and we are going to change it to file for inputData.

推荐答案

对我来说很好(这是我的代码,只需将其适应您的业务逻辑即可):

Works fine for me (it's my code, just adapt it to your business logic):

接口:

@Multipart
@POST("{projectName}/log")
Call<LogRp> uploadFile(
        @Path("projectName") String project,
        @PartMap Map<String, RequestBody> mp,
        @Part MultipartBody.Part file
        );

服务:

private MultipartBody.Part buildFilePart(File file, FileType type) {
    return MultipartBody.Part.createFormData("file", file.getName(),
            RequestBody.create(MediaType.parse(type.value.get()), file));
}

private Map<String, RequestBody> buildJsonPart(LogRq logRq) throws JsonProcessingException {
    return Collections.singletonMap("json_request_part", RequestBody.create(
            MediaType.parse("application/json"),
            new ObjectMapper().writeValueAsString(logRq))
    );
}

然后简单地:

client.uploadFile(
                        project,
                        buildJsonPart(logRq),
                        buildFilePart(file, type)
                )

LogRp和LogRq是响应和请求POJO. 如果需要帮助,可以ping通我.

LogRp and LogRq are Response and Request POJOs. ping me if help needed.

这篇关于使用带有Retrofit2的Multipart和JSON键值对上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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