하석형 하석형 03-20
250320 하석형 파일관련 throws Exception -> try catch 변경
@620cd491b7e3db665e8572419082756f5569dd69
src/main/java/com/takensoft/common/file/dao/FileDAO.java
--- src/main/java/com/takensoft/common/file/dao/FileDAO.java
+++ src/main/java/com/takensoft/common/file/dao/FileDAO.java
@@ -19,7 +19,7 @@
      *
      * 파일 등록
      */
-    public int fileInsert(FileVO fileVO) throws Exception;
+    public int fileInsert(FileVO fileVO);
 
     /**
      * @author 박정하
@@ -35,7 +35,7 @@
      *
      * 파일 삭제
      */
-    public int fileDelete(HashMap<String, Object> params) throws Exception;
+    public int fileDelete(HashMap<String, Object> params);
 
     /**
      * @author 방선주
@@ -43,5 +43,5 @@
      *
      * 파일 단일 조회
      */
-    public FileVO fileSelectOne(int fileId) throws Exception;
+    public FileVO fileSelectOne(int fileId);
 }
(파일 끝에 줄바꿈 문자 없음)
src/main/java/com/takensoft/common/file/dao/FileMngDAO.java
--- src/main/java/com/takensoft/common/file/dao/FileMngDAO.java
+++ src/main/java/com/takensoft/common/file/dao/FileMngDAO.java
@@ -20,7 +20,7 @@
      *
      * 파일매니저 등록
      */
-    public int fileMngInsert(FileMngVO fileMngVO) throws Exception;
+    public int fileMngInsert(FileMngVO fileMngVO);
 
     /**
      * @author 박정하
@@ -28,7 +28,7 @@
      *
      * 파일매니저 수정
      */
-    public int fileMngUpadate(FileMngVO fileMngVO) throws Exception;
+    public int fileMngUpadate(FileMngVO fileMngVO);
 
     /**
      * @author 박정하
@@ -36,5 +36,5 @@
      *
      * 파일매니저 삭제
      */
-    public int fileMngDelete(HashMap<String, Object> params) throws Exception;
+    public int fileMngDelete(HashMap<String, Object> params);
 }
(파일 끝에 줄바꿈 문자 없음)
src/main/java/com/takensoft/common/file/service/FileMngService.java
--- src/main/java/com/takensoft/common/file/service/FileMngService.java
+++ src/main/java/com/takensoft/common/file/service/FileMngService.java
@@ -18,7 +18,7 @@
      *
      * 파일매니저 등록
      */
-    public HashMap<String, Object> fileMngInsert(FileMngVO fileMngVO, List<MultipartFile> multipartFileList) throws Exception;
+    public HashMap<String, Object> fileMngInsert(FileMngVO fileMngVO, List<MultipartFile> multipartFileList) ;
 
     /**
      * @author 박정하
@@ -26,7 +26,7 @@
      *
      * 파일매니저 수정
      */
-    public int fileMngUpadate(FileMngVO fileMngVO, List<HashMap<String, Object>> deleteFileList, List<MultipartFile> multipartFileList) throws Exception;
+    public int fileMngUpadate(FileMngVO fileMngVO, List<HashMap<String, Object>> deleteFileList, List<MultipartFile> multipartFileList);
     
     /**
      * @author 박정하
@@ -34,5 +34,5 @@
      *
      * 파일매니저 삭제
      */
-    public int fileMngDelete(HashMap<String, Object> params) throws Exception;
+    public int fileMngDelete(HashMap<String, Object> params);
 }
(파일 끝에 줄바꿈 문자 없음)
src/main/java/com/takensoft/common/file/service/FileService.java
--- src/main/java/com/takensoft/common/file/service/FileService.java
+++ src/main/java/com/takensoft/common/file/service/FileService.java
@@ -19,7 +19,7 @@
      *
      * 파일 등록
      */
-    public int fileInsert(String fileMngId, List<MultipartFile> multipartFileList) throws Exception;
+    public int fileInsert(String fileMngId, List<MultipartFile> multipartFileList) ;
 
     /**
      * @author 박정하
@@ -35,14 +35,14 @@
      *
      * 파일 삭제
      */
-    public int fileDelete(List<HashMap<String, Object>> fileList) throws Exception;
+    public int fileDelete(List<HashMap<String, Object>> fileList);
     /**
      * @author 방선주
      * @since 2024.05.21
      *
      * 파일 단일 조회
      */
-    public FileVO fileSelectOne(int fileId) throws Exception;
+    public FileVO fileSelectOne(int fileId);
 
-    public String editorUploadImg(MultipartFile file) throws Exception;
+    public String editorUploadImg(MultipartFile file);
 }
(파일 끝에 줄바꿈 문자 없음)
src/main/java/com/takensoft/common/file/service/Impl/FileMngServiceImpl.java
--- src/main/java/com/takensoft/common/file/service/Impl/FileMngServiceImpl.java
+++ src/main/java/com/takensoft/common/file/service/Impl/FileMngServiceImpl.java
@@ -1,5 +1,6 @@
 package com.takensoft.common.file.service.Impl;
 
+import com.takensoft.common.exception.*;
 import com.takensoft.common.file.dao.FileMngDAO;
 import com.takensoft.common.file.service.FileMngService;
 import com.takensoft.common.file.service.FileService;
@@ -7,7 +8,9 @@
 import com.takensoft.common.idgen.service.IdgenService;
 import com.takensoft.common.util.JWTUtil;
 import lombok.RequiredArgsConstructor;
+import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
 import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
+import org.springframework.dao.DataAccessException;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -15,12 +18,17 @@
 import java.util.*;
 
 /**
- * @author  : 박정하
- * @since   : 2024.03.26
+ * @author 박정하
+ * @since 2024.03.26
+ * @modification
+ *     since    |    author    | description
+ *  2024.03.26  |    박정하     | 최초 등록
+ *  2024.03.27  |    박정하     | fileMngUpadate, fileMngDelete 추가
  *
- * 파일매니저 관련 구현체
  * EgovAbstractServiceImpl : 전자정부 상속
- * FileMngService : 검토사항 인터페이스 상속
+ * FileMngService : 파일매니저 관련 인터페이스 상속
+ *
+ * 파일매니저 관련 인터페이스 구현체
  */
 @Service("fileMngService")
 @RequiredArgsConstructor
@@ -31,13 +39,19 @@
     private final JWTUtil jwtUtil;
 
     /**
-     * @author 박정하
-     * @since 2024.03.26
+     * @param fileMngVO - 파일매니저 정보
+     * @param multipartFileList - 파일 목록
+     * @return HashMap<String, Object>
+     *  - fileInsertResult : 파일 등록 결과
+     *  - fileMngId : 파일매니저 아이디
+     *  - fileMngResult : 파일매니저 등록 결과
+     * @throws CustomNotFoundException - 사용자 정보 조회 예외 발생 시
+     * @throws CustomInsertFailException - 파일매니저 등록 예외 발생 시
      *
      * 파일매니저 등록
      */
     @Override
-    public HashMap<String, Object> fileMngInsert(FileMngVO fileMngVO, List<MultipartFile> multipartFileList) throws Exception {
+    public HashMap<String, Object> fileMngInsert(FileMngVO fileMngVO, List<MultipartFile> multipartFileList) {
         HashMap<String, Object> result = new HashMap<>();
         result.put("fileInsertResult", 0);
 
@@ -47,89 +61,92 @@
 
         // 작성자 생성
         String writer = jwtUtil.getWriter();
+        if (writer == null || writer.isEmpty()) {
+            throw new CustomNotFoundException("사용자 정보 조회에 실패했습니다.");
+        }
         fileMngVO.setRgtr(writer);
 
         // 파일 등록
-        try {
-            result.put("fileInsertResult", fileService.fileInsert(fileMngId, multipartFileList));
-        } catch (IOException e) {
-            e.printStackTrace();
-            return result;
-        }
+        result.put("fileInsertResult", fileService.fileInsert(fileMngId, multipartFileList));
 
         // 파일매니저 아이디 삽입
         result.put("fileMngId", fileMngId);
 
         // DB 등록
-        result.put("fileMngResult", fileMngDAO.fileMngInsert(fileMngVO));
+        int fileMngResult = fileMngDAO.fileMngInsert(fileMngVO);
+        if (fileMngResult == 0) {
+            throw new CustomInsertFailException("파일매니저 등록에 실패했습니다.");
+        }
+        result.put("fileMngResult", fileMngResult);
 
         return result;
     }
 
     /**
-     * @author 박정하
-     * @since 2024.03.27
+     * @param fileMngVO - 파일매니저 정보
+     * @param deleteFileList - 삭제 파일 목록
+     * @param multipartFileList - 파일 목록
+     * @return int - 파일매니저 수정 결과
+     * @throws CustomNotFoundException - 사용자 정보 조회 예외 발생 시
+     * @throws CustomUpdateFailException - 파일매니저 수정 예외 발생 시
      *
      * 파일매니저 수정
      */
     @Override
-    public int fileMngUpadate(FileMngVO fileMngVO, List<HashMap<String, Object>> deleteFileList, List<MultipartFile> multipartFileList) throws Exception {
+    public int fileMngUpadate(FileMngVO fileMngVO, List<HashMap<String, Object>> deleteFileList, List<MultipartFile> multipartFileList) {
+        int insertResult = 0;
+        int deleteResult = 0;
         int result = 0;
 
         // 수정자 생성
         String writer = jwtUtil.getWriter();
+        if(writer == null || writer.isEmpty()){
+            throw new CustomNotFoundException("사용자 정보 조회에 실패했습니다.");
+        }
         fileMngVO.setMdfr(writer);
 
         // 파일 추가
-        if(multipartFileList != null && multipartFileList.size() > 0) {
+        if (multipartFileList != null && multipartFileList.size() > 0) {
             // DB 등록
-            try {
-                String fileMngId =fileMngVO.getFileMngId();
-                result += fileService.fileInsert(fileMngId, multipartFileList);
-            } catch (IOException e) {
-                e.printStackTrace();
-                return result;
-            }
+            String fileMngId = fileMngVO.getFileMngId();
+            insertResult = fileService.fileInsert(fileMngId, multipartFileList);
         }
-        
+
         // 파일 삭제
-        if(deleteFileList != null && deleteFileList.size() > 0) {
-            try {
-                result += fileService.fileDelete(deleteFileList);
-            } catch (IOException e) {
-                e.printStackTrace();
-                return result;
-            }
+        if (deleteFileList != null && deleteFileList.size() > 0) {
+            deleteResult = fileService.fileDelete(deleteFileList);
         }
-        
+
         // DB 등록
-        result += fileMngDAO.fileMngUpadate(fileMngVO);
+        result = fileMngDAO.fileMngUpadate(fileMngVO);
+        if(result == 0){
+            throw new CustomUpdateFailException("파일매니저 수정에 실패했습니다.");
+        }
 
         return result;
     }
 
-
     /**
-     * @author 박정하
-     * @since 2024.03.27
+     * @param params - 파일매니저 아이디
+     * @return int - 파일매니저 삭제 결과
+     * @throws CustomDeleteFailException - 파일매니저 삭제 예외 발생 시
      *
      * 파일매니저 삭제
      */
     @Override
-    public int fileMngDelete(HashMap<String, Object> params) throws Exception {
+    public int fileMngDelete(HashMap<String, Object> params) {
+        int deleteResult = 0;
         int result = 0;
 
         // 파일 삭제
-        try {
-            List<HashMap<String, Object>> fileList = fileService.fileSelectList(params.get("fileMngId").toString());
-            result += fileService.fileDelete(fileList);
-        } catch (IOException e) {
-            e.printStackTrace();
-            return result;
-        }
+        List<HashMap<String, Object>> fileList = fileService.fileSelectList(params.get("fileMngId").toString());
+        deleteResult = fileService.fileDelete(fileList);
 
         // DB 등록
-        result += fileMngDAO.fileMngDelete(params);
+        result = fileMngDAO.fileMngDelete(params);
+        if(result == 0){
+            throw new CustomDeleteFailException("파일매니저 삭제에 실패했습니다.");
+        }
 
         return result;
     }
src/main/java/com/takensoft/common/file/service/Impl/FileServiceImpl.java
--- src/main/java/com/takensoft/common/file/service/Impl/FileServiceImpl.java
+++ src/main/java/com/takensoft/common/file/service/Impl/FileServiceImpl.java
@@ -1,10 +1,16 @@
 package com.takensoft.common.file.service.Impl;
 
+import com.takensoft.common.exception.CustomDeleteFailException;
+import com.takensoft.common.exception.CustomFileUploadFailException;
+import com.takensoft.common.exception.CustomInsertFailException;
+import com.takensoft.common.exception.CustomNotFoundException;
 import com.takensoft.common.file.dao.FileDAO;
 import com.takensoft.common.file.service.FileService;
 import com.takensoft.common.file.vo.FileVO;
+import com.takensoft.common.util.FileUtil;
 import com.takensoft.common.util.JWTUtil;
 import lombok.RequiredArgsConstructor;
+import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
 import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.dao.DataAccessException;
@@ -18,6 +24,22 @@
 import java.util.*;
 import java.util.UUID;
 
+
+/**
+ * @author 박정하
+ * @since 2024.03.26
+ * @modification
+ *     since    |    author    | description
+ *  2024.03.26  |    박정하     | 최초 등록
+ *  2024.03.27  |    박정하     | fileDelete 추가
+ *  2024.05.21  |    방선주     | fileSelectOne, editorUploadImg 추가
+ *  2025.03.20  |    하석형     | 코드 리펙토링
+ *
+ * EgovAbstractServiceImpl : 전자정부 상속
+ * FileService : 파일 관련 인터페이스 상속
+ * 
+ * 파일 관련 인터페이스 구현체
+ */
 @Service("fileService")
 @RequiredArgsConstructor
 public class FileServiceImpl extends EgovAbstractServiceImpl implements FileService {
@@ -40,81 +62,97 @@
     @Value("${file.edit-file-upload-path}")
     private String editPath;
 
+    @Value("${spring.servlet.multipart.max-file-size}")
+    private String maxFileSize;
+
     /**
-     * @author 박정하
-     * @since 2024.03.26
-     * <p>
+     * @param fileMngId - 파일관리아이디
+     * @param multipartFileList - 파일 목록
+     * @return int - 파일 등록 결과
+     * @throws RuntimeException - 파일 크기 초과 예외 발생 시
+     * @throws CustomInsertFailException - 파일 등록 예외 발생 시
+     * @throws CustomNotFoundException - 사용자 정보 조회 예외 발생 시
+     * @throws CustomFileUploadFailException - 파일 업로드 예외 발생 시
+     * @throws IllegalArgumentException - 잘못된 인자 예외 발생 시
+     * @throws DataAccessException - db 관련 예외 발생 시
+     * @throws Exception - 그 외 예외 발생 시
+     *
      * 파일 등록
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public int fileInsert(String fileMngId, List<MultipartFile> multipartFileList) throws Exception {
-        int result = 0;
+    public int fileInsert(String fileMngId, List<MultipartFile> multipartFileList) {
+        try {
+            int result = 0;
 
-        for (MultipartFile file : multipartFileList) {
-            if (file.isEmpty()) {
-                continue;
-            }
+            for (MultipartFile file : multipartFileList) {
+                if (file.isEmpty()) {
+                    continue;
+                }
 
-            int size = Long.valueOf(file.getSize()).intValue() / 100000;
-            if (size > 200) {
-                return -1;
-            }
+                int size = Long.valueOf(file.getSize()).intValue() / 100000;
+                if (size > FileUtil.sizeStrToBytes(maxFileSize)) {
+                    throw new RuntimeException("파일 크기가 " + maxFileSize + "를 초과했습니다. (" + size + "MB)");
+//                    throw new FileSizeLimitExceededException("파일 크기가 " + maxFileSize + "를 초과했습니다. (" + size + "MB)", file.getSize(), FileUtil.sizeStrToBytes(maxFileSize));
+//                return -1;
+                }
 
-            // 절대 경로 생성
-            // "file:///" 제거하여 실제 파일 시스템 경로만을 저장
-            String uploadPath = null;
-//            String[] pathArray = locationPath.split("/, ");
-//            for (String path : pathArray) {
-//                if (path.startsWith("file:///")) {
-//                    // "file:///" 부분을 제거하고 경로를 반환
-//                    uploadPath = path.replace("file:///", "") + fileUploadPath;
-//                }
-//            }
-            uploadPath = locationPath + fileUploadPath;
+                // 절대 경로 생성
+                String uploadPath = null;
+                uploadPath = locationPath + fileUploadPath;
 
-            // 파일 정보 가공
-            String fileName = file.getOriginalFilename(); // 파일명 (원본)
-            String fileNm = fileName.substring(0, fileName.lastIndexOf(".")); // 파일명
-            String maskNm = UUID.randomUUID().toString().replaceAll("-", ""); // 마스크명
-            String extsn = StringUtils.getFilenameExtension(fileName); // 확장자
-            String mg = Long.toString(file.getSize()); // 크기
-            String absltPath = makeDirectories(uploadPath) + File.separator + maskNm + "." + extsn; // 절대경로
-            String partPath = File.separator + maskNm + "." + extsn; // 일부경로
+                // 파일 정보 가공
+                String fileName = file.getOriginalFilename(); // 파일명 (원본)
+                String fileNm = fileName.substring(0, fileName.lastIndexOf(".")); // 파일명
+                String maskNm = UUID.randomUUID().toString().replaceAll("-", ""); // 마스크명
+                String extsn = StringUtils.getFilenameExtension(fileName); // 확장자
+                String mg = Long.toString(file.getSize()); // 크기
+                String absltPath = makeDirectories(uploadPath) + File.separator + maskNm + "." + extsn; // 절대경로
+                String partPath = File.separator + maskNm + "." + extsn; // 일부경로
 
-            // FileVO 생성
-            FileVO fileVO = new FileVO();
-            fileVO.setFileMngId(fileMngId); // 파일관리아이디
-            fileVO.setFileNm(fileNm); // 파일명
-            fileVO.setMaskNm(maskNm); // 마스크명
-            // 파일타입 추후에 추가
-            fileVO.setAbsltPath(absltPath); // 절대경로
-            fileVO.setPartPath(partPath); // 일부경로
-            fileVO.setExtnNm(extsn); // 확장자
-            fileVO.setFileSz(mg); // 크기
-            // 등록자 생성
-            String writer = jwtUtil.getWriter();
-            fileVO.setRgtr(writer);
+                // FileVO 생성
+                FileVO fileVO = new FileVO();
+                fileVO.setFileMngId(fileMngId); // 파일관리아이디
+                fileVO.setFileNm(fileNm); // 파일명
+                fileVO.setMaskNm(maskNm); // 마스크명
+                // 파일타입 추후에 추가
+                fileVO.setAbsltPath(absltPath); // 절대경로
+                fileVO.setPartPath(partPath); // 일부경로
+                fileVO.setExtnNm(extsn); // 확장자
+                fileVO.setFileSz(mg); // 크기
+                // 등록자 생성
+                String writer = jwtUtil.getWriter();
+                if(writer == null || writer.isEmpty()){
+                    throw new CustomNotFoundException("사용자 정보 조회에 실패했습니다.");
+                }
+                fileVO.setRgtr(writer);
 
-            try {
+                // 파일 업로드
                 try {
-                    // 파일 업로드
                     File uploadFile = new File(absltPath);
                     file.transferTo(uploadFile);
-                } catch (IOException e) {
-                    e.printStackTrace();
-                    return 0;
+                } catch (IOException ioe) {
+                    throw new CustomFileUploadFailException("파일 업로드에 실패했습니다.", ioe);
+//                    e.printStackTrace();
+//                    return 0;
                 }
 
                 // DB등록
                 result += fileDAO.fileInsert(fileVO);
-            } catch (IOException e) {
-                e.printStackTrace();
-                return 0;
+                if (result == 0) {
+                    throw new CustomInsertFailException("파일 등록에 실패했습니다.");
+//                return 0;
+                }
             }
-        }
 
-        return result;
+            return result;
+        } catch (IllegalArgumentException iae) {
+            throw iae;
+        } catch (DataAccessException dae) {
+            throw dae;
+        } catch (Exception e) {
+            throw e;
+        }
     }
 
     // 업로드 폴더(디렉터리) 생성
@@ -127,9 +165,11 @@
     }
 
     /**
-     * @author 박정하
-     * @since 2024.03.26
-     * <p>
+     * @param fileMngId - 파일관리아이디
+     * @return List<HashMap<String, Object>> - 파일 내용 정보를 담고 있는 객체 목록
+     * @throws DataAccessException - db 관련 예외 발생 시
+     * @throws Exception - 그 외 예외 발생 시
+     *
      * 파일 목록 조회
      */
     @Override
@@ -144,33 +184,32 @@
     }
 
     /**
-     * @author 박정하
-     * @since 2024.03.27
-     * <p>
+     * @param fileList - 파일 목록
+     * @return int - 파일 삭제 결과
+     * @throws CustomDeleteFailException - 파일 삭제 예외 발생 시
+     * @throws DataAccessException - db 관련 예외 발생 시
+     * @throws Exception - 그 외 예외 발생 시
+     *
      * 파일 삭제
      */
-    public int fileDelete(List<HashMap<String, Object>> fileList) throws Exception {
-        int result = 0;
+    public int fileDelete(List<HashMap<String, Object>> fileList) {
+        try {
+            int result = 0;
 
-        for (HashMap<String, Object> file : fileList) {
-            if (file.isEmpty()) {
-                continue;
-            }
+            for (HashMap<String, Object> file : fileList) {
+                if (file.isEmpty()) {
+                    continue;
+                }
 
-            try {
                 // DB 삭제
                 result += fileDAO.fileDelete(file);
+                if(result == 0){
+                    throw new CustomDeleteFailException("파일 삭제에 실패했습니다.");
+                }
 
                 // http 부분
                 String url = frontUrl + ":" + serverPort;
                 String uploadPath = null;
-//                String[] pathArray = locationPath.split("/, ");
-//                for (String path : pathArray) {
-//                    if (path.startsWith("file:///")) {
-//                        // "file:///" 부분을 제거하고 경로를 반환
-//                        uploadPath = path.replace("file:///", "") + fileUploadPath;
-//                    }
-//                }
                 uploadPath = locationPath + fileUploadPath;
 
                 String path = file.get("absltPath").toString();
@@ -178,65 +217,76 @@
 
                 // Disk 삭제
                 File newFile = new File(path);
-                
-
 //                newFile = newFile.replace(url, uploadPath);
                 if (newFile.exists()) {
                     newFile.delete();
                 }
-            } catch (IOException e) {
-                e.printStackTrace();
             }
-        }
 
-        return result;
+            return result;
+        } catch (DataAccessException dae) {
+            throw dae;
+        } catch (Exception e) {
+            throw e;
+        }
     }
 
     /**
-     * @author 방선주
-     * @since 2024.05.21
-     * <p>
+     * @param fileId - 파일 아이디
+     * @return FileVO - 파일 내용 정보를 담고 있는 객체
+     * @throws DataAccessException - db 관련 예외 발생 시
+     * @throws Exception - 그 외 예외 발생 시
+     *
      * 파일 단일 조회
      */
     @Override
-    public FileVO fileSelectOne(int fileId) throws Exception {
-        return fileDAO.fileSelectOne(fileId);
+    public FileVO fileSelectOne(int fileId) {
+        try {
+            return fileDAO.fileSelectOne(fileId);
+        } catch (DataAccessException dae) {
+            throw dae;
+        } catch (Exception e) {
+            throw e;
+        }
     }
 
+    /**
+     * @param file - 파일 정보
+     * @return String - 이미지 경로
+     * @throws CustomFileUploadFailException - 파일 업로드 예외 발생 시
+     * @throws DataAccessException - db 관련 예외 발생 시
+     * @throws Exception - 그 외 예외 발생 시
+     *
+     * 에디터 이미지 업로드
+     */
     @Override
-    public String editorUploadImg(MultipartFile file) throws Exception {
-        MultipartFile uploadFile = file;
-        // 파일 명 생성
-        String originalFileName = uploadFile.getOriginalFilename();
-        String ext = originalFileName.substring(originalFileName.lastIndexOf("."));
-        String maskNm = UUID.randomUUID() + ext; // 랜덤으로 파일명 생성
-        // 파일 저장 경로
-        String uploadPath = null;
-//        String[] pathArray = locationPath.split(", ");
-//        for (String path : pathArray) {
-//            if (path.startsWith("file:///")) {
-//                // "file:///" 부분을 제거하고 경로를 반환
-//                uploadPath = path.replace("file:///", "") + editPath;
-//                break; // 루프를 종료하여 첫 번째 일치하는 경로만 사용
-//            }
-//        }
-//        if (uploadPath == null) {
-//            throw new Exception("Upload path is not properly configured.");
-//        }
-        uploadPath = "/home/cloud-user/front-end/uploadFiles" + editPath;
-        String savePath = uploadPath + maskNm;
-        // 파일 저장
+    public String editorUploadImg(MultipartFile file) {
         try {
-            File saveFile = new File(savePath);
-            if (!saveFile.exists()) {
-                saveFile.mkdirs();
+            MultipartFile uploadFile = file;
+            // 파일 명 생성
+            String originalFileName = uploadFile.getOriginalFilename();
+            String ext = originalFileName.substring(originalFileName.lastIndexOf("."));
+            String maskNm = UUID.randomUUID() + ext; // 랜덤으로 파일명 생성
+            // 파일 저장 경로
+            String uploadPath = null;
+            uploadPath = "/home/cloud-user/front-end/uploadFiles" + editPath;
+            String savePath = uploadPath + maskNm;
+            // 파일 저장
+            try {
+                File saveFile = new File(savePath);
+                if (!saveFile.exists()) {
+                    saveFile.mkdirs();
+                }
+                uploadFile.transferTo(saveFile);
+            } catch (IOException ioe) {
+                throw new CustomFileUploadFailException("파일 업로드에 실패했습니다.", ioe);
             }
-            uploadFile.transferTo(saveFile);
+            String imgPath = frontUrl + "/uploadFiles" + editPath + maskNm;
+            return imgPath;
+        } catch (DataAccessException dae) {
+            throw dae;
         } catch (Exception e) {
-            e.printStackTrace();
-            throw new Exception("File upload failed.");
+            throw e;
         }
-        String imgPath = frontUrl + "/uploadFiles" + editPath + maskNm;
-        return imgPath;
     }
 }
(파일 끝에 줄바꿈 문자 없음)
 
src/main/java/com/takensoft/common/util/FileUtil.java (added)
+++ src/main/java/com/takensoft/common/util/FileUtil.java
@@ -0,0 +1,57 @@
+package com.takensoft.common.util;
+
+import org.springframework.stereotype.Component;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+/**
+ * @author 하석형
+ * @since 2025.03.13
+ * @modification
+ *     since    |    author    | description
+ *  2025.03.13  |    하석형     | 최초 등록
+ *
+ * 파일 관련 RestController
+ */
+@Component
+public class FileUtil {
+
+    /**
+     * @param sizeStr - 파일사이즈(문자열)
+     * @return long - 파일사이즈(바이트)
+     *
+     * 단위가 포함된 파일사이즈(문자열)를 바이트로 변환
+     */
+    public static long sizeStrToBytes(String sizeStr) {
+        if (sizeStr == null || sizeStr.isEmpty()) {
+            throw new IllegalArgumentException("입력된 파일 크기 값이 올바르지 않습니다.");
+        }
+
+        // 숫자 부분과 단위 부분 분리
+        sizeStr = sizeStr.toUpperCase().trim(); // 대문자로 변환 후 공백 제거
+        String numberPart = sizeStr.replaceAll("[^0-9.]", ""); // 숫자만 추출 (숫자 및 '.'를 제외한 문자 제거)
+        String unitPart = sizeStr.replaceAll("[0-9.]", "").trim(); // 단위만 추출 (숫자 및 '.' 제거)
+
+        // 숫자 값을 double로 변환
+        double sizeValue = Double.parseDouble(numberPart);
+
+        // 단위에 따라 바이트 변환
+        switch (unitPart) {
+            case "B":
+                return (long) sizeValue; // 바이트 그대로
+            case "KB":
+                return (long) (sizeValue * 1024);
+            case "MB":
+                return (long) (sizeValue * 1024 * 1024);
+            case "GB":
+                return (long) (sizeValue * 1024 * 1024 * 1024);
+            case "TB":
+                return (long) (sizeValue * 1024 * 1024 * 1024 * 1024);
+            case "PB":
+                return (long) (sizeValue * 1024 * 1024 * 1024 * 1024 * 1024);
+            default:
+                throw new IllegalArgumentException("지원하지 않는 파일 크기 단위: " + unitPart);
+        }
+    }
+}
Add a comment
List