사진 업로드를 구현하는 중에
FileSizeLimitExceededException
오류가 발생하였다!
업로드하는 파일의 용량이 커서 발생하는 오류로
스프링의 경우 1MB를 넘어가면 해당 오류가 발생한다.
해결 방법 1. application.properties 설정하기
스프링 부트 2.0.0이상인 경우
spring.servlet.multipart.max-file-size=12MB
spring.servlet.multipart.max-request-size=12MB
스프링 부트 1.5.9이하인 경우
spring.http.multipart.max-file-size = 20MB
spring.http.multipart.max-request-size = 20MB
스프링 부트의 버전에 맞게 application.properties에 해당 코드들을 입력하면
입력한 사이즈까지 파일 업로드가 가능하다😆
해결 방법 2. ExceptionHandler 어노테이션을 사용해서 예외처리하기
파일 용량을 제한하고도 그 이상으로 넘어가는 파일이 들어오는 경우에는
ExceptionHandler를 통해 따로 예외처리를 하기로 했다.
@ExceptionHandler(FileSizeLimitExceededException.class)
public ResponseEntity<ResponseForm> handleFileSizeLimitExceededException(FileSizeLimitExceededException e) {
log.info("FileSizeLimitExceededException 발생");
// 예외 처리 로직
ResponseForm responseForm = ResponseForm.builder()
.code(HttpStatus.BAD_REQUEST.value())
.httpStatus(HttpStatus.BAD_REQUEST)
.msg("사진 용량이 너무 큼")
.count(1)
.result(List.of(e.getMessage()))
.build();
return new ResponseEntity<>(responseForm, responseForm.getHttpStatus());
}
[출처]
'프로젝트' 카테고리의 다른 글
[SuiteCare] 공공데이터 포털에 OPEN API 요청하기 (0) | 2024.04.27 |
---|---|
[SuiteCare] MySQL 다른 테이블에서 데이터 가져와서 insert하기 (0) | 2024.04.15 |
[SuiteCare] MySQL에서 일정 시간 후 자동으로 데이터 업데이트하기 (0) | 2024.04.09 |
[SuiteCare] SpringBoot에서 MultipartFile과 DTO 같이 받기 (1) | 2024.03.22 |
[SuiteCare] MultipartFile과 JSON 같이 보내기 (0) | 2024.03.21 |