I'm trying to send an image file from Android retrofit2 to a spring server, but I couldn't work on it, so I'm posting a questioncrying

Asked 2 years ago, Updated 2 years ago, 145 views

public static insertPromoteMsg insertPromoteMsgAPI() {
        return (insertPromoteMsg) retrofit(insertPromoteMsg.class);
    }

    public interface insertPromoteMsg {
        @Multipart
        @POST("/insertPromoteMsgApp3.do")
        Call<ResponseCode> insertPromoteMsg(@Part MultipartBody.Part uploadFile);
    }

The format is retrofit2.

private void insertPromote(EditText writingArea, EditText promoteTitle) {
        File file = new File(imgPath);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part uploadFile = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        Log.i("test", "insertPromote: "+ file.getName());
        Log.i("test", "insertPromote: "+ requestFile.contentType());
        Log.i("test", "insertPromote: "+ uploadFile.body());
        Call<ResponseCode> call = NetworkService.insertPromoteMsgAPI().insertPromoteMsg(uploadFile);
        call.enqueue(new Callback<ResponseCode>(){
            @Override
            public void onResponse(Call<ResponseCode> call, Response<ResponseCode> response) {
                if(response.isSuccessful()){
                }else{
                }
            }

            @Override
            public void onFailure(Call<ResponseCode> call, Throwable t) {
                Log.i("test", "onFailure: "+t.getMessage());
            }
        });
    }

Code sent to the server.ㅠ f As the file name is taken well, there seems to be no problem with the file object itself.

    @RequestMapping(value = "insertPromoteMsgApp.do", method = RequestMethod.POST)
    @ResponseBody
    public int insertPromoteMsgApp(MultipartFile uploadFile) {
        System.out.println ("insertPromoteMsgApp call");
        if(uploadFile == null) {
            System.out.println("null");
        }else {
            System.out.println(uploadFile.getContentType());
        }
        return 101;
    }

Server-side code. As you can see, the MultipartFile object continues to be null.

I've tried everything while googling, but I keep getting nulls.I've been shoveling for two days May I know what the problem is?

retrofit2 android spring

2022-09-22 10:50

2 Answers

It's a self-answer.

MultipartBody.Part uploadFile = MultipartBody.Part.createFormData("file", file.getName(), requestFile);

I matched the "file" part with the server parameter name, so it worked.

It's definitely the same work I've done.I tried it because I know that if you match the parameter name when communicating on the web, it will be mapped on its own.I did it again just in case.ㅠ<


2022-09-22 10:50

RequestBody.create(MediaType.parse("multipart/form-data"), file);

In , you must insert the MIME Type of the file instead of multipart/form-data. For your information, if you have imported an image from the gallery, you can import the MIME type as shown below.

getContentResolver().getType(data.getData())


2022-09-22 10:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.