diff --git a/src/main/java/com/no1/wms/mypage/ImgService.java b/src/main/java/com/no1/wms/mypage/ImgService.java index 86d5465..4812ad2 100644 --- a/src/main/java/com/no1/wms/mypage/ImgService.java +++ b/src/main/java/com/no1/wms/mypage/ImgService.java @@ -1,13 +1,11 @@ package com.no1.wms.mypage; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; -import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -16,20 +14,19 @@ import com.no1.wms.excel.EgovWebUtil; @Service public class ImgService { - - @Autowired - private ServletContext servletContext; public void imgFileUplode(HttpServletRequest request, MultipartFile imageFile, String fileName) { String storePathString = ""; try { - storePathString = servletContext.getRealPath("/img/mypage/profile/"); - System.out.println("storePathString : " + storePathString); - + ClassPathResource resource = new ClassPathResource("/static/img/mypage/profile"); + storePathString = resource.getFile().getAbsolutePath(); + //storePathString = System.getProperty("user.dir") + "/src/main/resources/static/img/mypage/profile"; + //System.out.println("storePathString : " + storePathString); //String path = ResourceUtils.getFile("classpath:static/img/mypage/profile/").toPath().toString(); - //String storePathString = path; + //storePathString = path; + //System.out.println("storePathString : " + storePathString); } catch (Exception e) { @@ -48,18 +45,29 @@ public class ImgService { String newFileName = fileName; // 저장될 파일 경로 설정 - //String filePath = storePathString + File.separator + newFileName + "." + fileExt; - String filePath = storePathString + newFileName + "." + fileExt; + String filePath = storePathString + File.separator + newFileName + "." + fileExt; + try { // 동일한 파일명이 존재하는지 확인하고 있다면 기존 파일 삭제 - File existingFile = new File(filePath); - if (existingFile.exists()) { - existingFile.delete(); - } - + String jpg = storePathString + File.separator + newFileName + "." + "jpg"; + String png = storePathString + File.separator + newFileName + "." + "png"; + String jpeg = storePathString + File.separator + newFileName + "." + "jpeg"; + File existingJpgFile = new File(jpg); + File existingPngFile = new File(png); + File existingJpegFile = new File(jpeg); + + String imgSrc = ""; + if(existingJpgFile.exists()) { + existingJpgFile.delete(); + }else if(existingPngFile.exists()) { + existingPngFile.delete(); + }else if(existingJpegFile.exists()) { + existingJpegFile.delete(); + } + // 이미지를 지정된 경로에 저장 imageFile.transferTo(new File(EgovWebUtil.filePathBlackList(filePath))); - System.out.println("Image file saved at: " + filePath); + //System.out.println("Image file saved at: " + filePath); } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/com/no1/wms/mypage/MypageController.java b/src/main/java/com/no1/wms/mypage/MypageController.java index cd77ecb..fd21e22 100644 --- a/src/main/java/com/no1/wms/mypage/MypageController.java +++ b/src/main/java/com/no1/wms/mypage/MypageController.java @@ -1,9 +1,13 @@ package com.no1.wms.mypage; +import java.io.File; +import java.io.IOException; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -34,7 +38,40 @@ public class MypageController { AccountDto list = accountService.selectById(dto); m.addAttribute("list", list); - + + String id = list.getId(); + + ClassPathResource resource = new ClassPathResource("/static/img/mypage/profile"); + String storePathString; + try { + storePathString = resource.getFile().getAbsolutePath(); + String jpg = storePathString + File.separator + id + "." + "jpg"; + String png = storePathString + File.separator + id + "." + "png"; + String jpeg = storePathString + File.separator + id + "." + "jpeg"; + File existingJpgFile = new File(jpg); + File existingPngFile = new File(png); + File existingJpegFile = new File(jpeg); + + String imgSrc = ""; + if(existingJpgFile.exists()) { + imgSrc = "/resources/static/img/mypage/profile/"+ id +".jpg"; + //System.out.println("jpg"); + }else if(existingPngFile.exists()) { + imgSrc = "/resources/static/img/mypage/profile/"+ id +".png"; + //System.out.println("png"); + }else if(existingJpegFile.exists()) { + imgSrc = "/resources/static/img/mypage/profile/"+ id +".jpeg"; + //System.out.println("jpeg"); + }else { + imgSrc = "/resources/static/img/mypage/profile/defaultimg.png"; + //System.out.println("default"); + } + m.addAttribute("imgSrc", imgSrc); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return "mypage/mypage"; } @@ -52,11 +89,11 @@ public class MypageController { @PostMapping("/mypage/uplodeImg") public String imgFileUplode(HttpServletRequest request, MultipartFile file) { - System.out.println(file); + //System.out.println(file); HttpSession session = request.getSession(); AccountDto dto = (AccountDto) session.getAttribute("userData"); String fileName = dto.getId(); - System.out.println(fileName); + //System.out.println(fileName); imgService.imgFileUplode(request, file, fileName); return "redirect:/mypage"; diff --git a/src/main/resources/static/img/mypage/defaultimg.png b/src/main/resources/static/img/mypage/profile/defaultimg.png similarity index 100% rename from src/main/resources/static/img/mypage/defaultimg.png rename to src/main/resources/static/img/mypage/profile/defaultimg.png diff --git a/src/main/webapp/WEB-INF/views/mypage/mypage.jsp b/src/main/webapp/WEB-INF/views/mypage/mypage.jsp index 829c1a9..458b289 100644 --- a/src/main/webapp/WEB-INF/views/mypage/mypage.jsp +++ b/src/main/webapp/WEB-INF/views/mypage/mypage.jsp @@ -16,8 +16,9 @@
- Default Image - + Default Image + +

${list.positionDto.name }   diff --git a/src/main/webapp/img/mypage/profile/15b68e54-b0f0-11ee-935d-0242ac110006.png b/src/main/webapp/img/mypage/profile/15b68e54-b0f0-11ee-935d-0242ac110006.png deleted file mode 100644 index cd971e6..0000000 Binary files a/src/main/webapp/img/mypage/profile/15b68e54-b0f0-11ee-935d-0242ac110006.png and /dev/null differ diff --git a/src/main/webapp/img/mypage/profile/defaultimg.png b/src/main/webapp/img/mypage/profile/defaultimg.png deleted file mode 100644 index 147064e..0000000 Binary files a/src/main/webapp/img/mypage/profile/defaultimg.png and /dev/null differ