박민혁 박민혁 08-01
240801 박민혁 파일 업로드 기능
@63e755f0f490a6686214777821a6455733d5d8ff
src/main/java/com/takensoft/ai_lms/lms/file/dao/FileDAO.java
--- src/main/java/com/takensoft/ai_lms/lms/file/dao/FileDAO.java
+++ src/main/java/com/takensoft/ai_lms/lms/file/dao/FileDAO.java
@@ -1,6 +1,7 @@
 package com.takensoft.ai_lms.lms.file.dao;
 
 
+import com.takensoft.ai_lms.lms.file.vo.FileVO;
 import org.egovframe.rte.psl.dataaccess.mapper.Mapper;
 
 import java.util.HashMap;
@@ -19,4 +20,20 @@
 
     // 파일 정보 조회
     List<HashMap<String, Object>> findByFileId(HashMap<String, Object> params) throws Exception;
+
+    /*
+     * @author  : 박민혁
+     * since   : 2024.08.01
+     *
+     * 파일 매니지 아이디 insert
+     */
+    int fileManageInsert(String fileMngId) throws Exception;
+
+    /*
+     * @author  : 박민혁
+     * since   : 2024.08.01
+     *
+     * 파일 정보 insert
+     */
+    int fileInsert(FileVO fileVO) throws Exception;
 }
src/main/java/com/takensoft/ai_lms/lms/file/service/FileService.java
--- src/main/java/com/takensoft/ai_lms/lms/file/service/FileService.java
+++ src/main/java/com/takensoft/ai_lms/lms/file/service/FileService.java
@@ -1,6 +1,8 @@
 package com.takensoft.ai_lms.lms.file.service;
 
 
+import org.springframework.web.multipart.MultipartFile;
+
 import java.util.HashMap;
 import java.util.List;
 
@@ -13,4 +15,13 @@
 public interface FileService {
     // 파일 정보 조회
     List<HashMap<String, Object>> findByFileId(HashMap<String, Object> params) throws Exception;
+
+    /*
+     * @author  : 박민혁
+     * since   : 2024.08.01
+     *
+     * 파일 업로드 기능 (파일 매니지 아이디 및 파일 정보 올리면서)
+     */
+    String fileUpload(MultipartFile[] files) throws Exception;
+
 }
src/main/java/com/takensoft/ai_lms/lms/file/service/impl/FileServiceImpl.java
--- src/main/java/com/takensoft/ai_lms/lms/file/service/impl/FileServiceImpl.java
+++ src/main/java/com/takensoft/ai_lms/lms/file/service/impl/FileServiceImpl.java
@@ -1,22 +1,87 @@
 package com.takensoft.ai_lms.lms.file.service.impl;
 
+import com.takensoft.ai_lms.common.idgen.service.IdgenService;
 import com.takensoft.ai_lms.lms.file.dao.FileDAO;
 import com.takensoft.ai_lms.lms.file.service.FileService;
+import com.takensoft.ai_lms.lms.file.vo.FileManageVO;
+import com.takensoft.ai_lms.lms.file.vo.FileVO;
 import lombok.RequiredArgsConstructor;
 import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.HashMap;
 import java.util.List;
+import java.util.UUID;
 
 @Service("FileService")
 @RequiredArgsConstructor
 public class FileServiceImpl extends EgovAbstractServiceImpl implements FileService {
     private final FileDAO fileDAO;
 
+    private final IdgenService fileMngIdgn;
+    private final IdgenService fileIdgn;
+
     // 파일 정보 조회
     // @Override
     public List<HashMap<String, Object>> findByFileId(HashMap<String, Object> params) throws Exception {
         return fileDAO.findByFileId(params);
     }
+
+    /*
+     * @author  : 박민혁
+     * since   : 2024.08.01
+     *
+     * 파일 업로드 기능 (파일 매니지 아이디 및 파일 정보 올리면서)
+     */
+    public String fileUpload(MultipartFile[] files) throws Exception {
+        FileManageVO fileManageVO = new FileManageVO();
+        String fileMngId = fileMngIdgn.getNextStringId();
+        fileManageVO.setFileMngId(fileMngId);
+
+        // 파일 매니지 저장
+        fileDAO.fileManageInsert(fileMngId);
+
+        String rootPath = "C:\\ai_lms\\cmmmfile\\";
+
+        for (MultipartFile file : files) {
+            String originalFileName = file.getOriginalFilename();
+            String fileName = UUID.randomUUID().toString() + "_" + originalFileName;
+
+            File tempDir = new File(rootPath);
+            if (!tempDir.exists()) {
+                tempDir.mkdirs();
+            }
+
+            Path savePath = Paths.get(rootPath + fileName);
+
+            // 파일 저장
+            Files.copy(file.getInputStream(), savePath);
+
+            // 단일 파일들 저장
+            FileVO fileVO = new FileVO();
+
+            String fileId = fileIdgn.getNextStringId();
+            fileVO.setFileId(fileId);
+            fileVO.setFileMngId(fileMngId);
+            fileVO.setFileNm(originalFileName);
+            fileVO.setFileEncptNm(fileName);
+            fileVO.setFileSz(String.valueOf(file.getSize()));
+            fileVO.setFileClsf(null);
+            fileVO.setFileApath(savePath.toString());
+            fileVO.setFileExtn(fileName.substring(fileName.lastIndexOf(".") + 1));
+
+            // 상대 경로 설정
+            String relativePath = savePath.toString().replace(rootPath, "");
+            fileVO.setFileRpath(relativePath);
+
+            fileDAO.fileInsert(fileVO);
+        }
+
+        return fileMngId;
+    }
 }
 
src/main/java/com/takensoft/ai_lms/lms/file/vo/FileManageVO.java (added)
+++ src/main/java/com/takensoft/ai_lms/lms/file/vo/FileManageVO.java
@@ -0,0 +1,24 @@
+package com.takensoft.ai_lms.lms.file.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+/**
+ * @author  : 박민혁
+ * since   : 2024.08.01
+ *
+ * 파일 매니지 VO
+ */
+
+
+
+@Getter
+@Setter
+@AllArgsConstructor
+@NoArgsConstructor
+public class FileManageVO {
+    // 파일 매니저 아이디
+    private String fileMngId;
+}
src/main/java/com/takensoft/ai_lms/lms/file/web/FileController.java
--- src/main/java/com/takensoft/ai_lms/lms/file/web/FileController.java
+++ src/main/java/com/takensoft/ai_lms/lms/file/web/FileController.java
@@ -4,10 +4,8 @@
 import lombok.RequiredArgsConstructor;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.util.HashMap;
 
@@ -36,4 +34,18 @@
         result.put("list", fileService.findByFileId(params));
         return new ResponseEntity<>(result, HttpStatus.OK);
     }
+
+    /**
+     * @author 박민혁
+     * @since 2024.08.01
+     *
+     * 파일 업로드 기능
+     */
+    @PostMapping("/upload.json")
+    public ResponseEntity<?> fileUpload (@RequestParam("files") MultipartFile[] files) throws Exception {
+        HashMap<String, Object> result = new HashMap<>();
+
+        result.put("fileMngId", fileService.fileUpload(files));
+        return new ResponseEntity<>(result, HttpStatus.OK);
+    }
 }
src/main/resources/mybatis/mapper/lms/files-SQL.xml
--- src/main/resources/mybatis/mapper/lms/files-SQL.xml
+++ src/main/resources/mybatis/mapper/lms/files-SQL.xml
@@ -42,4 +42,49 @@
         FROM cmmn_file
         WHERE file_mng_id = #{file_mng_id}
     </select>
+
+    <!--
+        작성자 : 박민혁
+        작성일 : 2024.08.01
+        내 용 : 파일 매니지 정보 넣기
+    -->
+    <select id="fileManageInsert"  parameterType="String">
+        INSERT INTO cmmn_file_manage
+            file_mng_id
+        VALUES (#{fileMngId})
+    </select>
+    <!--
+        작성자 : 박민혁
+        작성일 : 2024.08.01
+        내 용 : 파일 매니지 정보 넣기
+    -->
+    <select id="fileInsert"  parameterType="FileVO">
+        INSERT INTO cmmn_file(
+            file_id,
+            file_mng_id,
+            file_nm,
+            file_encpt_nm,
+            file_apath,
+            file_rpath,
+            file_extn,
+            file_sz,
+            file_clsf,
+            reg,
+            reg_dt
+            )
+        VALUES (
+            #{fileId},
+            #{fileMngId},
+            #{fileNm},
+            #{fileEncptNm},
+            #{fileApath},
+            #{fileRpath},
+            #{fileExtn},
+            #{fileSz},
+            #{fileClsf},
+            #{reg},
+            now()
+        )
+    </select>
+
 </mapper>
(파일 끝에 줄바꿈 문자 없음)
Add a comment
List