【问题标题】:With retrofit upload image using multipart form but not get the path of the file使用多部分表单修改上传图像但未获取文件的路径
【发布时间】:2019-04-22 22:32:55
【问题描述】:

当我尝试使用带有此代码的多部分表单上传图像时,我没有获得要上传到服务器的文件的路径。

  1. 已经创建了这样的界面,我想使用 多部分
 @Headers({"Content-Type: application/json;charset=UTF-8"})
    @Multipart
    @POST("api/updateprofile")
    Call<ResponseBody> uploadPhoto1(
            @Header("Authorization") String token,
            @Part("v_name") RequestBody name,
            @Part MultipartBody.Part image,
  1. 当我尝试调用此 api 时,我得到了响应,但出现了问题 向服务器发送文件。
 RequestBody namePart = RequestBody.create(MediaType.parse("text/plain"), "Narendra");
   File file = new File(filePath);
   RequestBody filePart = RequestBody.create(MediaType.parse("image/*"), file);
   MultipartBody.Part file1 = MultipartBody.Part.createFormData("v_image", file.getName(), filePart);


    APIService apiService = ApiClient.getRetrofit().create(APIService.class);
    Call<ResponseBody> Call = apiService.uploadPhoto1(token,namePart,file1);

我该如何解决?

【问题讨论】:

  • @BrijeshMavani:感谢您想在这里编辑。你是对的,这里的引用块是不合适的,但是过分的粗体也是如此。最好谨慎使用样式,因为冗长的粗体/斜体很难阅读。也请不要代表问题作者添加尽快乞求 - 这被广泛认为是不礼貌的。

标签: java android image android-studio retrofit


【解决方案1】:

我自己解决了这个问题

只需从此界面中删除标题部分

@Multipart
@POST("api/updateprofile")
Call<ResponseBody> uploadPhoto1(
        @Header("Authorization") String token,
        @Part("v_name") RequestBody name,
        @Part MultipartBody.Part image,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 2017-01-01
    • 2013-03-04
    相关资源
    最近更新 更多