Spring framework 에서 다음과 같이 요청값에 대한 크기를 설정하기 위해 관리하는 값이 있다.

요청이 들어왔을 때 별도의 로직으로 처리하지 않고 요청을 거부하기 위해서 위의 설정값을 지정해주면 된다.

max-file-size

HTTP 요청 메시지에 포함되어 있는 하나의 파일 의 최대 크기를 설정하기 위한 값이다. 최대 크기를 지정하지 않으려면 해당 값을 -1 로 설정한다.

spring:
	servlet:
		multipart:
			max-file-size: 1MB # 원하는 크기로 지정

지정한 크기보다 큰 파일을 요청할 경우 다음과 같은 에러가 발생한다.

(MaxUploadSizeExceededException, FileSizeLimitExceededException)

org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.

max-request-size

HTTP 요청 메시지 자체의 최대 크기를 설정하기 위한 값이다. 최대 크기를 지정하지 않으려면 해당 값을 -1 로 설정한다.

spring:
	servlet:
		multipart:
			max-request-size: 1MB # 원하는 크기로 지정

지정한 크기보다 큰 요청을 할 경우 다음과 같은 에러가 발생한다.

(MaxUploadSizeExceededException, SizeLimitExceededException)

org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (3597000) exceeds the configured maximum (1048576)

이미지 크기 제한

모모 프로젝트에서는 확장성을 고려하여 Spring 에서 설정하는 방식과 서비스 로직에서 처리하는 방식 두 가지를 채택하였다. 이후에 추가될 이미지에 대한 처리를 위해 Spring 에서 설정한 값에 대한 예외 처리를 추가하여 보편적인 예외 처리를 추가하였고, 모임 이미지에 적절한 예외 처리를 해주기 위해 서비스 로직에서 예외 처리를 추가하였다.