Я хочу загрузить изображение / файл на сервер, используя его URI, используя следующий код
try {
String descriptionString = "Sample description";
RequestBody description = RequestBody.create(MultipartBody.FORM, descriptionString);
File file = new File(path);
String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
if (extension != null) {
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
RequestBody requestFile = RequestBody.create(MediaType.parse(type), file);
MultipartBody.Part body2 = MultipartBody.Part.createFormData("avatar", file.getName(), new FileRequestBody(file, extension));
Response<ResponseBody> response = ApiCaller.getProfileAPI().requestingUploadProfilePhoto("Bearer " + AuthenticationProvider.getInstance().token.token,
"form-data; name=avatar; filename=\"" + file.getName() + "\"",
ProfileProvider.getInstance().parsedProfile.profileId,
description,
body2).execute();
if (response.isSuccessful()){
Logger.msg("Photo", ": uploaded successfully " + uri);
}else{
Logger.msg("Photo", ": uploaded successfully none" + uri + response.errorBody().string());
}
}else{
Logger.msg("Photo", ":extension error");
}
} catch (IOException e) {
e.printStackTrace();
}
Мой API-сервис для этого запроса
@Multipart
@POST("v1/avatar/profiles/{profileId}/upload")
Call<ResponseBody> requestingUploadProfilePhoto(@Header("Authorization") String authHeader,
@Header("Content-Disposition") String content_type,
@Path("profileId") String profileId,
@Part("description") RequestBody description,
@Part MultipartBody.Part file);
Итак, мой ответ не был успешным,API возвращает мне статус ошибки Internal Server 500, но я знаю, что сервер работает хорошо (я тестировал это другое приложение).Кроме того, файл URI тоже хорошо.Я новичок здесь, так что кто-то может найти мою ошибку и подробно объяснить, почему это неправильно.