이미지 업로드 관련

This commit is contained in:
Kana
2024-01-17 12:12:13 +09:00
parent d51668f3ec
commit 51d58cac5d
5 changed files with 123 additions and 12 deletions

View File

@@ -0,0 +1,69 @@
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.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
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);
//String path = ResourceUtils.getFile("classpath:static/img/mypage/profile/").toPath().toString();
//String storePathString = path;
//System.out.println("storePathString : " + storePathString);
} catch (Exception e) {
e.printStackTrace();
}
File saveFolder = new File(EgovWebUtil.filePathBlackList(storePathString));
//폴더 없으면 생성
if (!saveFolder.exists() || saveFolder.isFile()) {
saveFolder.mkdirs();
}
// 원본 파일 이름 가져오기
String originalFileName = imageFile.getOriginalFilename();
// 확장자 추출
int index = originalFileName.lastIndexOf(".");
String fileExt = originalFileName.substring(index + 1);
String newFileName = fileName;
// 저장될 파일 경로 설정
//String filePath = storePathString + File.separator + newFileName + "." + fileExt;
String filePath = storePathString + newFileName + "." + fileExt;
try {
// 동일한 파일명이 존재하는지 확인하고 있다면 기존 파일 삭제
File existingFile = new File(filePath);
if (existingFile.exists()) {
existingFile.delete();
}
// 이미지를 지정된 경로에 저장
imageFile.transferTo(new File(EgovWebUtil.filePathBlackList(filePath)));
System.out.println("Image file saved at: " + filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -6,10 +6,13 @@ import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.no1.wms.account.AccountDto;
import com.no1.wms.account.AccountService;
@@ -19,21 +22,22 @@ public class MypageController {
@Autowired
AccountService accountService;
@Autowired
ImgService imgService;
@GetMapping("/mypage")
public String mypage(Model m, HttpServletRequest request) {
HttpSession session = request.getSession();
AccountDto dto = (AccountDto) session.getAttribute("userData");
AccountDto list = accountService.selectById(dto);
m.addAttribute("list", list);
return "mypage/mypage";
}
@ResponseBody
@PutMapping("/mypage/updateUserInfo")
public boolean updateUserInfo(AccountDto dto) {
@@ -46,6 +50,17 @@ public class MypageController {
}
}
@PostMapping("/mypage/uplodeImg")
public String imgFileUplode(HttpServletRequest request, MultipartFile file) {
System.out.println(file);
HttpSession session = request.getSession();
AccountDto dto = (AccountDto) session.getAttribute("userData");
String fileName = dto.getId();
System.out.println(fileName);
imgService.imgFileUplode(request, file, fileName);
return "redirect:/mypage";
}
}