Files
No1WMS/src/main/java/com/no1/wms/mypage/ImgService.java
2024-01-25 15:43:33 +09:00

82 lines
2.6 KiB
Java

package com.no1.wms.mypage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.no1.wms.excel.EgovWebUtil;
@Service
public class ImgService {
public void imgFileUplode(HttpServletRequest request, MultipartFile imageFile, String fileName) {
String storePathString = "";
try {
//대안1
//InputStream resource = getClass().getResourceAsStream("/static/img/mypage/profile");
//대안2
//InputStream resource = getClass().getClassLoader().getResourceAsStream("static/img/mypage/profile");
//기존
ClassPathResource resource = new ClassPathResource("/static/img/mypage/profile");
storePathString = resource.getFile().getAbsolutePath();
} 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 + ".jpg";//+ fileExt;
try {
// 동일한 파일명이 존재하는지 확인하고 있다면 기존 파일 삭제
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);
} catch (IOException e) {
e.printStackTrace();
}
}
}