Android Retrofit Multipart Image Upload 500错误

Andrioid Retrofit Multipart Image Upload 500 error

我尝试使用改造上传图片时遇到 500 错误。我将图像发送为 multipart/formdata。在邮递员中提供 201Post man screen shot,但我们的应用程序收到 500 错误。谁能帮我解决这个问题。 这是我的代码

  fun uploadImage(filepath: String) {
        Log.e("info", ">>>>>status>>>>>" + ">>>>>>" + filepath)
        val file = File(filepath)
        Log.d("file...", "length..." + file.length())

        val mimeType = getMimeType(file)
        val metaobject = JSONObject()
        metaobject.put("eye", "OD")
        // for image
        val requestBody: RequestBody = MultipartBody.Builder().setType(MultipartBody.FORM)
            .addFormDataPart("file", file.name, file.asRequestBody(mimeType!!.toMediaTypeOrNull())).build()
//        val requestBody: RequestBody = RequestBody.create(MultipartBody.FORM,file)

        val body: MultipartBody.Part = MultipartBody.Part.createFormData("file", file.name, requestBody)
        // for meta data
        val metabody: RequestBody = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart(
                "metadata", metaobject.toString()).build()

        val header = HashMap<String, String>()
      //  header["Content-Type"] = "multipart/form-data; boundary=--------------------//-941843893261340198843616"
   //     header["Content-Length"] = file.length().toString()
        header["Authorization"] =
            "Bearer <<token>>"
        val request = ApiService.buildService(ApiHelper::class.java)
        val call = request.sendMediaFile(header,body, metabody)
        call.enqueue(
            object : Callback<ResponseBody?> {
                override fun onResponse(
                    call: Call<ResponseBody?>,
                    response: Response<ResponseBody?>
                ) {
                    Log.e("Upload", "success" + response.body().toString())
                    Log.e("Upload", "success" + response.errorBody().toString())

                }

                override fun onFailure(call: Call<ResponseBody?>, t: Throwable) {
                    Log.e("Upload error:", t.message!!)
                }
            })
    }

和API帮手class

@Multipart
    @POST("files")
    fun sendMediaFile(
        @HeaderMap headers: Map<String, String>,
        @Part image: MultipartBody.Part,
        @Part("metadata") metadata: RequestBody?
    ): Call<ResponseBody?>

我也遇到过这个问题,我是这样解决的。也许它也适合你。将带有“application/json”值的“接受”键添加到请求的 Header 中。

我已经解决了问题。我已将两个参数都发布在单个请求正文中,并将参数从@Part 更改为@Body。


fun uploadImage(filepath: String) {
        Log.e("info", ">>>>>status>>>>>" + ">>>>>>" + filepath)
        val file = File(filepath)
        Log.d("file...", "length..." + file.length())

        val mimeType = getMimeType(file)
        val metaobject = JSONObject()
        metaobject.put("eye", "OD")



 val requestBody: RequestBody = MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("metadata", metaobject.toString())
                        .addFormDataPart(
                            "file", file.name,
                            file.asRequestBody(mimeType!!.toMediaTypeOrNull())
                        )
                        .build()

        val header = HashMap<String, String>()
      
  
        header["Authorization"] =
            "Bearer <<token>>"
        val request = ApiService.buildService(ApiHelper::class.java)
        val call = request.sendMediaFile(header,requestBody)
        call.enqueue(
            object : Callback<ResponseBody?> {
                override fun onResponse(
                    call: Call<ResponseBody?>,
                    response: Response<ResponseBody?>
                ) {
                    Log.e("Upload", "success" + response.body().toString())
                    Log.e("Upload", "success" + response.errorBody().toString())

                }

                override fun onFailure(call: Call<ResponseBody?>, t: Throwable) {
                    Log.e("Upload error:", t.message!!)
                }
            })
    }


并在帮助程序中 class

@POST("{profileId}/files")
    fun sendMediaFile(
        @Path("profileId") profile_id: String,
        @HeaderMap headers: Map<String, String>,
        @Body metadata: RequestBody?
    ): Call<ResponseBody?>