단순히 파일을 업로드하는 것에 그치지 않고, 대상에 대한 검사나, 전처리 등이 요구된다.
- 업로드 되는 파일의 확장자 검사 : 특정 확장자에 대해서만 업로드 허용
- 파일 사이즈에 대한 검사 : 업로드 사이즈에 대한 한계 값 설정
- 중복 파일 처리 : 동일 이름의 파일 업로드 시의 처리
- 저장 파일에 대한 관리 : 저장 파일 수가 증가하는 경우, 로딩 속도가 증가함
- 썸네일에 대한 처리 : 이미지의 미리보기용 썸네일 생성
- 업로드 파일에 대한 정보 전달 : 이미지 전송과 동시에 해당 파일에 대한 정보 전달
썸네일 생성의 경우, 별도의 maven repository 에서의 설정이 요구됨
pom.xml 설정
- 썸네일 생성용
1 2 3 4 5 6 | <dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 | <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.9.5</version> </dependency> | cs |
실습 jsp(ajax 방식)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <div id='uploadDiv'> <input type='file' name='uploadFile' multiple> </div> <button id='uploadBtn'>upload</button> </body> <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script> <script type="text/javascript"> $(document).ready(function(){ // 정규식을 이용하여 해당 파일 확장자 확인 var regax = new RegExp("(.*?)\.(exe|sh|zip|alz)$"); // 파일 최대 사이즈 var maxSize = 5242880; // 파일 최대 사이즈 지정 및 확장자 검사 function checkExtension(fileName, fileSize){ if(fileSize >= maxSize){ alert("파일 사이즈 초과") return false; } if(regax.test(fileName)){ alert("해당 종류의 파일은 업로드할 수 없습니다.") return false; } return true; } $("#uploadBtn").on('click', function(e){ var formData = new FormData(); var inputFile=$("input[name='uploadFile']") var files = inputFile[0].files; console.log(files) for(var i =0; i<files.length; i++){ if(!checkExtension(files[i].name, files[i].size)){ return false; } formData.append("uploadFile", files[i]); } $.ajax({ url : '/uploadAjaxAction', processData : false, contentType : false, data : formData, type : 'POST', dataType:'json', success : function(result){ alert("uploaded"); } }) }) }) </script> </html> | cs |
- json 형식 전송 수행
사용 DTO(VO)
1 2 3 4 5 6 7 8 9 10 11 12 | package org.zerock.domain; import lombok.Data; @Data public class AttachFileDTO { private String filename; private String uploadPath; private String uuid; private boolean image; } | cs |
- 파일의 이름, 업로드 경로, 저장에 사용된 uuid, 이미지 여부 확인 저장
사용 controller
cs
댓글
댓글 쓰기