fileupload 용량제한 관련 수정
parent
f3f7a96f8f
commit
1def698009
@ -0,0 +1,119 @@
|
||||
package egovframework.exception;
|
||||
|
||||
import egovframework.util.FileUtil;
|
||||
import lombok.Getter;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
|
||||
/**
|
||||
* 파일 업로드 크기 제한 초과 예외 클래스
|
||||
*
|
||||
* 이 예외는 파일 업로드 시 크기 제한을 초과했을 때 발생합니다.
|
||||
* 실제 설정된 파일 크기 제한 정보를 포함합니다.
|
||||
*
|
||||
* Spring Boot의 MaxUploadSizeExceededException을 래핑하여
|
||||
* 실제 설정된 파일 크기 제한 정보를 에러 메시지에 표시할 수 있도록 합니다.
|
||||
*/
|
||||
@Getter
|
||||
public class FileUploadLimitExceededException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 에러 코드
|
||||
*/
|
||||
private final String errorCode;
|
||||
|
||||
/**
|
||||
* 최대 파일 크기 (바이트 단위)
|
||||
*/
|
||||
private final long maxFileSize;
|
||||
|
||||
/**
|
||||
* 최대 요청 크기 (바이트 단위)
|
||||
*/
|
||||
private final long maxRequestSize;
|
||||
|
||||
/**
|
||||
* 실제 파일 크기 (바이트 단위, 알 수 있는 경우)
|
||||
*/
|
||||
private final long actualFileSize;
|
||||
|
||||
/**
|
||||
* 기본 생성자
|
||||
*
|
||||
* @param message 예외 메시지
|
||||
* @param maxFileSize 최대 파일 크기 (바이트 단위)
|
||||
* @param maxRequestSize 최대 요청 크기 (바이트 단위)
|
||||
* @param cause 원인 예외
|
||||
*/
|
||||
public FileUploadLimitExceededException(String message, long maxFileSize, long maxRequestSize, MaxUploadSizeExceededException cause) {
|
||||
super(message, cause);
|
||||
this.errorCode = "FILE_SIZE_EXCEEDED";
|
||||
this.maxFileSize = maxFileSize;
|
||||
this.maxRequestSize = maxRequestSize;
|
||||
this.actualFileSize = extractActualFileSize(cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* 원인 예외에서 실제 파일 크기를 추출합니다.
|
||||
*
|
||||
* @param cause 원인 예외
|
||||
* @return 실제 파일 크기 (바이트 단위), 추출할 수 없는 경우 -1
|
||||
*/
|
||||
private long extractActualFileSize(MaxUploadSizeExceededException cause) {
|
||||
if (cause == null) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// MaxUploadSizeExceededException의 메시지에서 실제 파일 크기를 추출
|
||||
// 예: "the request was rejected because its size (3520463011) exceeds the configured maximum (10485760)"
|
||||
String message = cause.getMessage();
|
||||
if (message == null) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
try {
|
||||
// 괄호 안의 첫 번째 숫자를 추출
|
||||
int startIndex = message.indexOf('(');
|
||||
int endIndex = message.indexOf(')', startIndex);
|
||||
if (startIndex >= 0 && endIndex > startIndex) {
|
||||
String sizeStr = message.substring(startIndex + 1, endIndex).trim();
|
||||
return Long.parseLong(sizeStr);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 추출 실패 시 무시
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 최대 파일 크기를 사람이 읽기 쉬운 형식으로 반환합니다.
|
||||
*
|
||||
* @return 사람이 읽기 쉬운 형식의 최대 파일 크기 (예: "10MB")
|
||||
*/
|
||||
public String getHumanReadableMaxFileSize() {
|
||||
return FileUtil.formatFileSize(maxFileSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* 최대 요청 크기를 사람이 읽기 쉬운 형식으로 반환합니다.
|
||||
*
|
||||
* @return 사람이 읽기 쉬운 형식의 최대 요청 크기 (예: "50MB")
|
||||
*/
|
||||
public String getHumanReadableMaxRequestSize() {
|
||||
return FileUtil.formatFileSize(maxRequestSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* 실제 파일 크기를 사람이 읽기 쉬운 형식으로 반환합니다.
|
||||
*
|
||||
* @return 사람이 읽기 쉬운 형식의 실제 파일 크기 (예: "15MB"), 알 수 없는 경우 "알 수 없음"
|
||||
*/
|
||||
public String getHumanReadableActualFileSize() {
|
||||
if (actualFileSize < 0) {
|
||||
return "알 수 없음";
|
||||
}
|
||||
return FileUtil.formatFileSize(actualFileSize);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue