Retrofit使用MultipartBody

Posted by phantomVK on July 4, 2019

指定 RequestBody 上传的文件路径

val requestBody = RequestBody.create(MediaType.parse("file/*"), File(filepath))

构建 MultipartBody 消息体

val body = MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart("convertType", type)
        .addFormDataPart("file", fileName, requestBody)
        .build()

发出请求即可

val request = Request.Builder()
        .url(urlString)
        .post(body)
        .build()