
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
... | ... | @@ -19,7 +19,7 @@ |
19 | 19 |
* |
20 | 20 |
* 파일 등록 |
21 | 21 |
*/ |
22 |
- public int fileInsert(FileVO fileVO) throws Exception; |
|
22 |
+ public int fileInsert(FileVO fileVO); |
|
23 | 23 |
|
24 | 24 |
/** |
25 | 25 |
* @author 박정하 |
... | ... | @@ -35,7 +35,7 @@ |
35 | 35 |
* |
36 | 36 |
* 파일 삭제 |
37 | 37 |
*/ |
38 |
- public int fileDelete(HashMap<String, Object> params) throws Exception; |
|
38 |
+ public int fileDelete(HashMap<String, Object> params); |
|
39 | 39 |
|
40 | 40 |
/** |
41 | 41 |
* @author 방선주 |
... | ... | @@ -43,5 +43,5 @@ |
43 | 43 |
* |
44 | 44 |
* 파일 단일 조회 |
45 | 45 |
*/ |
46 |
- public FileVO fileSelectOne(int fileId) throws Exception; |
|
46 |
+ public FileVO fileSelectOne(int fileId); |
|
47 | 47 |
}(파일 끝에 줄바꿈 문자 없음) |
--- src/main/java/com/takensoft/common/file/dao/FileMngDAO.java
+++ src/main/java/com/takensoft/common/file/dao/FileMngDAO.java
... | ... | @@ -20,7 +20,7 @@ |
20 | 20 |
* |
21 | 21 |
* 파일매니저 등록 |
22 | 22 |
*/ |
23 |
- public int fileMngInsert(FileMngVO fileMngVO) throws Exception; |
|
23 |
+ public int fileMngInsert(FileMngVO fileMngVO); |
|
24 | 24 |
|
25 | 25 |
/** |
26 | 26 |
* @author 박정하 |
... | ... | @@ -28,7 +28,7 @@ |
28 | 28 |
* |
29 | 29 |
* 파일매니저 수정 |
30 | 30 |
*/ |
31 |
- public int fileMngUpadate(FileMngVO fileMngVO) throws Exception; |
|
31 |
+ public int fileMngUpadate(FileMngVO fileMngVO); |
|
32 | 32 |
|
33 | 33 |
/** |
34 | 34 |
* @author 박정하 |
... | ... | @@ -36,5 +36,5 @@ |
36 | 36 |
* |
37 | 37 |
* 파일매니저 삭제 |
38 | 38 |
*/ |
39 |
- public int fileMngDelete(HashMap<String, Object> params) throws Exception; |
|
39 |
+ public int fileMngDelete(HashMap<String, Object> params); |
|
40 | 40 |
}(파일 끝에 줄바꿈 문자 없음) |
--- src/main/java/com/takensoft/common/file/service/FileMngService.java
+++ src/main/java/com/takensoft/common/file/service/FileMngService.java
... | ... | @@ -18,7 +18,7 @@ |
18 | 18 |
* |
19 | 19 |
* 파일매니저 등록 |
20 | 20 |
*/ |
21 |
- public HashMap<String, Object> fileMngInsert(FileMngVO fileMngVO, List<MultipartFile> multipartFileList) throws Exception; |
|
21 |
+ public HashMap<String, Object> fileMngInsert(FileMngVO fileMngVO, List<MultipartFile> multipartFileList) ; |
|
22 | 22 |
|
23 | 23 |
/** |
24 | 24 |
* @author 박정하 |
... | ... | @@ -26,7 +26,7 @@ |
26 | 26 |
* |
27 | 27 |
* 파일매니저 수정 |
28 | 28 |
*/ |
29 |
- public int fileMngUpadate(FileMngVO fileMngVO, List<HashMap<String, Object>> deleteFileList, List<MultipartFile> multipartFileList) throws Exception; |
|
29 |
+ public int fileMngUpadate(FileMngVO fileMngVO, List<HashMap<String, Object>> deleteFileList, List<MultipartFile> multipartFileList); |
|
30 | 30 |
|
31 | 31 |
/** |
32 | 32 |
* @author 박정하 |
... | ... | @@ -34,5 +34,5 @@ |
34 | 34 |
* |
35 | 35 |
* 파일매니저 삭제 |
36 | 36 |
*/ |
37 |
- public int fileMngDelete(HashMap<String, Object> params) throws Exception; |
|
37 |
+ public int fileMngDelete(HashMap<String, Object> params); |
|
38 | 38 |
}(파일 끝에 줄바꿈 문자 없음) |
--- src/main/java/com/takensoft/common/file/service/FileService.java
+++ src/main/java/com/takensoft/common/file/service/FileService.java
... | ... | @@ -19,7 +19,7 @@ |
19 | 19 |
* |
20 | 20 |
* 파일 등록 |
21 | 21 |
*/ |
22 |
- public int fileInsert(String fileMngId, List<MultipartFile> multipartFileList) throws Exception; |
|
22 |
+ public int fileInsert(String fileMngId, List<MultipartFile> multipartFileList) ; |
|
23 | 23 |
|
24 | 24 |
/** |
25 | 25 |
* @author 박정하 |
... | ... | @@ -35,14 +35,14 @@ |
35 | 35 |
* |
36 | 36 |
* 파일 삭제 |
37 | 37 |
*/ |
38 |
- public int fileDelete(List<HashMap<String, Object>> fileList) throws Exception; |
|
38 |
+ public int fileDelete(List<HashMap<String, Object>> fileList); |
|
39 | 39 |
/** |
40 | 40 |
* @author 방선주 |
41 | 41 |
* @since 2024.05.21 |
42 | 42 |
* |
43 | 43 |
* 파일 단일 조회 |
44 | 44 |
*/ |
45 |
- public FileVO fileSelectOne(int fileId) throws Exception; |
|
45 |
+ public FileVO fileSelectOne(int fileId); |
|
46 | 46 |
|
47 |
- public String editorUploadImg(MultipartFile file) throws Exception; |
|
47 |
+ public String editorUploadImg(MultipartFile file); |
|
48 | 48 |
}(파일 끝에 줄바꿈 문자 없음) |
--- 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 @@ |
1 | 1 |
package com.takensoft.common.file.service.Impl; |
2 | 2 |
|
3 |
+import com.takensoft.common.exception.*; |
|
3 | 4 |
import com.takensoft.common.file.dao.FileMngDAO; |
4 | 5 |
import com.takensoft.common.file.service.FileMngService; |
5 | 6 |
import com.takensoft.common.file.service.FileService; |
... | ... | @@ -7,7 +8,9 @@ |
7 | 8 |
import com.takensoft.common.idgen.service.IdgenService; |
8 | 9 |
import com.takensoft.common.util.JWTUtil; |
9 | 10 |
import lombok.RequiredArgsConstructor; |
11 |
+import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException; |
|
10 | 12 |
import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl; |
13 |
+import org.springframework.dao.DataAccessException; |
|
11 | 14 |
import org.springframework.stereotype.Service; |
12 | 15 |
import org.springframework.web.multipart.MultipartFile; |
13 | 16 |
|
... | ... | @@ -15,12 +18,17 @@ |
15 | 18 |
import java.util.*; |
16 | 19 |
|
17 | 20 |
/** |
18 |
- * @author : 박정하 |
|
19 |
- * @since : 2024.03.26 |
|
21 |
+ * @author 박정하 |
|
22 |
+ * @since 2024.03.26 |
|
23 |
+ * @modification |
|
24 |
+ * since | author | description |
|
25 |
+ * 2024.03.26 | 박정하 | 최초 등록 |
|
26 |
+ * 2024.03.27 | 박정하 | fileMngUpadate, fileMngDelete 추가 |
|
20 | 27 |
* |
21 |
- * 파일매니저 관련 구현체 |
|
22 | 28 |
* EgovAbstractServiceImpl : 전자정부 상속 |
23 |
- * FileMngService : 검토사항 인터페이스 상속 |
|
29 |
+ * FileMngService : 파일매니저 관련 인터페이스 상속 |
|
30 |
+ * |
|
31 |
+ * 파일매니저 관련 인터페이스 구현체 |
|
24 | 32 |
*/ |
25 | 33 |
@Service("fileMngService") |
26 | 34 |
@RequiredArgsConstructor |
... | ... | @@ -31,13 +39,19 @@ |
31 | 39 |
private final JWTUtil jwtUtil; |
32 | 40 |
|
33 | 41 |
/** |
34 |
- * @author 박정하 |
|
35 |
- * @since 2024.03.26 |
|
42 |
+ * @param fileMngVO - 파일매니저 정보 |
|
43 |
+ * @param multipartFileList - 파일 목록 |
|
44 |
+ * @return HashMap<String, Object> |
|
45 |
+ * - fileInsertResult : 파일 등록 결과 |
|
46 |
+ * - fileMngId : 파일매니저 아이디 |
|
47 |
+ * - fileMngResult : 파일매니저 등록 결과 |
|
48 |
+ * @throws CustomNotFoundException - 사용자 정보 조회 예외 발생 시 |
|
49 |
+ * @throws CustomInsertFailException - 파일매니저 등록 예외 발생 시 |
|
36 | 50 |
* |
37 | 51 |
* 파일매니저 등록 |
38 | 52 |
*/ |
39 | 53 |
@Override |
40 |
- public HashMap<String, Object> fileMngInsert(FileMngVO fileMngVO, List<MultipartFile> multipartFileList) throws Exception { |
|
54 |
+ public HashMap<String, Object> fileMngInsert(FileMngVO fileMngVO, List<MultipartFile> multipartFileList) { |
|
41 | 55 |
HashMap<String, Object> result = new HashMap<>(); |
42 | 56 |
result.put("fileInsertResult", 0); |
43 | 57 |
|
... | ... | @@ -47,89 +61,92 @@ |
47 | 61 |
|
48 | 62 |
// 작성자 생성 |
49 | 63 |
String writer = jwtUtil.getWriter(); |
64 |
+ if (writer == null || writer.isEmpty()) { |
|
65 |
+ throw new CustomNotFoundException("사용자 정보 조회에 실패했습니다."); |
|
66 |
+ } |
|
50 | 67 |
fileMngVO.setRgtr(writer); |
51 | 68 |
|
52 | 69 |
// 파일 등록 |
53 |
- try { |
|
54 |
- result.put("fileInsertResult", fileService.fileInsert(fileMngId, multipartFileList)); |
|
55 |
- } catch (IOException e) { |
|
56 |
- e.printStackTrace(); |
|
57 |
- return result; |
|
58 |
- } |
|
70 |
+ result.put("fileInsertResult", fileService.fileInsert(fileMngId, multipartFileList)); |
|
59 | 71 |
|
60 | 72 |
// 파일매니저 아이디 삽입 |
61 | 73 |
result.put("fileMngId", fileMngId); |
62 | 74 |
|
63 | 75 |
// DB 등록 |
64 |
- result.put("fileMngResult", fileMngDAO.fileMngInsert(fileMngVO)); |
|
76 |
+ int fileMngResult = fileMngDAO.fileMngInsert(fileMngVO); |
|
77 |
+ if (fileMngResult == 0) { |
|
78 |
+ throw new CustomInsertFailException("파일매니저 등록에 실패했습니다."); |
|
79 |
+ } |
|
80 |
+ result.put("fileMngResult", fileMngResult); |
|
65 | 81 |
|
66 | 82 |
return result; |
67 | 83 |
} |
68 | 84 |
|
69 | 85 |
/** |
70 |
- * @author 박정하 |
|
71 |
- * @since 2024.03.27 |
|
86 |
+ * @param fileMngVO - 파일매니저 정보 |
|
87 |
+ * @param deleteFileList - 삭제 파일 목록 |
|
88 |
+ * @param multipartFileList - 파일 목록 |
|
89 |
+ * @return int - 파일매니저 수정 결과 |
|
90 |
+ * @throws CustomNotFoundException - 사용자 정보 조회 예외 발생 시 |
|
91 |
+ * @throws CustomUpdateFailException - 파일매니저 수정 예외 발생 시 |
|
72 | 92 |
* |
73 | 93 |
* 파일매니저 수정 |
74 | 94 |
*/ |
75 | 95 |
@Override |
76 |
- public int fileMngUpadate(FileMngVO fileMngVO, List<HashMap<String, Object>> deleteFileList, List<MultipartFile> multipartFileList) throws Exception { |
|
96 |
+ public int fileMngUpadate(FileMngVO fileMngVO, List<HashMap<String, Object>> deleteFileList, List<MultipartFile> multipartFileList) { |
|
97 |
+ int insertResult = 0; |
|
98 |
+ int deleteResult = 0; |
|
77 | 99 |
int result = 0; |
78 | 100 |
|
79 | 101 |
// 수정자 생성 |
80 | 102 |
String writer = jwtUtil.getWriter(); |
103 |
+ if(writer == null || writer.isEmpty()){ |
|
104 |
+ throw new CustomNotFoundException("사용자 정보 조회에 실패했습니다."); |
|
105 |
+ } |
|
81 | 106 |
fileMngVO.setMdfr(writer); |
82 | 107 |
|
83 | 108 |
// 파일 추가 |
84 |
- if(multipartFileList != null && multipartFileList.size() > 0) { |
|
109 |
+ if (multipartFileList != null && multipartFileList.size() > 0) { |
|
85 | 110 |
// DB 등록 |
86 |
- try { |
|
87 |
- String fileMngId =fileMngVO.getFileMngId(); |
|
88 |
- result += fileService.fileInsert(fileMngId, multipartFileList); |
|
89 |
- } catch (IOException e) { |
|
90 |
- e.printStackTrace(); |
|
91 |
- return result; |
|
92 |
- } |
|
111 |
+ String fileMngId = fileMngVO.getFileMngId(); |
|
112 |
+ insertResult = fileService.fileInsert(fileMngId, multipartFileList); |
|
93 | 113 |
} |
94 |
- |
|
114 |
+ |
|
95 | 115 |
// 파일 삭제 |
96 |
- if(deleteFileList != null && deleteFileList.size() > 0) { |
|
97 |
- try { |
|
98 |
- result += fileService.fileDelete(deleteFileList); |
|
99 |
- } catch (IOException e) { |
|
100 |
- e.printStackTrace(); |
|
101 |
- return result; |
|
102 |
- } |
|
116 |
+ if (deleteFileList != null && deleteFileList.size() > 0) { |
|
117 |
+ deleteResult = fileService.fileDelete(deleteFileList); |
|
103 | 118 |
} |
104 |
- |
|
119 |
+ |
|
105 | 120 |
// DB 등록 |
106 |
- result += fileMngDAO.fileMngUpadate(fileMngVO); |
|
121 |
+ result = fileMngDAO.fileMngUpadate(fileMngVO); |
|
122 |
+ if(result == 0){ |
|
123 |
+ throw new CustomUpdateFailException("파일매니저 수정에 실패했습니다."); |
|
124 |
+ } |
|
107 | 125 |
|
108 | 126 |
return result; |
109 | 127 |
} |
110 | 128 |
|
111 |
- |
|
112 | 129 |
/** |
113 |
- * @author 박정하 |
|
114 |
- * @since 2024.03.27 |
|
130 |
+ * @param params - 파일매니저 아이디 |
|
131 |
+ * @return int - 파일매니저 삭제 결과 |
|
132 |
+ * @throws CustomDeleteFailException - 파일매니저 삭제 예외 발생 시 |
|
115 | 133 |
* |
116 | 134 |
* 파일매니저 삭제 |
117 | 135 |
*/ |
118 | 136 |
@Override |
119 |
- public int fileMngDelete(HashMap<String, Object> params) throws Exception { |
|
137 |
+ public int fileMngDelete(HashMap<String, Object> params) { |
|
138 |
+ int deleteResult = 0; |
|
120 | 139 |
int result = 0; |
121 | 140 |
|
122 | 141 |
// 파일 삭제 |
123 |
- try { |
|
124 |
- List<HashMap<String, Object>> fileList = fileService.fileSelectList(params.get("fileMngId").toString()); |
|
125 |
- result += fileService.fileDelete(fileList); |
|
126 |
- } catch (IOException e) { |
|
127 |
- e.printStackTrace(); |
|
128 |
- return result; |
|
129 |
- } |
|
142 |
+ List<HashMap<String, Object>> fileList = fileService.fileSelectList(params.get("fileMngId").toString()); |
|
143 |
+ deleteResult = fileService.fileDelete(fileList); |
|
130 | 144 |
|
131 | 145 |
// DB 등록 |
132 |
- result += fileMngDAO.fileMngDelete(params); |
|
146 |
+ result = fileMngDAO.fileMngDelete(params); |
|
147 |
+ if(result == 0){ |
|
148 |
+ throw new CustomDeleteFailException("파일매니저 삭제에 실패했습니다."); |
|
149 |
+ } |
|
133 | 150 |
|
134 | 151 |
return result; |
135 | 152 |
} |
--- 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 @@ |
1 | 1 |
package com.takensoft.common.file.service.Impl; |
2 | 2 |
|
3 |
+import com.takensoft.common.exception.CustomDeleteFailException; |
|
4 |
+import com.takensoft.common.exception.CustomFileUploadFailException; |
|
5 |
+import com.takensoft.common.exception.CustomInsertFailException; |
|
6 |
+import com.takensoft.common.exception.CustomNotFoundException; |
|
3 | 7 |
import com.takensoft.common.file.dao.FileDAO; |
4 | 8 |
import com.takensoft.common.file.service.FileService; |
5 | 9 |
import com.takensoft.common.file.vo.FileVO; |
10 |
+import com.takensoft.common.util.FileUtil; |
|
6 | 11 |
import com.takensoft.common.util.JWTUtil; |
7 | 12 |
import lombok.RequiredArgsConstructor; |
13 |
+import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException; |
|
8 | 14 |
import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl; |
9 | 15 |
import org.springframework.beans.factory.annotation.Value; |
10 | 16 |
import org.springframework.dao.DataAccessException; |
... | ... | @@ -18,6 +24,22 @@ |
18 | 24 |
import java.util.*; |
19 | 25 |
import java.util.UUID; |
20 | 26 |
|
27 |
+ |
|
28 |
+/** |
|
29 |
+ * @author 박정하 |
|
30 |
+ * @since 2024.03.26 |
|
31 |
+ * @modification |
|
32 |
+ * since | author | description |
|
33 |
+ * 2024.03.26 | 박정하 | 최초 등록 |
|
34 |
+ * 2024.03.27 | 박정하 | fileDelete 추가 |
|
35 |
+ * 2024.05.21 | 방선주 | fileSelectOne, editorUploadImg 추가 |
|
36 |
+ * 2025.03.20 | 하석형 | 코드 리펙토링 |
|
37 |
+ * |
|
38 |
+ * EgovAbstractServiceImpl : 전자정부 상속 |
|
39 |
+ * FileService : 파일 관련 인터페이스 상속 |
|
40 |
+ * |
|
41 |
+ * 파일 관련 인터페이스 구현체 |
|
42 |
+ */ |
|
21 | 43 |
@Service("fileService") |
22 | 44 |
@RequiredArgsConstructor |
23 | 45 |
public class FileServiceImpl extends EgovAbstractServiceImpl implements FileService { |
... | ... | @@ -40,81 +62,97 @@ |
40 | 62 |
@Value("${file.edit-file-upload-path}") |
41 | 63 |
private String editPath; |
42 | 64 |
|
65 |
+ @Value("${spring.servlet.multipart.max-file-size}") |
|
66 |
+ private String maxFileSize; |
|
67 |
+ |
|
43 | 68 |
/** |
44 |
- * @author 박정하 |
|
45 |
- * @since 2024.03.26 |
|
46 |
- * <p> |
|
69 |
+ * @param fileMngId - 파일관리아이디 |
|
70 |
+ * @param multipartFileList - 파일 목록 |
|
71 |
+ * @return int - 파일 등록 결과 |
|
72 |
+ * @throws RuntimeException - 파일 크기 초과 예외 발생 시 |
|
73 |
+ * @throws CustomInsertFailException - 파일 등록 예외 발생 시 |
|
74 |
+ * @throws CustomNotFoundException - 사용자 정보 조회 예외 발생 시 |
|
75 |
+ * @throws CustomFileUploadFailException - 파일 업로드 예외 발생 시 |
|
76 |
+ * @throws IllegalArgumentException - 잘못된 인자 예외 발생 시 |
|
77 |
+ * @throws DataAccessException - db 관련 예외 발생 시 |
|
78 |
+ * @throws Exception - 그 외 예외 발생 시 |
|
79 |
+ * |
|
47 | 80 |
* 파일 등록 |
48 | 81 |
*/ |
49 | 82 |
@Override |
50 | 83 |
@Transactional(rollbackFor = Exception.class) |
51 |
- public int fileInsert(String fileMngId, List<MultipartFile> multipartFileList) throws Exception { |
|
52 |
- int result = 0; |
|
84 |
+ public int fileInsert(String fileMngId, List<MultipartFile> multipartFileList) { |
|
85 |
+ try { |
|
86 |
+ int result = 0; |
|
53 | 87 |
|
54 |
- for (MultipartFile file : multipartFileList) { |
|
55 |
- if (file.isEmpty()) { |
|
56 |
- continue; |
|
57 |
- } |
|
88 |
+ for (MultipartFile file : multipartFileList) { |
|
89 |
+ if (file.isEmpty()) { |
|
90 |
+ continue; |
|
91 |
+ } |
|
58 | 92 |
|
59 |
- int size = Long.valueOf(file.getSize()).intValue() / 100000; |
|
60 |
- if (size > 200) { |
|
61 |
- return -1; |
|
62 |
- } |
|
93 |
+ int size = Long.valueOf(file.getSize()).intValue() / 100000; |
|
94 |
+ if (size > FileUtil.sizeStrToBytes(maxFileSize)) { |
|
95 |
+ throw new RuntimeException("파일 크기가 " + maxFileSize + "를 초과했습니다. (" + size + "MB)"); |
|
96 |
+// throw new FileSizeLimitExceededException("파일 크기가 " + maxFileSize + "를 초과했습니다. (" + size + "MB)", file.getSize(), FileUtil.sizeStrToBytes(maxFileSize)); |
|
97 |
+// return -1; |
|
98 |
+ } |
|
63 | 99 |
|
64 |
- // 절대 경로 생성 |
|
65 |
- // "file:///" 제거하여 실제 파일 시스템 경로만을 저장 |
|
66 |
- String uploadPath = null; |
|
67 |
-// String[] pathArray = locationPath.split("/, "); |
|
68 |
-// for (String path : pathArray) { |
|
69 |
-// if (path.startsWith("file:///")) { |
|
70 |
-// // "file:///" 부분을 제거하고 경로를 반환 |
|
71 |
-// uploadPath = path.replace("file:///", "") + fileUploadPath; |
|
72 |
-// } |
|
73 |
-// } |
|
74 |
- uploadPath = locationPath + fileUploadPath; |
|
100 |
+ // 절대 경로 생성 |
|
101 |
+ String uploadPath = null; |
|
102 |
+ uploadPath = locationPath + fileUploadPath; |
|
75 | 103 |
|
76 |
- // 파일 정보 가공 |
|
77 |
- String fileName = file.getOriginalFilename(); // 파일명 (원본) |
|
78 |
- String fileNm = fileName.substring(0, fileName.lastIndexOf(".")); // 파일명 |
|
79 |
- String maskNm = UUID.randomUUID().toString().replaceAll("-", ""); // 마스크명 |
|
80 |
- String extsn = StringUtils.getFilenameExtension(fileName); // 확장자 |
|
81 |
- String mg = Long.toString(file.getSize()); // 크기 |
|
82 |
- String absltPath = makeDirectories(uploadPath) + File.separator + maskNm + "." + extsn; // 절대경로 |
|
83 |
- String partPath = File.separator + maskNm + "." + extsn; // 일부경로 |
|
104 |
+ // 파일 정보 가공 |
|
105 |
+ String fileName = file.getOriginalFilename(); // 파일명 (원본) |
|
106 |
+ String fileNm = fileName.substring(0, fileName.lastIndexOf(".")); // 파일명 |
|
107 |
+ String maskNm = UUID.randomUUID().toString().replaceAll("-", ""); // 마스크명 |
|
108 |
+ String extsn = StringUtils.getFilenameExtension(fileName); // 확장자 |
|
109 |
+ String mg = Long.toString(file.getSize()); // 크기 |
|
110 |
+ String absltPath = makeDirectories(uploadPath) + File.separator + maskNm + "." + extsn; // 절대경로 |
|
111 |
+ String partPath = File.separator + maskNm + "." + extsn; // 일부경로 |
|
84 | 112 |
|
85 |
- // FileVO 생성 |
|
86 |
- FileVO fileVO = new FileVO(); |
|
87 |
- fileVO.setFileMngId(fileMngId); // 파일관리아이디 |
|
88 |
- fileVO.setFileNm(fileNm); // 파일명 |
|
89 |
- fileVO.setMaskNm(maskNm); // 마스크명 |
|
90 |
- // 파일타입 추후에 추가 |
|
91 |
- fileVO.setAbsltPath(absltPath); // 절대경로 |
|
92 |
- fileVO.setPartPath(partPath); // 일부경로 |
|
93 |
- fileVO.setExtnNm(extsn); // 확장자 |
|
94 |
- fileVO.setFileSz(mg); // 크기 |
|
95 |
- // 등록자 생성 |
|
96 |
- String writer = jwtUtil.getWriter(); |
|
97 |
- fileVO.setRgtr(writer); |
|
113 |
+ // FileVO 생성 |
|
114 |
+ FileVO fileVO = new FileVO(); |
|
115 |
+ fileVO.setFileMngId(fileMngId); // 파일관리아이디 |
|
116 |
+ fileVO.setFileNm(fileNm); // 파일명 |
|
117 |
+ fileVO.setMaskNm(maskNm); // 마스크명 |
|
118 |
+ // 파일타입 추후에 추가 |
|
119 |
+ fileVO.setAbsltPath(absltPath); // 절대경로 |
|
120 |
+ fileVO.setPartPath(partPath); // 일부경로 |
|
121 |
+ fileVO.setExtnNm(extsn); // 확장자 |
|
122 |
+ fileVO.setFileSz(mg); // 크기 |
|
123 |
+ // 등록자 생성 |
|
124 |
+ String writer = jwtUtil.getWriter(); |
|
125 |
+ if(writer == null || writer.isEmpty()){ |
|
126 |
+ throw new CustomNotFoundException("사용자 정보 조회에 실패했습니다."); |
|
127 |
+ } |
|
128 |
+ fileVO.setRgtr(writer); |
|
98 | 129 |
|
99 |
- try { |
|
130 |
+ // 파일 업로드 |
|
100 | 131 |
try { |
101 |
- // 파일 업로드 |
|
102 | 132 |
File uploadFile = new File(absltPath); |
103 | 133 |
file.transferTo(uploadFile); |
104 |
- } catch (IOException e) { |
|
105 |
- e.printStackTrace(); |
|
106 |
- return 0; |
|
134 |
+ } catch (IOException ioe) { |
|
135 |
+ throw new CustomFileUploadFailException("파일 업로드에 실패했습니다.", ioe); |
|
136 |
+// e.printStackTrace(); |
|
137 |
+// return 0; |
|
107 | 138 |
} |
108 | 139 |
|
109 | 140 |
// DB등록 |
110 | 141 |
result += fileDAO.fileInsert(fileVO); |
111 |
- } catch (IOException e) { |
|
112 |
- e.printStackTrace(); |
|
113 |
- return 0; |
|
142 |
+ if (result == 0) { |
|
143 |
+ throw new CustomInsertFailException("파일 등록에 실패했습니다."); |
|
144 |
+// return 0; |
|
145 |
+ } |
|
114 | 146 |
} |
115 |
- } |
|
116 | 147 |
|
117 |
- return result; |
|
148 |
+ return result; |
|
149 |
+ } catch (IllegalArgumentException iae) { |
|
150 |
+ throw iae; |
|
151 |
+ } catch (DataAccessException dae) { |
|
152 |
+ throw dae; |
|
153 |
+ } catch (Exception e) { |
|
154 |
+ throw e; |
|
155 |
+ } |
|
118 | 156 |
} |
119 | 157 |
|
120 | 158 |
// 업로드 폴더(디렉터리) 생성 |
... | ... | @@ -127,9 +165,11 @@ |
127 | 165 |
} |
128 | 166 |
|
129 | 167 |
/** |
130 |
- * @author 박정하 |
|
131 |
- * @since 2024.03.26 |
|
132 |
- * <p> |
|
168 |
+ * @param fileMngId - 파일관리아이디 |
|
169 |
+ * @return List<HashMap<String, Object>> - 파일 내용 정보를 담고 있는 객체 목록 |
|
170 |
+ * @throws DataAccessException - db 관련 예외 발생 시 |
|
171 |
+ * @throws Exception - 그 외 예외 발생 시 |
|
172 |
+ * |
|
133 | 173 |
* 파일 목록 조회 |
134 | 174 |
*/ |
135 | 175 |
@Override |
... | ... | @@ -144,33 +184,32 @@ |
144 | 184 |
} |
145 | 185 |
|
146 | 186 |
/** |
147 |
- * @author 박정하 |
|
148 |
- * @since 2024.03.27 |
|
149 |
- * <p> |
|
187 |
+ * @param fileList - 파일 목록 |
|
188 |
+ * @return int - 파일 삭제 결과 |
|
189 |
+ * @throws CustomDeleteFailException - 파일 삭제 예외 발생 시 |
|
190 |
+ * @throws DataAccessException - db 관련 예외 발생 시 |
|
191 |
+ * @throws Exception - 그 외 예외 발생 시 |
|
192 |
+ * |
|
150 | 193 |
* 파일 삭제 |
151 | 194 |
*/ |
152 |
- public int fileDelete(List<HashMap<String, Object>> fileList) throws Exception { |
|
153 |
- int result = 0; |
|
195 |
+ public int fileDelete(List<HashMap<String, Object>> fileList) { |
|
196 |
+ try { |
|
197 |
+ int result = 0; |
|
154 | 198 |
|
155 |
- for (HashMap<String, Object> file : fileList) { |
|
156 |
- if (file.isEmpty()) { |
|
157 |
- continue; |
|
158 |
- } |
|
199 |
+ for (HashMap<String, Object> file : fileList) { |
|
200 |
+ if (file.isEmpty()) { |
|
201 |
+ continue; |
|
202 |
+ } |
|
159 | 203 |
|
160 |
- try { |
|
161 | 204 |
// DB 삭제 |
162 | 205 |
result += fileDAO.fileDelete(file); |
206 |
+ if(result == 0){ |
|
207 |
+ throw new CustomDeleteFailException("파일 삭제에 실패했습니다."); |
|
208 |
+ } |
|
163 | 209 |
|
164 | 210 |
// http 부분 |
165 | 211 |
String url = frontUrl + ":" + serverPort; |
166 | 212 |
String uploadPath = null; |
167 |
-// String[] pathArray = locationPath.split("/, "); |
|
168 |
-// for (String path : pathArray) { |
|
169 |
-// if (path.startsWith("file:///")) { |
|
170 |
-// // "file:///" 부분을 제거하고 경로를 반환 |
|
171 |
-// uploadPath = path.replace("file:///", "") + fileUploadPath; |
|
172 |
-// } |
|
173 |
-// } |
|
174 | 213 |
uploadPath = locationPath + fileUploadPath; |
175 | 214 |
|
176 | 215 |
String path = file.get("absltPath").toString(); |
... | ... | @@ -178,65 +217,76 @@ |
178 | 217 |
|
179 | 218 |
// Disk 삭제 |
180 | 219 |
File newFile = new File(path); |
181 |
- |
|
182 |
- |
|
183 | 220 |
// newFile = newFile.replace(url, uploadPath); |
184 | 221 |
if (newFile.exists()) { |
185 | 222 |
newFile.delete(); |
186 | 223 |
} |
187 |
- } catch (IOException e) { |
|
188 |
- e.printStackTrace(); |
|
189 | 224 |
} |
190 |
- } |
|
191 | 225 |
|
192 |
- return result; |
|
226 |
+ return result; |
|
227 |
+ } catch (DataAccessException dae) { |
|
228 |
+ throw dae; |
|
229 |
+ } catch (Exception e) { |
|
230 |
+ throw e; |
|
231 |
+ } |
|
193 | 232 |
} |
194 | 233 |
|
195 | 234 |
/** |
196 |
- * @author 방선주 |
|
197 |
- * @since 2024.05.21 |
|
198 |
- * <p> |
|
235 |
+ * @param fileId - 파일 아이디 |
|
236 |
+ * @return FileVO - 파일 내용 정보를 담고 있는 객체 |
|
237 |
+ * @throws DataAccessException - db 관련 예외 발생 시 |
|
238 |
+ * @throws Exception - 그 외 예외 발생 시 |
|
239 |
+ * |
|
199 | 240 |
* 파일 단일 조회 |
200 | 241 |
*/ |
201 | 242 |
@Override |
202 |
- public FileVO fileSelectOne(int fileId) throws Exception { |
|
203 |
- return fileDAO.fileSelectOne(fileId); |
|
243 |
+ public FileVO fileSelectOne(int fileId) { |
|
244 |
+ try { |
|
245 |
+ return fileDAO.fileSelectOne(fileId); |
|
246 |
+ } catch (DataAccessException dae) { |
|
247 |
+ throw dae; |
|
248 |
+ } catch (Exception e) { |
|
249 |
+ throw e; |
|
250 |
+ } |
|
204 | 251 |
} |
205 | 252 |
|
253 |
+ /** |
|
254 |
+ * @param file - 파일 정보 |
|
255 |
+ * @return String - 이미지 경로 |
|
256 |
+ * @throws CustomFileUploadFailException - 파일 업로드 예외 발생 시 |
|
257 |
+ * @throws DataAccessException - db 관련 예외 발생 시 |
|
258 |
+ * @throws Exception - 그 외 예외 발생 시 |
|
259 |
+ * |
|
260 |
+ * 에디터 이미지 업로드 |
|
261 |
+ */ |
|
206 | 262 |
@Override |
207 |
- public String editorUploadImg(MultipartFile file) throws Exception { |
|
208 |
- MultipartFile uploadFile = file; |
|
209 |
- // 파일 명 생성 |
|
210 |
- String originalFileName = uploadFile.getOriginalFilename(); |
|
211 |
- String ext = originalFileName.substring(originalFileName.lastIndexOf(".")); |
|
212 |
- String maskNm = UUID.randomUUID() + ext; // 랜덤으로 파일명 생성 |
|
213 |
- // 파일 저장 경로 |
|
214 |
- String uploadPath = null; |
|
215 |
-// String[] pathArray = locationPath.split(", "); |
|
216 |
-// for (String path : pathArray) { |
|
217 |
-// if (path.startsWith("file:///")) { |
|
218 |
-// // "file:///" 부분을 제거하고 경로를 반환 |
|
219 |
-// uploadPath = path.replace("file:///", "") + editPath; |
|
220 |
-// break; // 루프를 종료하여 첫 번째 일치하는 경로만 사용 |
|
221 |
-// } |
|
222 |
-// } |
|
223 |
-// if (uploadPath == null) { |
|
224 |
-// throw new Exception("Upload path is not properly configured."); |
|
225 |
-// } |
|
226 |
- uploadPath = "/home/cloud-user/front-end/uploadFiles" + editPath; |
|
227 |
- String savePath = uploadPath + maskNm; |
|
228 |
- // 파일 저장 |
|
263 |
+ public String editorUploadImg(MultipartFile file) { |
|
229 | 264 |
try { |
230 |
- File saveFile = new File(savePath); |
|
231 |
- if (!saveFile.exists()) { |
|
232 |
- saveFile.mkdirs(); |
|
265 |
+ MultipartFile uploadFile = file; |
|
266 |
+ // 파일 명 생성 |
|
267 |
+ String originalFileName = uploadFile.getOriginalFilename(); |
|
268 |
+ String ext = originalFileName.substring(originalFileName.lastIndexOf(".")); |
|
269 |
+ String maskNm = UUID.randomUUID() + ext; // 랜덤으로 파일명 생성 |
|
270 |
+ // 파일 저장 경로 |
|
271 |
+ String uploadPath = null; |
|
272 |
+ uploadPath = "/home/cloud-user/front-end/uploadFiles" + editPath; |
|
273 |
+ String savePath = uploadPath + maskNm; |
|
274 |
+ // 파일 저장 |
|
275 |
+ try { |
|
276 |
+ File saveFile = new File(savePath); |
|
277 |
+ if (!saveFile.exists()) { |
|
278 |
+ saveFile.mkdirs(); |
|
279 |
+ } |
|
280 |
+ uploadFile.transferTo(saveFile); |
|
281 |
+ } catch (IOException ioe) { |
|
282 |
+ throw new CustomFileUploadFailException("파일 업로드에 실패했습니다.", ioe); |
|
233 | 283 |
} |
234 |
- uploadFile.transferTo(saveFile); |
|
284 |
+ String imgPath = frontUrl + "/uploadFiles" + editPath + maskNm; |
|
285 |
+ return imgPath; |
|
286 |
+ } catch (DataAccessException dae) { |
|
287 |
+ throw dae; |
|
235 | 288 |
} catch (Exception e) { |
236 |
- e.printStackTrace(); |
|
237 |
- throw new Exception("File upload failed."); |
|
289 |
+ throw e; |
|
238 | 290 |
} |
239 |
- String imgPath = frontUrl + "/uploadFiles" + editPath + maskNm; |
|
240 |
- return imgPath; |
|
241 | 291 |
} |
242 | 292 |
}(파일 끝에 줄바꿈 문자 없음) |
+++ src/main/java/com/takensoft/common/util/FileUtil.java
... | ... | @@ -0,0 +1,57 @@ |
1 | +package com.takensoft.common.util; | |
2 | + | |
3 | +import org.springframework.stereotype.Component; | |
4 | + | |
5 | +import java.net.InetAddress; | |
6 | +import java.net.UnknownHostException; | |
7 | + | |
8 | +/** | |
9 | + * @author 하석형 | |
10 | + * @since 2025.03.13 | |
11 | + * @modification | |
12 | + * since | author | description | |
13 | + * 2025.03.13 | 하석형 | 최초 등록 | |
14 | + * | |
15 | + * 파일 관련 RestController | |
16 | + */ | |
17 | +@Component | |
18 | +public class FileUtil { | |
19 | + | |
20 | + /** | |
21 | + * @param sizeStr - 파일사이즈(문자열) | |
22 | + * @return long - 파일사이즈(바이트) | |
23 | + * | |
24 | + * 단위가 포함된 파일사이즈(문자열)를 바이트로 변환 | |
25 | + */ | |
26 | + public static long sizeStrToBytes(String sizeStr) { | |
27 | + if (sizeStr == null || sizeStr.isEmpty()) { | |
28 | + throw new IllegalArgumentException("입력된 파일 크기 값이 올바르지 않습니다."); | |
29 | + } | |
30 | + | |
31 | + // 숫자 부분과 단위 부분 분리 | |
32 | + sizeStr = sizeStr.toUpperCase().trim(); // 대문자로 변환 후 공백 제거 | |
33 | + String numberPart = sizeStr.replaceAll("[^0-9.]", ""); // 숫자만 추출 (숫자 및 '.'를 제외한 문자 제거) | |
34 | + String unitPart = sizeStr.replaceAll("[0-9.]", "").trim(); // 단위만 추출 (숫자 및 '.' 제거) | |
35 | + | |
36 | + // 숫자 값을 double로 변환 | |
37 | + double sizeValue = Double.parseDouble(numberPart); | |
38 | + | |
39 | + // 단위에 따라 바이트 변환 | |
40 | + switch (unitPart) { | |
41 | + case "B": | |
42 | + return (long) sizeValue; // 바이트 그대로 | |
43 | + case "KB": | |
44 | + return (long) (sizeValue * 1024); | |
45 | + case "MB": | |
46 | + return (long) (sizeValue * 1024 * 1024); | |
47 | + case "GB": | |
48 | + return (long) (sizeValue * 1024 * 1024 * 1024); | |
49 | + case "TB": | |
50 | + return (long) (sizeValue * 1024 * 1024 * 1024 * 1024); | |
51 | + case "PB": | |
52 | + return (long) (sizeValue * 1024 * 1024 * 1024 * 1024 * 1024); | |
53 | + default: | |
54 | + throw new IllegalArgumentException("지원하지 않는 파일 크기 단위: " + unitPart); | |
55 | + } | |
56 | + } | |
57 | +} |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?