Android studio 使用改装2将图片上载到服务器

Android studio 使用改装2将图片上载到服务器,android-studio,retrofit2,okhttp3,Android Studio,Retrofit2,Okhttp3,首先,让我们看看代码: public interface ApiInterface { @Multipart @POST("my/files/photo/") Call<FileUploadResponse> uploadPhoto(@Header("authorization") String auth, @Part MultipartBody.Part file,

首先,让我们看看代码:

public interface ApiInterface {
    @Multipart
    @POST("my/files/photo/")
    Call<FileUploadResponse> uploadPhoto(@Header("authorization") String auth,
                                     @Part MultipartBody.Part file,
                                     @Part("file-type") RequestBody fileType);
}
我从服务器得到的响应是未提供的文件类型。如果我在uploadPhoto函数中更改文件位置和文件类型,如下所示:

RequestBody body = RequestBody.create(MediaType
            .parse(getContentResolver().getType(fileUri)), file);
MultipartBody.Part avatarBody = MultipartBody.Part
            .createFormData(data, file.getName(), body);
RequestBody fileType = RequestBody.create(
            MediaType.parse("text/plain"), "profile");
client.uploadPhoto(getToken(), avatarBody, fileType);
@Multipart
@POST("my/files/photo/")
Call<FileUploadResponse> uploadPhoto(@Header("authorization") String auth,
                                 @Part("file-type") RequestBody fileType
                                 @Part MultipartBody.Part file);
@POST("my/files/photo/")
    Call<FileUploadResponse> uploadPhoto(@Header("Content-Type") String contentType,
                                          @Header("Authorization") String auth,
                                          @Body MultipartBody body);
我收到的响应文件未提供。 我已经用自己写在JS上的代码检查了服务器:

var blobFile; 函数在局部变化中的作用{ blobFile=ss.files[0]; alertblobFile; } 函数上传文件{ var storedJWT=$'authorize'.val; var fileType=$'type'.val; var formData=新formData; formData.appendfile-type,fileType; formData.appendphoto,blobFile; console.logstoredJWT; $.ajax{ url:url, 类型:POST,, 数据:formData, 标题:{ 授权:storedJWT }, processData:false, contentType:false, 成功:功能响应{ console.logresponse; }, 错误:functionjqXHR、textStatus、errorMessage{ console.logerrorMessage; } };
} 经过多次搜索,我发现服务器没有处理像

Content-Transfer-Encoding: binary
Content-Type: text/plain; charset=utf-8
我必须从我的请求中删除这些头。解决方案是在我的API接口中创建上传函数,如下所示:

RequestBody body = RequestBody.create(MediaType
            .parse(getContentResolver().getType(fileUri)), file);
MultipartBody.Part avatarBody = MultipartBody.Part
            .createFormData(data, file.getName(), body);
RequestBody fileType = RequestBody.create(
            MediaType.parse("text/plain"), "profile");
client.uploadPhoto(getToken(), avatarBody, fileType);
@Multipart
@POST("my/files/photo/")
Call<FileUploadResponse> uploadPhoto(@Header("authorization") String auth,
                                 @Part("file-type") RequestBody fileType
                                 @Part MultipartBody.Part file);
@POST("my/files/photo/")
    Call<FileUploadResponse> uploadPhoto(@Header("Content-Type") String contentType,
                                          @Header("Authorization") String auth,
                                          @Body MultipartBody body);
因此,我收到以下请求:

Content-Type: multipart/form-data; boundary=27ec8d66-d29d-48aa-a2fe-08c6664b10c7
Content-Length: 56412
Authorization: JWT token
--27ec8d66-d29d-48aa-a2fe-08c6664b10c7
Content-Disposition: form-data; name="file-type"
Content-Length: 7
profile
--27ec8d66-d29d-48aa-a2fe-08c6664b10c7
Content-Disposition: form-data; name="photo"; filename="image.png"
Content-Type: image/jpeg
Content-Length: 56089
/* bytes */
--27ec8d66-d29d-48aa-a2fe-08c6664b10c7--
而且效果很好