마이페이지 프로필사진 수정기능 완료

This commit is contained in:
Kana
2024-01-17 13:39:44 +09:00
parent 51d58cac5d
commit 711bc207cd
6 changed files with 74 additions and 23 deletions

View File

@@ -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,24 @@ 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 {
//장원형님 아이디어
ClassPathResource resource = new ClassPathResource("/static/img/mypage/profile");
storePathString = resource.getFile().getAbsolutePath();
//System.out.println("storePathString : " + storePathString);
storePathString = servletContext.getRealPath("/img/mypage/profile/");
System.out.println("storePathString : " + storePathString);
//되는거
//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 +50,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();

View File

@@ -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";

View File

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 58 KiB

View File

@@ -16,8 +16,9 @@
<div class="container-fluid">
<div class="row">
<div class="col-md-4" style="text-align: center;">
<img alt="Default Image" src="/main/webapp/img/mypage/profile/defaultimg.png" width="200" />
<!-- <img alt="Default Image" src="/resources/static/img/mypage/defaultimg.png" width="200" /> -->
<img alt="Default Image" src="${imgSrc}" width="200" />
<!-- <img alt="Default Image" src="/resources/static/img/mypage/profile/${list.id}.png" width="200" /> -->
<!-- -->
<hr>
<p><b>
<span style="color: #6E6ED7; font-size: 20px;">${list.positionDto.name }&nbsp;&nbsp; </span>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 764 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB