☆ Code
★ register.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Write</title>
</head>
<body>
<h1>마음대로 글을 쓰셔도 될까요?</h1>
<hr>
<form action="/board/register" method="post">
<p>title : <input type="text" name="title"></p>
<p>content : <br><textarea name="content"></textarea></p>
<p>writer : <input type="text" name="writer"></p>
<div class="uploadDiv">
<input type="file" name="uploadFile" multiple style="width: 200px;">
</div>
<div class="uploadResult">
<ul>
<li>File1</li>
<li>File2</li>
</ul>
</div>
<input type="submit" value="Register">
</form>
</body>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$("input[type='file']").change(function(e){
let inputFile = $("input[name='uploadFile']");
let files = inputFile[0].files;
console.log(files);
let formData = new FormData();
for(let i=0; i<files.length; i++){
if(checkFile(files[i].name, files[i].size) == false){
alert("파일 업로드 실패");
return;
}
formData.append("uploadFile", files[i]);
}
console.log(formData);
$.ajax({
url: "/upload/uploadAjaxAction",
processData: false,
contentType: false,
data: formData,
type: "POST",
success: function(result){
alert("File Upload Completed");
console.log(result);
},
error: function(error){
console.log("야 오류났어");
console.log(error);
}
});
});
function checkFile(fileName, fileSize){
let MAXSIZE = 5000000;
if(fileSize > MAXSIZE){
alert("파일 사이즈가 왜 이렇게 큰가요");
return false;
}
let RULE = new RegExp("(.*?)\.(exe|sh|zip|alz)$");
if(RULE.test(fileName)){
alert("이런 파일 올리시면 혼납니다.")
return false;
}
return true;
}
</script>
</html>
★ UploadContriller.java
package com.koreait.board.controller;
import java.io.File;
import java.nio.file.Files;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import lombok.extern.slf4j.Slf4j;
@Controller
@Slf4j
@RequestMapping("/upload/*")
public class UploadController {
@PostMapping("uploadAjaxAction")
@ResponseBody
public String uploadAjaxPost(MultipartFile[] uploadFile) {
log.info("[uploadController] uploadAjaxPost() Called OK");
String uploadFolder = "C:/upload/temp";
for(MultipartFile f : uploadFile) {
log.info("filename : " + f.getOriginalFilename());
log.info("filesize : " + f.getSize());
File saveFile = new File(uploadFolder, f.getOriginalFilename());
try {
f.transferTo(saveFile);
if(checkImageType(saveFile)) {
log.info("Image File");
}
else {
log.info("Not Image File");
}
}
catch(Exception e) {
e.printStackTrace();
}
}
return "데이터를 돌려드립니다";
}
private boolean checkImageType(File file) {
try {
String contentType = Files.probeContentType(file.toPath());
log.info("ContentType : " + contentType);
return contentType.startsWith("image");
}
catch(Exception e) { e.printStackTrace(); }
return false;
}
}
'Spring' 카테고리의 다른 글
Spring 첨부파일 띄우기 (0) | 2025.01.04 |
---|---|
Spring 파일 업로드 심화 (1) | 2024.12.31 |
Spring 미디어쿼리를 통한 반응형 페이지 추가 (1) | 2024.12.28 |
Spring 글 수정 로직 (0) | 2024.12.27 |
Spring 게시판 글 작성, 삭제 로직 (0) | 2024.12.26 |