Cannot POST multipart data from retrofit 2

烂漫一生 提交于 2019-12-05 05:41:26

At API side

  @Multipart
  @POST("users/{id}/user_photos")
  Call<SignUpResp> uploadPhoto(@Part("description") RequestBody description, @Part MultipartBody.Part file,(add if more parameter req as like ID,name));

At java file side

    if (fileUpload != null) {


        /**
         * code for multipart
         */

        // create RequestBody instance from file
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"), fileUpload);

        // MultipartBody.Part is used to send also the actual file name
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("user_photo[image]", fileUpload.getName(), requestFile);

        // add another part within the multipart request
        String descriptionString = "hello, this is description speaking";
        RequestBody description =
                RequestBody.create(
                        MediaType.parse("multipart/form-data"), descriptionString);


        call = userRequest.uploadPhoto(description, body, authData);

    }

I've used this lib and it works like a charm for uploading multiparts

change

@Part("name=\"user_photo[image]\"") RequestBody file)

to

@Part("user_photo[image]\"; filename=\"file.jpg\" ")

Create file's requestBody using

RequestBody fileBody = RequestBody.create(MediaType.parse("image"), myImageFile); 

You can find more about this on

https://github.com/square/retrofit/issues/1140

In this case the filename file.jpg is hardcoded you can even have dynamic filenames. Although I didn't try dynamic file naming but below is the link which might work

https://github.com/square/retrofit/issues/1063#issuecomment-145920568

HourGlass

You have to send it as typed file.

 TypedFile upload_file = new TypedFile("multipart/form-data", new File(your_file_location));

 @Multipart
 @POST("users/{id}/user_photos")
 Call<models.UploadResponse> uploadPhoto(@Path("id") int userId, @Part("name=\"user_photo[image]\"")TypedFile upload_file);

Hope it helps

I used the following methods, they are not in RetroFit, hope that helps: Server Side: Spring Boot java application:

@RequestMapping(value="/applyeffect", method=RequestMethod.POST,produces=MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public @ResponseBody byte[] applyEffect(@RequestParam("userid") String userId, @RequestParam("type") int effectType,@RequestParam("file") MultipartFile file){

        try {
                byte[] bytes = file.getBytes();
                File temp=new File(file.getName());
                BufferedOutputStream stream =
                        new BufferedOutputStream(new FileOutputStream(temp));
                stream.write(bytes);
                stream.close();
                File newFile=null;
                if(effectType==1){
                    newFile=ImageUtils.applyTint(userId, temp, 50); 
                }else if(effectType==2){
                    newFile=ImageUtils.applyEffect(userId, temp, 128);
                }else if(effectType==3){
                    newFile=ImageUtils.applyBlackWhiteEffect(userId,temp, 128);
                }






                FileInputStream input=new FileInputStream(newFile);
                byte[]data=IOUtils.toByteArray(input);


                return data;

            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
    }

Android Client side: I used this util class https://github.com/MinaSamy/DailySelfie/blob/master/app/src/main/java/bloodstone/dailyselfie/android/utils/PostMultiPart.java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!