[error] file data type error

인철·2024년 3월 2일

Error

목록 보기
12/12
post-thumbnail

Spring MVC에서 컨트롤러의 메서드에 바인딩된 모델 객체의 유효성 검사(validation) 실패를 나타냅니다. 특히, joinForm 객체의 profileImg 필드에 대한 유효성 검사에서 문제가 발생했음을 지적합니다. profileImg 필드는 org.springframework.web.multipart.MultipartFile 타입으로 기대되지만, 실제로는 java.lang.String 타입의 값이 전달되었습니다. 이는 클라이언트 측에서 파일 업로드 필드에 문자열 값을 전달하거나, 서버 측에서 이 필드를 처리하는 방식에 에러 발생

Validation failed for object='joinForm'. Error count: 1
org.springframework.web.method.annotation.ModelAttributeMethodProcessor1:Validationfailedforargument[0]inpublicjava.lang.Stringcom.sideproject.healMingle.boundContext.member.controller.MemberController.join(com.sideproject.healMingle.boundContext.member.controller.MemberController1: Validation failed for argument [0] in public java.lang.String com.sideproject.healMingle.boundContext.member.controller.MemberController.join(com.sideproject.healMingle.boundContext.member.controller.MemberControllerJoinForm): [Field error in object 'joinForm' on field 'profileImg': rejected value [IT_Infratructure.png]; codes [typeMismatch.joinForm.profileImg,typeMismatch.profileImg,typeMismatch.org.springframework.web.multipart.MultipartFile,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [joinForm.profileImg,profileImg]; arguments []; default message [profileImg]]; default message [Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; Cannot convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile': no matching editors or conversion strategy found]]
--- 에러 내용---

오류 주요 내용

  • 필드 경로 : 'joinForm.profileImg'
  • 거부된 값 : IT_Infratructure.png (문자열)
  • 오류 코드 : typeMismatch
  • 기본 메시지: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'

원인 분석

  • 'ModelAttribute'를 이용하여 폼 데이터를 'joinForm'. 객체에 바인딩할 때 'profileImg'필드가 MultipartFile 타입으로 선언되었으나, 실제로는 문자열 값이 전달됨
  • 파일 업로드를 위한 HTML 폼이 enctype="multipart/form-data" 속성으로 설정되지 않았을 수 있음, 이 속성이 없을 시 브라우저는 파일을 올바르게 업로드 하지 않고, 파일 필드의 이름을 문자열 값으로 서버에 전송

해결방법

  • enctype="multipart/form-data" 추가하기
<form action="/your-endpoint" method="post" enctype="multipart/form-data">
    <input type="file" name="profileImg"/>
    <!-- 기타 필드 -->
    <button type="submit">Submit</button>
</form>
profile
같은글이있어도양해부탁드려요(킁킁)

0개의 댓글