使用改造将图像上传到服务器 [英] Upload image into server using retrofit

查看:50
本文介绍了使用改造将图像上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下代码使用其uri将图像/文件上传到服务器

I want to upload image/file to server using its uri using below code

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服务

My API service for that request

 @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向我返回了内部服务器错误状态500,但我知道服务器运行良好(我在其他应用程序上对其进行了测试).另外,文件uri也可以.我是新来的,所以有人可以找到我的错误并详细解释为什么是错误的.

So, here my response is not successful. API returns me Internal Server error status 500, but I know that server works well(I tested it other application). Also, file uri is ok too. I am new here, so can someone find my mistake and explain in detail why it is wrong.

推荐答案

如果有人发现它有用,则错误在于此处的第一个参数名称"(在我的情况下,根据服务器,它应该是文件",而不是头像")

if someone find it useful, mistake was in 1st parameter "name" here (in my case it should be "file" according to server, instead of "avatar")

MultipartBody.Part body2 = MultipartBody.Part.createFormData("avatar", file.getName(), new FileRequestBody(file, extension));

Response<ResponseBody> response = ApiCaller.getProfileAPI().requestingUploadProfilePhoto("Bearer " + AuthenticationProvider.getInstance().token.token,
                            ProfileProvider.getInstance().parsedProfile.profileId,
                            body2).execute();
                    if (response.isSuccessful()){
                        Logger.msg("Photo", ": uploaded successfully " + uri);
                    }else{
                        Logger.msg("Photo", ": uploaded successfully none" + uri + response.errorBody().string());
                    }

因此,足以对其进行更改并删除所有不必要的内容.同样,当然,有必要根据参数更改服务文件.我没有足够注意它,因为在教程中没有将它作为一些重要参数提及.

So, it was enough to change it and remove all unnecessary stuff. Also, of course, it is necessary to change service file, according to parameters. I did not pay attention to it enough, because in tutorials it was not mentioned as some important parameter.

这篇关于使用改造将图像上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆