Resolved [org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded]
out 로그(terminal)에는 위와같이 찍혔다.
우선 파일 제한 용량을 올려주기 위해 다음과 같이 할 수 있다.
– application.properties 또는 application.yaml 수정
spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=10MB
이건 properteis
spring: servlet: multipart: max-file-size: 10MB max-request-size: 10MB
yaml 파일은 이렇게
10MB말고 다른걸로 해도됨.
Bean 을 만들어서 Java 에서도 관리할 수 있는것 같지만 내 수준엔 아직 어려워보이니 설정파일은 건드는것으로 해결
그럼 10MB로 늘려서 어느정도 공간을 확보했는데,
그 이상의 용량의 파일을 또 업로드 시도를 할수 있을텐데. 이를 미연에 방지하는 방법은?
<form id=”postForm” action=”${pageContext.request.contextPath}/Modify?num=${post.num}” method=”post” accept-charset=”utf-8″ enctype=”multipart/form-data”>
<label for=”title”>글번호:</label>
<input type=”text” id=”num” name=”num” value=”${post.num}” readonly><br><br>
<label for=”title”>제목:</label>
<input type=”text” id=”title” name=”title” value=”${post.title}”><br><br>
<label for=”author”>작성자:</label>
<input type=”text” id=”author” name=”author” value=”${post.author}”><br><br>
<label for=”file”>파일첨부:</label>
<input type=”file” id=”file” name=”file”><br><br>
<label for=”contents”>내용:</label><br>
<textarea id=”contents” name=”contents”>${post.contents}</textarea><br><br>
<input type=”submit” value=”수정”>
</form>
내 업로드 form 양식
<script>
document.getElementById(‘postForm’).addEventListener(‘submit’, function(event) {
const fileInput = document.getElementById(“file”);
const maxSize = 10 * 1024 * 1024;
if (fileInput.files.length > 0){
if(fileInput.files[0].size > maxSize){
alert(“10MB 이하만 업로드 가능합니다.”)
event.preventDefault();
}
}
});
</script>
script 에서 fileSize 를 미리 확인하여 방지할 수 있다.
그 외에 파일 업로드 컨트롤러를 따로 만들어두었다면
private static final long MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB @PostMapping("/upload") public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) { if (file.getSize() > MAX_FILE_SIZE) { return ResponseEntity.badRequest().body("File size exceeds 10MB limit."); } // 파일 처리 로직 return ResponseEntity.ok("File uploaded successfully."); }
이런 예제로 미리 파일 업로드 처리전에 막을 수 있다.