Spring

Spring 파일 업로드

테라시아 2024. 12. 30. 05:48

☆ 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;
	}
}