큐공통, 서비스별 큐, 파일관리유틸
@f06eb1a20f09dbdd7c84ee10d7c8a7190bc05a0a
+++ src/main/java/com/munjaon/server/queue/config/QueueHeaderConfig.java
... | ... | @@ -0,0 +1,14 @@ |
| 1 | +package com.munjaon.server.queue.config; | |
| 2 | + | |
| 3 | +public final class QueueHeaderConfig { | |
| 4 | + /** Queue File Header Length - [Create Date:10 Byte, Push Count:10 Byte] */ | |
| 5 | + public static final int QUEUE_HEADER_LENGTH = 20; | |
| 6 | + /* 큐생성일 읽을 위치값 */ | |
| 7 | + public static final int CREATE_DATE_POSITION = 0; | |
| 8 | + /* 큐생성일 읽을 크기 */ | |
| 9 | + public static final int CREATE_DATE_LENGTH = 10; | |
| 10 | + /* 큐 쓰기 카운트 읽을 위치값 */ | |
| 11 | + public static final int PUSH_COUNT_POSITION = 10; | |
| 12 | + /* 큐 쓰기 카운트 읽을 크기 */ | |
| 13 | + public static final int PUSH_COUNT_LENGTH = 10; | |
| 14 | +} |
+++ src/main/java/com/munjaon/server/queue/dto/LmsMessageDto.java
... | ... | @@ -0,0 +1,12 @@ |
| 1 | +package com.munjaon.server.queue.dto; | |
| 2 | + | |
| 3 | +import lombok.Getter; | |
| 4 | +import lombok.Setter; | |
| 5 | +import lombok.ToString; | |
| 6 | + | |
| 7 | +@Getter | |
| 8 | +@Setter | |
| 9 | +@ToString | |
| 10 | +public class LmsMessageDto extends BasicMessageDto { | |
| 11 | + private String userSubject = ""; | |
| 12 | +} |
+++ src/main/java/com/munjaon/server/queue/dto/QueueInfo.java
... | ... | @@ -0,0 +1,16 @@ |
| 1 | +package com.munjaon.server.queue.dto; | |
| 2 | + | |
| 3 | +import lombok.Getter; | |
| 4 | +import lombok.Setter; | |
| 5 | +import lombok.ToString; | |
| 6 | + | |
| 7 | +@Getter | |
| 8 | +@Setter | |
| 9 | +@ToString | |
| 10 | +public class QueueInfo { | |
| 11 | + private String queueName = ""; | |
| 12 | + private String queueFileName = ""; | |
| 13 | + private int queueDataLength = 0; | |
| 14 | + private String serviceType = ""; | |
| 15 | + private String readXMLFileName = ""; | |
| 16 | +} |
+++ src/main/java/com/munjaon/server/queue/service/KakaoWriteQueue.java
... | ... | @@ -0,0 +1,9 @@ |
| 1 | +package com.munjaon.server.queue.service; | |
| 2 | + | |
| 3 | +import com.munjaon.server.queue.dto.KakaoMessageDto; | |
| 4 | + | |
| 5 | +public class KakaoWriteQueue extends WriteQueue { | |
| 6 | + public void pushBuffer(KakaoMessageDto data) throws Exception { | |
| 7 | + | |
| 8 | + } | |
| 9 | +} |
+++ src/main/java/com/munjaon/server/queue/service/LmsWriteQueue.java
... | ... | @@ -0,0 +1,9 @@ |
| 1 | +package com.munjaon.server.queue.service; | |
| 2 | + | |
| 3 | +import com.munjaon.server.queue.dto.LmsMessageDto; | |
| 4 | + | |
| 5 | +public class LmsWriteQueue extends WriteQueue { | |
| 6 | + public void pushBuffer(LmsMessageDto data) throws Exception { | |
| 7 | + | |
| 8 | + } | |
| 9 | +} |
+++ src/main/java/com/munjaon/server/queue/service/MmsWriteQueue.java
... | ... | @@ -0,0 +1,9 @@ |
| 1 | +package com.munjaon.server.queue.service; | |
| 2 | + | |
| 3 | +import com.munjaon.server.queue.dto.MmsMessageDto; | |
| 4 | + | |
| 5 | +public class MmsWriteQueue extends WriteQueue { | |
| 6 | + public void pushBuffer(MmsMessageDto data) throws Exception { | |
| 7 | + | |
| 8 | + } | |
| 9 | +} |
+++ src/main/java/com/munjaon/server/queue/service/SmsWriteQueue.java
... | ... | @@ -0,0 +1,9 @@ |
| 1 | +package com.munjaon.server.queue.service; | |
| 2 | + | |
| 3 | +import com.munjaon.server.queue.dto.BasicMessageDto; | |
| 4 | + | |
| 5 | +public class SmsWriteQueue extends WriteQueue { | |
| 6 | + public void pushBuffer(BasicMessageDto data) throws Exception { | |
| 7 | + | |
| 8 | + } | |
| 9 | +} |
+++ src/main/java/com/munjaon/server/queue/service/WriteQueue.java
... | ... | @@ -0,0 +1,131 @@ |
| 1 | +package com.munjaon.server.queue.service; | |
| 2 | + | |
| 3 | +import com.munjaon.server.queue.config.QueueConstants; | |
| 4 | +import com.munjaon.server.queue.config.QueueHeaderConfig; | |
| 5 | +import com.munjaon.server.queue.dto.QueueInfo; | |
| 6 | + | |
| 7 | +import java.io.File; | |
| 8 | +import java.io.IOException; | |
| 9 | +import java.io.RandomAccessFile; | |
| 10 | +import java.nio.ByteBuffer; | |
| 11 | +import java.nio.channels.FileChannel; | |
| 12 | +import java.time.LocalDateTime; | |
| 13 | +import java.time.format.DateTimeFormatter; | |
| 14 | + | |
| 15 | +public abstract class WriteQueue { | |
| 16 | + /** Queue Header Size - [Create Date:10 Byte, Push Count:10 Byte] */ | |
| 17 | + protected static final int QUEUE_HEADER_LENGTH = 20; | |
| 18 | + /** Queue Create Date - [Format:YYYYMMDD] */ | |
| 19 | + protected String createDate = ""; | |
| 20 | + /** Queue Push Counter */ | |
| 21 | + protected int pushCounter = 0; | |
| 22 | + /** Queue Header Buffer */ | |
| 23 | + protected ByteBuffer headerBuffer = null; | |
| 24 | + /** Queue Information */ | |
| 25 | + protected QueueInfo queueInfo = null; | |
| 26 | + /** Queue File Channel */ | |
| 27 | + protected FileChannel channel = null; | |
| 28 | + /** Header 에서 사용하는 변수 */ | |
| 29 | + protected byte[] headerArray = null; | |
| 30 | + /** pushBuffer() 함수에서 사용하는 변수 */ | |
| 31 | + protected ByteBuffer dataBuffer = null; | |
| 32 | + | |
| 33 | + protected void initQueue() throws Exception { | |
| 34 | + this.headerBuffer = ByteBuffer.allocateDirect(QueueHeaderConfig.QUEUE_HEADER_LENGTH); | |
| 35 | + try{ | |
| 36 | + File file = new File(this.queueInfo.getQueueFileName()); | |
| 37 | + this.channel = new RandomAccessFile(file, "rw").getChannel(); | |
| 38 | + //this.lock = this.channel.lock(); | |
| 39 | + | |
| 40 | + if(file.length() == 0) { | |
| 41 | + // Push 및 Pop 카운트 초기화 | |
| 42 | + this.createDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); | |
| 43 | + this.pushCounter = 0; | |
| 44 | + // 헤더 초기화 | |
| 45 | + writeHeader(); | |
| 46 | + }else{ | |
| 47 | + readHeader(); | |
| 48 | + } | |
| 49 | + }catch(Exception e){ | |
| 50 | + throw e; | |
| 51 | + } finally { | |
| 52 | + //lock.release(); | |
| 53 | + } | |
| 54 | + } | |
| 55 | + | |
| 56 | + public void readHeader() throws Exception { | |
| 57 | + try { | |
| 58 | + initHeaderBuffer(); | |
| 59 | + this.channel.position(0); | |
| 60 | + this.channel.read(this.headerBuffer); | |
| 61 | + this.headerArray = new byte[10]; | |
| 62 | + // 생성날짜 가져오기 - 생성날짜(10) / 읽은카운트(10) / 쓴카운트(10) | |
| 63 | + this.headerBuffer.position(0); | |
| 64 | + this.headerBuffer.get(this.headerArray); | |
| 65 | + this.createDate = (new String(this.headerArray)).trim(); | |
| 66 | + // 쓴 카운트 가져오기 | |
| 67 | + this.headerArray = new byte[10]; | |
| 68 | + this.headerBuffer.position(10); | |
| 69 | + this.headerBuffer.get(this.headerArray); | |
| 70 | + this.pushCounter = Integer.parseInt((new String(this.headerArray)).trim()); | |
| 71 | + } catch(Exception e) { | |
| 72 | + throw e; | |
| 73 | + } | |
| 74 | + } | |
| 75 | + | |
| 76 | + public void writeHeader() throws Exception { | |
| 77 | + try { | |
| 78 | + initHeaderBuffer(); | |
| 79 | + this.channel.position(0); | |
| 80 | + this.headerBuffer.put(this.createDate.getBytes()); | |
| 81 | + this.headerBuffer.position(10); | |
| 82 | + this.headerBuffer.put(Integer.toString(this.pushCounter).getBytes()); | |
| 83 | + this.headerBuffer.flip(); | |
| 84 | + this.channel.write(this.headerBuffer); | |
| 85 | + } catch(Exception e) { | |
| 86 | + throw e; | |
| 87 | + } | |
| 88 | + } | |
| 89 | + | |
| 90 | + public void initHeaderBuffer(){ | |
| 91 | + this.headerBuffer.clear(); | |
| 92 | + for(int loopCnt = 0; loopCnt < QueueHeaderConfig.QUEUE_HEADER_LENGTH; loopCnt++){ | |
| 93 | + this.headerBuffer.put(QueueConstants.SET_DEFAULT_BYTE); | |
| 94 | + } | |
| 95 | + this.headerBuffer.position(0); | |
| 96 | + } | |
| 97 | + | |
| 98 | + public void close() throws IOException { | |
| 99 | + try { | |
| 100 | + if(channel != null && channel.isOpen()) { | |
| 101 | + channel.close(); | |
| 102 | + } | |
| 103 | + } catch(IOException e) { | |
| 104 | + throw e; | |
| 105 | + } | |
| 106 | + } | |
| 107 | + | |
| 108 | + public void truncateQueue() throws Exception{ | |
| 109 | + try { | |
| 110 | + //this.lock = this.channel.lock(); | |
| 111 | + | |
| 112 | + if(channel != null && channel.isOpen()) { | |
| 113 | + // 헤더정보 읽기 | |
| 114 | + readHeader(); | |
| 115 | + String thisDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); | |
| 116 | + // 날짜가 지난경우와 더이상 읽을 데이터가 없을 경우 큐를 초기화 | |
| 117 | + if((this.createDate.equals(thisDate) == false)){ | |
| 118 | + // 파일 내용을 모두 지운다. | |
| 119 | + channel.truncate(0); | |
| 120 | + // 헤더 정보를 초기화한다. | |
| 121 | + this.createDate = thisDate; | |
| 122 | + this.pushCounter = 0; | |
| 123 | + // 헤더에 초기데이터를 저장 | |
| 124 | + writeHeader(); | |
| 125 | + } | |
| 126 | + } | |
| 127 | + } catch(Exception e) { | |
| 128 | + throw e; | |
| 129 | + } | |
| 130 | + } | |
| 131 | +} |
+++ src/main/java/com/munjaon/server/util/JobFileFactory.java
... | ... | @@ -0,0 +1,474 @@ |
| 1 | +package com.munjaon.server.util; | |
| 2 | + | |
| 3 | +import com.munjaon.server.util.dto.FileBaseDto; | |
| 4 | +import lombok.extern.slf4j.Slf4j; | |
| 5 | +import org.springframework.web.multipart.MultipartFile; | |
| 6 | + | |
| 7 | +import java.io.File; | |
| 8 | +import java.io.FileInputStream; | |
| 9 | +import java.io.FileOutputStream; | |
| 10 | +import java.io.IOException; | |
| 11 | +import java.nio.channels.FileChannel; | |
| 12 | +import java.time.LocalDateTime; | |
| 13 | +import java.time.format.DateTimeFormatter; | |
| 14 | +import java.util.ArrayList; | |
| 15 | +import java.util.List; | |
| 16 | + | |
| 17 | +@Slf4j | |
| 18 | +public final class JobFileFactory { | |
| 19 | + public static final Long MAX_LIMIT_BYTES = 10485760L; | |
| 20 | + /** | |
| 21 | + * 파일 관련 : 파일 객체 반환 : 디렉토리 경로 포함 | |
| 22 | + * @param path | |
| 23 | + * @return | |
| 24 | + */ | |
| 25 | + public static File getFile(final String path) { | |
| 26 | + if (path == null) { | |
| 27 | + return null; | |
| 28 | + } | |
| 29 | + | |
| 30 | + return new File(path); | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * 파일 관련 : 파일 객체 반환 : 경로, 파일명 | |
| 35 | + * @param path | |
| 36 | + * @param fileName | |
| 37 | + * @return | |
| 38 | + */ | |
| 39 | + public static File getFile(final String path, final String fileName) { | |
| 40 | + return getFile(path + File.separator + fileName); | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * 파일관련 : 파일 존재 여부 | |
| 45 | + * @param file | |
| 46 | + * @return | |
| 47 | + */ | |
| 48 | + private static boolean exist(final File file) { | |
| 49 | + return file.exists(); | |
| 50 | + } | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * 파일관련 : 파일 존재 여부 | |
| 54 | + * @param path | |
| 55 | + * @return | |
| 56 | + */ | |
| 57 | + public static boolean exist(final String path) { | |
| 58 | + if (path == null) { | |
| 59 | + return false; | |
| 60 | + } | |
| 61 | + log.info("path : {}", path); | |
| 62 | + return new File(path).exists(); | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * 파일관련 : 파일 존재 여부 | |
| 67 | + * @param path | |
| 68 | + * @param fileName | |
| 69 | + * @return | |
| 70 | + */ | |
| 71 | + public static boolean exist(final String path, final String fileName) { | |
| 72 | + return exist(path + File.separator + fileName); | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * 파일관련 : 디렉토리 여부 | |
| 77 | + * @param path | |
| 78 | + * @param fileName | |
| 79 | + * @return | |
| 80 | + */ | |
| 81 | + public static boolean isDirectory(final String path, final String fileName) { | |
| 82 | + return getFile(path, fileName).isDirectory(); | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * 파일관련 : 디렉토리 여부 | |
| 87 | + * @param path | |
| 88 | + * @return | |
| 89 | + */ | |
| 90 | + public static boolean isDirectory(final String path) { | |
| 91 | + return getFile(path).isDirectory(); | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * 파일관련 : 디렉토리 여부 | |
| 96 | + * @param file | |
| 97 | + * @return | |
| 98 | + */ | |
| 99 | + public static boolean isDirectory(final File file) { | |
| 100 | + return file.isDirectory(); | |
| 101 | + } | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * 파일관련 : 파일 여부 | |
| 105 | + * @param path | |
| 106 | + * @param fileName | |
| 107 | + * @return | |
| 108 | + */ | |
| 109 | + public static boolean isFile(final String path, final String fileName) { | |
| 110 | + return getFile(path, fileName).isFile(); | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * 파일관련 : 파일 여부 | |
| 115 | + * @param path | |
| 116 | + * @return | |
| 117 | + */ | |
| 118 | + public static boolean isFile(final String path) { | |
| 119 | + return getFile(path).isFile(); | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * 파일관련 : 파일 여부 | |
| 124 | + * @param file | |
| 125 | + * @return | |
| 126 | + */ | |
| 127 | + public static boolean isFile(final File file) { | |
| 128 | + return file.isFile(); | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * 파일관련 : 파일, 디렉토리 삭제 | |
| 133 | + * @param path | |
| 134 | + * @param fileName | |
| 135 | + * @return | |
| 136 | + */ | |
| 137 | + public static boolean deleteFile(final String path, final String fileName) { | |
| 138 | + return getFile(path, fileName).delete(); | |
| 139 | + } | |
| 140 | + | |
| 141 | + /** | |
| 142 | + * 파일관련 : 파일, 디렉토리 삭제 | |
| 143 | + * @param path | |
| 144 | + * @return | |
| 145 | + */ | |
| 146 | + public static boolean deleteFile(final String path) { | |
| 147 | + return getFile(path).delete(); | |
| 148 | + } | |
| 149 | + | |
| 150 | + /** | |
| 151 | + * 파일관련 : 파일, 디렉토리 삭제 | |
| 152 | + * @param file | |
| 153 | + * @return | |
| 154 | + */ | |
| 155 | + public static boolean deleteFile(final File file) { | |
| 156 | + return file.delete(); | |
| 157 | + } | |
| 158 | + | |
| 159 | + /** | |
| 160 | + * 파일관련 : 파일 크기 | |
| 161 | + * @param path | |
| 162 | + * @param fileName | |
| 163 | + * @return | |
| 164 | + */ | |
| 165 | + public static long fileSize(final String path, final String fileName) { | |
| 166 | + return getFile(path, fileName).length(); | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * 파일관련 : 파일 크기 | |
| 171 | + * @param path | |
| 172 | + * @return | |
| 173 | + */ | |
| 174 | + public static long fileSize(final String path) { | |
| 175 | + return getFile(path).length(); | |
| 176 | + } | |
| 177 | + | |
| 178 | + /** | |
| 179 | + * 파일관련 : 파일 크기 | |
| 180 | + * @param file | |
| 181 | + * @return | |
| 182 | + */ | |
| 183 | + public static long fileSize(final File file) { | |
| 184 | + return file.length(); | |
| 185 | + } | |
| 186 | + | |
| 187 | + /** | |
| 188 | + * 파일관련 : 다중 디렉토리 생성 | |
| 189 | + * @param dirs | |
| 190 | + * @return | |
| 191 | + */ | |
| 192 | + public static boolean makeMultiDirectory(final String dirs) { | |
| 193 | + return getFile(dirs).mkdirs(); | |
| 194 | + } | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * 파일관련 : 디렉토리 생성 | |
| 198 | + * @param dir | |
| 199 | + * @return | |
| 200 | + */ | |
| 201 | + public static boolean makeDirectory(final String dir) { | |
| 202 | + return getFile(dir).mkdir(); | |
| 203 | + } | |
| 204 | + | |
| 205 | + /** | |
| 206 | + * 파일관련 : 파일 확장자 추출 | |
| 207 | + * @param fileName | |
| 208 | + * @return | |
| 209 | + */ | |
| 210 | + public static String getExtension(final String fileName) { | |
| 211 | + if (fileName == null) { | |
| 212 | + return null; | |
| 213 | + } | |
| 214 | + if (fileName.lastIndexOf(".") >= 0) { | |
| 215 | + return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); | |
| 216 | + } | |
| 217 | + | |
| 218 | + return null; | |
| 219 | + } | |
| 220 | + | |
| 221 | + /** | |
| 222 | + * 파일관련 : 파일 확장자 추출 | |
| 223 | + * @param file | |
| 224 | + * @return | |
| 225 | + */ | |
| 226 | + public static String getExtension(final File file) { | |
| 227 | + return getExtension(file.getName()); | |
| 228 | + } | |
| 229 | + | |
| 230 | + /** | |
| 231 | + * 파일관련 : 파일 명 추출 | |
| 232 | + * @param fileName | |
| 233 | + * @return | |
| 234 | + */ | |
| 235 | + public static String getFileName(final String fileName) { | |
| 236 | + if (fileName == null) { | |
| 237 | + return null; | |
| 238 | + } | |
| 239 | + if (fileName.lastIndexOf(".") >= 0) { | |
| 240 | + return fileName.substring(0, fileName.lastIndexOf(".")); | |
| 241 | + } | |
| 242 | + | |
| 243 | + return fileName; | |
| 244 | + } | |
| 245 | + | |
| 246 | + /** | |
| 247 | + * 파일관련 : 파일 명 추출 | |
| 248 | + * @param file | |
| 249 | + * @return | |
| 250 | + */ | |
| 251 | + public static String getFileName(final File file) { | |
| 252 | + return getFileName(file.getName()); | |
| 253 | + } | |
| 254 | + | |
| 255 | + /** | |
| 256 | + * 파일관련 : 저장 파일 명 생성 | |
| 257 | + * @param fileName | |
| 258 | + * @return | |
| 259 | + */ | |
| 260 | + public static String getSaveFileName(final String fileName) { | |
| 261 | + if (fileName != null && fileName.lastIndexOf(".") >= 0) { | |
| 262 | + return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + SerialNoUtil.getMultiAlphabet(3) + SerialNoUtil.getMultiNumeric(3) + SerialNoUtil.getMultiAlphabet(3) + SerialNoUtil.getMultiNumeric(7) + "." + getExtension(fileName); | |
| 263 | + } | |
| 264 | + | |
| 265 | + return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + SerialNoUtil.getMultiAlphabet(3) + SerialNoUtil.getMultiNumeric(3) + SerialNoUtil.getMultiAlphabet(3) + SerialNoUtil.getMultiNumeric(7); | |
| 266 | + } | |
| 267 | + | |
| 268 | + /** | |
| 269 | + * 파일관련 : 저장 파일 명 생성 | |
| 270 | + * @param file | |
| 271 | + * @return | |
| 272 | + */ | |
| 273 | + public static String getSaveFileName(final File file) { | |
| 274 | + return getSaveFileName(file.getName()); | |
| 275 | + } | |
| 276 | + | |
| 277 | + /** | |
| 278 | + * 파일관련 : 파일 복사 | |
| 279 | + * @param srcDir | |
| 280 | + * @param srcFileName | |
| 281 | + * @param destDir | |
| 282 | + * @return | |
| 283 | + * @throws IOException | |
| 284 | + */ | |
| 285 | + public static boolean transferFile(final String srcDir, final String srcFileName, final String destDir) throws IOException { | |
| 286 | + return transferFile(srcDir, srcFileName, destDir, srcFileName); | |
| 287 | + } | |
| 288 | + | |
| 289 | + /** | |
| 290 | + * 파일관련 : 파일 복사 | |
| 291 | + * @param srcDir | |
| 292 | + * @param srcFileName | |
| 293 | + * @param destDir | |
| 294 | + * @param destFileName | |
| 295 | + * @return | |
| 296 | + * @throws IOException | |
| 297 | + */ | |
| 298 | + public static boolean transferFile(final String srcDir, final String srcFileName, final String destDir, final String destFileName) throws IOException { | |
| 299 | + if (exist(srcDir + File.separator + srcFileName)) { | |
| 300 | + if (exist(destDir) == false) { | |
| 301 | + makeMultiDirectory(destDir); | |
| 302 | + } | |
| 303 | + | |
| 304 | + return transferFile(srcDir + File.separator + srcFileName, destDir + File.separator + destFileName); | |
| 305 | + } | |
| 306 | + | |
| 307 | + return false; | |
| 308 | + } | |
| 309 | + | |
| 310 | + /** | |
| 311 | + * 파일관련 : 파일 복사 | |
| 312 | + * @param srcPath | |
| 313 | + * @param destPath | |
| 314 | + * @return | |
| 315 | + * @throws IOException | |
| 316 | + */ | |
| 317 | + public static boolean transferFile(final String srcPath, final String destPath) throws IOException { | |
| 318 | + return transferFile(new File(srcPath), new File(destPath)); | |
| 319 | + } | |
| 320 | + | |
| 321 | + /** | |
| 322 | + * 파일관련 : 파일 복사 | |
| 323 | + * @param srcFile | |
| 324 | + * @param destFile | |
| 325 | + * @return | |
| 326 | + * @throws IOException | |
| 327 | + */ | |
| 328 | + public static boolean transferFile(final File srcFile, final File destFile) throws IOException { | |
| 329 | + log.info("srcFile : {}", srcFile); | |
| 330 | + log.info("destFile : {}", destFile); | |
| 331 | + FileInputStream fis = new FileInputStream(srcFile); | |
| 332 | + FileOutputStream fos = new FileOutputStream(destFile); | |
| 333 | + | |
| 334 | + FileChannel inputChannel = fis.getChannel(); | |
| 335 | + FileChannel outputChannel = fos.getChannel(); | |
| 336 | + | |
| 337 | + boolean transferStatus = false; | |
| 338 | + if (inputChannel.size() > 0) { | |
| 339 | + transferStatus = true; | |
| 340 | + inputChannel.transferTo(0, inputChannel.size(), outputChannel); | |
| 341 | + } | |
| 342 | + | |
| 343 | + fis.close(); | |
| 344 | + fos.close(); | |
| 345 | + | |
| 346 | + return transferStatus; | |
| 347 | + } | |
| 348 | + | |
| 349 | + public static String getJobDirectory(final String rootDirectory, final String orderNo, final String companyNo, final String date) { | |
| 350 | + return rootDirectory + File.separator + orderNo + File.separator + orderNo + "_" + companyNo + File.separator + date; | |
| 351 | + } | |
| 352 | + | |
| 353 | + /** | |
| 354 | + * 파일관련 : 다중 파일 업로드 | |
| 355 | + * @param path | |
| 356 | + * @param listMultipartFile | |
| 357 | + * @return | |
| 358 | + * @throws IOException | |
| 359 | + */ | |
| 360 | + public static List<FileBaseDto> uploadMultiFile(final String path, final List<MultipartFile> listMultipartFile) throws IOException { | |
| 361 | + return uploadMultiFileEnableExtension(path, listMultipartFile, null); | |
| 362 | + } | |
| 363 | + | |
| 364 | + /** | |
| 365 | + * 파일관련 : 다중 파일 업로드 : 허용 확장자 | |
| 366 | + * @param path | |
| 367 | + * @param listMultipartFile | |
| 368 | + * @param listEnableExtension | |
| 369 | + * @return | |
| 370 | + * @throws IOException | |
| 371 | + */ | |
| 372 | + public static List<FileBaseDto> uploadMultiFileEnableExtension(final String path, final List<MultipartFile> listMultipartFile, final String[] listEnableExtension) throws IOException { | |
| 373 | + if (listMultipartFile == null || listMultipartFile.size() == 0) { | |
| 374 | + return null; | |
| 375 | + } | |
| 376 | + | |
| 377 | + List<FileBaseDto> listFile = null; | |
| 378 | + for (MultipartFile multipartFile : listMultipartFile) { | |
| 379 | + if (multipartFile.isEmpty()) { | |
| 380 | + continue; | |
| 381 | + } | |
| 382 | + | |
| 383 | + FileBaseDto fileBaseDto = uploadFileEnableExtension(path, multipartFile, listEnableExtension); | |
| 384 | + if (fileBaseDto != null) { | |
| 385 | + if (listFile == null) { | |
| 386 | + listFile = new ArrayList<>(); | |
| 387 | + } | |
| 388 | + listFile.add(fileBaseDto); | |
| 389 | + } | |
| 390 | + } | |
| 391 | + return listFile; | |
| 392 | + } | |
| 393 | + | |
| 394 | + /** | |
| 395 | + * 파일관련 : 파일 업로드 | |
| 396 | + * @param path | |
| 397 | + * @param multipartFile | |
| 398 | + * @return | |
| 399 | + * @throws IOException | |
| 400 | + */ | |
| 401 | + public static FileBaseDto uploadFile(final String path, final MultipartFile multipartFile) throws IOException { | |
| 402 | + return uploadFileEnableExtension(path, multipartFile, null); | |
| 403 | + } | |
| 404 | + | |
| 405 | + /** | |
| 406 | + * 파일관련 : 파일 업로드 : 허용 확장자 | |
| 407 | + * @param path | |
| 408 | + * @param multipartFile | |
| 409 | + * @param listEnableExtension | |
| 410 | + * @return | |
| 411 | + * @throws IOException | |
| 412 | + */ | |
| 413 | + public static FileBaseDto uploadFileEnableExtension(final String path, final MultipartFile multipartFile, final String[] listEnableExtension) throws IOException { | |
| 414 | + if (multipartFile == null || multipartFile.isEmpty()) { | |
| 415 | + return null; | |
| 416 | + } | |
| 417 | + if (exist(path) == false) { | |
| 418 | + makeMultiDirectory(path); | |
| 419 | + } | |
| 420 | + | |
| 421 | + boolean enableUpload = true; | |
| 422 | + if (listEnableExtension != null && listEnableExtension.length > 0) { | |
| 423 | + enableUpload = false; // 가능한 확장자를 만나면 다시 true | |
| 424 | + String extension = getExtension(multipartFile.getOriginalFilename()); | |
| 425 | + if (extension != null) { | |
| 426 | + for (String enableExtension : listEnableExtension) { | |
| 427 | + if (extension.equals(enableExtension)) { | |
| 428 | + enableUpload = true; | |
| 429 | + break; | |
| 430 | + } | |
| 431 | + } | |
| 432 | + } | |
| 433 | + } | |
| 434 | + | |
| 435 | + FileBaseDto fileBaseDto = null; | |
| 436 | + if (enableUpload) { | |
| 437 | + fileBaseDto = new FileBaseDto(); | |
| 438 | + fileBaseDto.setOriginFileName(multipartFile.getOriginalFilename()); | |
| 439 | + fileBaseDto.setSaveFileName(getSaveFileName(multipartFile.getOriginalFilename())); | |
| 440 | + fileBaseDto.setFileSize(multipartFile.getSize()); | |
| 441 | + | |
| 442 | + String uploadPath = path + File.separator + fileBaseDto.getSaveFileName(); | |
| 443 | + File uploadFile = new File(uploadPath); | |
| 444 | + multipartFile.transferTo(uploadFile); | |
| 445 | + } | |
| 446 | + | |
| 447 | + return fileBaseDto; | |
| 448 | + } | |
| 449 | + | |
| 450 | + public static Long uploadFileSize(final MultipartFile multipartFile) { | |
| 451 | + if (multipartFile == null || multipartFile.isEmpty()) { | |
| 452 | + return -1L; | |
| 453 | + } | |
| 454 | + | |
| 455 | + return multipartFile.getSize(); | |
| 456 | + } | |
| 457 | + /* */ | |
| 458 | + public static void main(String[] args) throws IOException { | |
| 459 | + System.out.println(); | |
| 460 | +// File file = new File("d:\\example\\image.jpg"); | |
| 461 | +// long bytes = file.length(); | |
| 462 | +// long kilobyte = bytes / 1024; | |
| 463 | +// long megabyte = kilobyte / 1024; | |
| 464 | +// System.out.println(bytes + " byte"); // 3980059 byte System.out.println(kilobyte + " kb"); // 3886 kb System.out.println(megabyte + " mb"); // 3 mb | |
| 465 | +// File file = new File("c:\\example\\new_directory\\test.txt"); | |
| 466 | +// boolean directoryCreated = file.mkdirs(); | |
| 467 | + System.out.println("ext : " + JobFileFactory.getExtension("test")); | |
| 468 | + System.out.println("name : " + JobFileFactory.getFileName("test")); | |
| 469 | + System.out.println("name : " + JobFileFactory.getSaveFileName("test.txt")); | |
| 470 | + System.out.println("exist : " + JobFileFactory.exist("C:\\uploads\\20231129\\30e718ce1445499a9ef1c4929310c87f.xlsx")); | |
| 471 | + System.out.println("Now : " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))); | |
| 472 | +// System.out.println("transfer : " + JobFileFactory.transferFile("C:\\Docs", "test.txt", "C:\\Docs", "test2.txt")); | |
| 473 | + } | |
| 474 | +} |
+++ src/main/java/com/munjaon/server/util/SerialNoUtil.java
... | ... | @@ -0,0 +1,43 @@ |
| 1 | +package com.munjaon.server.util; | |
| 2 | + | |
| 3 | +import java.time.LocalDateTime; | |
| 4 | +import java.time.format.DateTimeFormatter; | |
| 5 | + | |
| 6 | +public final class SerialNoUtil { | |
| 7 | + public static String getSerialNo() { | |
| 8 | + return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + getMultiAlphabet(3) + getMultiNumeric(3) + getMultiAlphabet(2) + getMultiNumeric(6); | |
| 9 | + } | |
| 10 | + | |
| 11 | + public static String getMultiAlphabet(final int count) { | |
| 12 | + if (count <= 0) { | |
| 13 | + return String.valueOf((char) ((Math.random() * 26) + 65)); | |
| 14 | + } | |
| 15 | + | |
| 16 | + StringBuilder retValue = new StringBuilder(); | |
| 17 | + for (int i = 0; i < count; i++) { | |
| 18 | + retValue.append((char) ((Math.random() * 26) + 65)); | |
| 19 | + } | |
| 20 | + | |
| 21 | + return retValue.toString(); | |
| 22 | + } | |
| 23 | + | |
| 24 | + public static String getMultiNumeric(final int count) { | |
| 25 | + if (count <= 0) { | |
| 26 | + return String.valueOf((int) (Math.random() * 10)); | |
| 27 | + } | |
| 28 | + | |
| 29 | + StringBuilder retValue = new StringBuilder(); | |
| 30 | + for (int i = 0; i < count; i++) { | |
| 31 | + retValue.append((int) (Math.random() * 10)); | |
| 32 | + } | |
| 33 | + | |
| 34 | + return retValue.toString(); | |
| 35 | + } | |
| 36 | + | |
| 37 | + public static String getNumber(String str) { | |
| 38 | + if (str == null) { | |
| 39 | + return null; | |
| 40 | + } | |
| 41 | + return str.replaceAll("[^0-9]",""); | |
| 42 | + } | |
| 43 | +} |
+++ src/main/java/com/munjaon/server/util/dto/FileBaseDto.java
... | ... | @@ -0,0 +1,15 @@ |
| 1 | +package com.munjaon.server.util.dto; | |
| 2 | + | |
| 3 | +import lombok.Getter; | |
| 4 | +import lombok.Setter; | |
| 5 | +import lombok.ToString; | |
| 6 | + | |
| 7 | +@Getter | |
| 8 | +@Setter | |
| 9 | +@ToString | |
| 10 | +public class FileBaseDto { | |
| 11 | + protected String originFileName; // 원본 파일명 | |
| 12 | + protected String saveFileName; // 저장 파일명 | |
| 13 | + protected long fileSize; // 파일 크기 | |
| 14 | + protected String createdDt; | |
| 15 | +} |
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?