안드로이드 retrofit2 에서 spring 서버로 이미지파일을 전송할려고 하는데 도저히 작업을 해도 안돼서 질문 올립니다ㅠㅠ

조회수 4541회
public static insertPromoteMsg insertPromoteMsgAPI() {
        return (insertPromoteMsg) retrofit(insertPromoteMsg.class);
    }

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

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());
            }
        });
    }

서버로 전송하는 코드입니다..ㅠㅠ file 이름이 잘 찍히는거 보니 file 객체 자체의 문제는 없는것같습니다..

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

서버쪽 코드입니다. 보시면 아시겠지만 MultipartFile 객체가 계속 null이 나옵니다..

구글링하면서 진짜 이방법 저방법 다해봤는데 계속 null이 나오네요ㅠㅠ...이틀째 삽질중인데 뭐가 문제인지 알 수 있을까요...

  • (•́ ✖ •̀)
    알 수 없는 사용자

2 답변

  • 자문 자답입니다..

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

    여기서 "file" 부분을 서버 매개변수명이랑 맞추니깐 되네요..

    분명 해봤던거 같은 작업인데..web에서도 통신할때 매개변수명 맞추면 알아서 매핑이 되는걸 알기때문에 분명 해봤는데..혹시나하는 마음에 다시 해보니깐 되네요..ㅠㅠ

    • (•́ ✖ •̀)
      알 수 없는 사용자
    • 아 저도 매개변수 달라서 헤맸던 적이 있었던것 같네요 ㅋㅋ 알 수 없는 사용자 2018.7.9 18:49
  • RequestBody.create(MediaType.parse("multipart/form-data"), file);
    

    에서 multipart/form-data 대신 해당 파일의 MIME Type 을 넣어야 합니다. 참고로 갤러리에서 이미지를 불어왔을 경우 아래와 같이 MIME type 을 가져올 수 있습니다.

    getContentResolver().getType(data.getData())
    
    • (•́ ✖ •̀)
      알 수 없는 사용자
    • 해당 작업을 해본결과 multipart/form-data 대신에 mine type 이 image/jpeg 로 나와서 image/jpeg 을 넣어봤는데 똑같이 서버에서는 Multipart 객체가 null로 나오네요..ㅠㅠㅠ 알 수 없는 사용자 2018.7.9 13:42

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)