From dda51a89b885e7f8785399dffa46098d6b296683 Mon Sep 17 00:00:00 2001 From: SeoJin Kim Date: Mon, 10 May 2021 05:57:01 +0900 Subject: [PATCH] =?UTF-8?q?[Spring][=EC=87=BC=ED=95=91=EB=AA=B0=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=EC=A0=9D=ED=8A=B8][24]=20=EC=83=81=ED=92=88?= =?UTF-8?q?=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=97=85=EB=A1=9C=EB=93=9C(?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20=ED=8F=B4=EB=8D=94=20=EC=83=9D=EC=84=B1)?= =?UTF-8?q?=20-=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://kimvampa.tistory.com/215 --- .../com/vam/controller/AdminController.java | 39 +++++++++++++++++++ .../WEB-INF/views/admin/goodsEnroll.jsp | 13 ++++++- .../maven/com.vam/controller/pom.properties | 2 +- .../com/vam/controller/AdminController.java | 39 ++++++++++++++++++- .../WEB-INF/views/admin/goodsEnroll.jsp | 13 ++++++- .../maven/com.vam/controller/pom.properties | 2 +- 6 files changed, 103 insertions(+), 5 deletions(-) diff --git a/VamPa/src/main/java/com/vam/controller/AdminController.java b/VamPa/src/main/java/com/vam/controller/AdminController.java index 08f5632..dd8a8dd 100644 --- a/VamPa/src/main/java/com/vam/controller/AdminController.java +++ b/VamPa/src/main/java/com/vam/controller/AdminController.java @@ -1,5 +1,8 @@ package com.vam.controller; +import java.io.File; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.List; import org.slf4j.Logger; @@ -11,6 +14,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.fasterxml.jackson.core.JsonProcessingException; @@ -261,4 +265,39 @@ public class AdminController { model.addAttribute("pageMaker", new PageDTO(cri, authorService.authorGetTotal(cri))); } + /* 첨부 파일 업로드 */ + @PostMapping("/uploadAjaxAction") + public void uploadAjaxActionPOST(MultipartFile[] uploadFile) { + + logger.info("uploadAjaxActionPOST.........."); + String uploadFolder = "C:\\upload"; + + /* 날짜 폴더 경로 */ + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + + Date date = new Date(); + + String str = sdf.format(date); + + String datePath = str.replace("-", File.separator); + + /* 폴더 생성 */ + File uploadPath = new File(uploadFolder, datePath); + + if(uploadPath.exists() == false) { + uploadPath.mkdirs(); + } + + + + // 향상된 for + for(MultipartFile multipartFile : uploadFile) { + + } + + + + + } + } diff --git a/VamPa/src/main/webapp/WEB-INF/views/admin/goodsEnroll.jsp b/VamPa/src/main/webapp/WEB-INF/views/admin/goodsEnroll.jsp index 1e1d098..69e04c1 100644 --- a/VamPa/src/main/webapp/WEB-INF/views/admin/goodsEnroll.jsp +++ b/VamPa/src/main/webapp/WEB-INF/views/admin/goodsEnroll.jsp @@ -484,6 +484,7 @@ $("#enrollBtn").on("click",function(e){ /* 이미지 업로드 */ $("input[type='file']").on("change", function(e){ + let formData = new FormData(); let fileInput = $('input[name="uploadFile"]'); let fileList = fileInput[0].files; let fileObj = fileList[0]; @@ -492,7 +493,17 @@ $("#enrollBtn").on("click",function(e){ return false; } - alert("통과"); + formData.append("uploadFile", fileObj); + + $.ajax({ + url: '/admin/uploadAjaxAction', + processData : false, + contentType : false, + data : formData, + type : 'POST', + dataType : 'json' + }); + }); diff --git a/VamPa/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties b/VamPa/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties index 338cac8..f353d42 100644 --- a/VamPa/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties +++ b/VamPa/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties @@ -1,5 +1,5 @@ #Generated by Maven Integration for Eclipse -#Wed Apr 28 02:05:10 KST 2021 +#Mon May 10 05:22:44 KST 2021 m2e.projectLocation=C\:\\Users\\sjinj\\git\\Blog_Project2\\VamPa m2e.projectName=VamPa groupId=com.vam diff --git a/VamPa_MySQL/src/main/java/com/vam/controller/AdminController.java b/VamPa_MySQL/src/main/java/com/vam/controller/AdminController.java index 1f8ae24..220614d 100644 --- a/VamPa_MySQL/src/main/java/com/vam/controller/AdminController.java +++ b/VamPa_MySQL/src/main/java/com/vam/controller/AdminController.java @@ -1,5 +1,8 @@ package com.vam.controller; +import java.io.File; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.List; import org.slf4j.Logger; @@ -11,6 +14,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.fasterxml.jackson.core.JsonProcessingException; @@ -263,6 +267,39 @@ public class AdminController { model.addAttribute("pageMaker", new PageDTO(cri, authorService.authorGetTotal(cri))); } - + /* 첨부 파일 업로드 */ + @PostMapping("/uploadAjaxAction") + public void uploadAjaxActionPOST(MultipartFile[] uploadFile) { + + logger.info("uploadAjaxActionPOST.........."); + String uploadFolder = "C:\\upload"; + + /* 날짜 폴더 경로 */ + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + + Date date = new Date(); + + String str = sdf.format(date); + + String datePath = str.replace("-", File.separator); + + /* 폴더 생성 */ + File uploadPath = new File(uploadFolder, datePath); + + if(uploadPath.exists() == false) { + uploadPath.mkdirs(); + } + + + + // 향상된 for + for(MultipartFile multipartFile : uploadFile) { + + } + + + + + } } diff --git a/VamPa_MySQL/src/main/webapp/WEB-INF/views/admin/goodsEnroll.jsp b/VamPa_MySQL/src/main/webapp/WEB-INF/views/admin/goodsEnroll.jsp index f8d64e7..562774f 100644 --- a/VamPa_MySQL/src/main/webapp/WEB-INF/views/admin/goodsEnroll.jsp +++ b/VamPa_MySQL/src/main/webapp/WEB-INF/views/admin/goodsEnroll.jsp @@ -465,6 +465,7 @@ $("#enrollBtn").on("click",function(e){ /* 이미지 업로드 */ $("input[type='file']").on("change", function(e){ + let formData = new FormData(); let fileInput = $('input[name="uploadFile"]'); let fileList = fileInput[0].files; let fileObj = fileList[0]; @@ -473,7 +474,17 @@ $("#enrollBtn").on("click",function(e){ return false; } - alert("통과"); + formData.append("uploadFile", fileObj); + + $.ajax({ + url: '/admin/uploadAjaxAction', + processData : false, + contentType : false, + data : formData, + type : 'POST', + dataType : 'json' + }); + }); diff --git a/VamPa_MySQL/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties b/VamPa_MySQL/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties index af82c83..1d57ffa 100644 --- a/VamPa_MySQL/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties +++ b/VamPa_MySQL/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties @@ -1,5 +1,5 @@ #Generated by Maven Integration for Eclipse -#Wed Apr 28 02:05:10 KST 2021 +#Mon May 10 05:53:52 KST 2021 m2e.projectLocation=C\:\\Users\\sjinj\\git\\Blog_Project2\\VamPa_MySQL m2e.projectName=VamPa_MySQL groupId=com.vam