[Spring Boot] ํŒŒ์ผ ์—…๋กœ๋“œ ๋งŒ๋“ค๊ธฐ -3-

    ๋ฐ˜์‘ํ˜•

    https://lucete-stellae.tistory.com/78

    ์ง€๋‚œ๋ฒˆ์— ์ด์–ด์„œ ํŒŒ์ผ ์—…๋กœ๋“œ ํ•จ์ˆ˜์— ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ ํ•˜์˜€๋‹ค.

    1. ํŒŒ์ผ ์—…๋กœ๋“œ ํ•จ์ˆ˜์˜ ๊ณตํ†ตํ™”

    2. ํŒŒ์ผ ์˜ˆ์™ธ์ฒ˜๋ฆฌ (์‚ฌ์ด์ฆˆ,ํ™•์žฅ์ž)

    3. ํŒŒ์ผ ์ด๋ฆ„ ์•”ํ˜ธํ™”

    4. ํŒŒ์ผ ์ €์žฅ ์œ„์น˜๋ฅผ ํƒ€์ž…์— ๋”ฐ๋ผ ๋ณ€๊ฒฝ ๋ฐ ๋‚ ์งœ๋ณ„๋กœ ํด๋” ๊ตฌ๋ถ„ํ•˜๊ธฐ

     

    - FIleUtil.java

    package com.file.example.util;
    
    import java.io.File;
    import java.io.InputStream;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.StandardCopyOption;
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Arrays;
    import java.util.Base64;
    import java.util.HashMap;
    
    import com.file.example.repository.FileUploadRepository;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    import org.springframework.web.multipart.MultipartFile;
    
    /**
     * ํŒŒ์ผ ์œ ํ‹ธ ํด๋ž˜์Šค
     */
    @Component
    public class FileUtil {
        // ์„œ๋ฒ„ ์—…๋กœ๋“œ ๊ฒฝ๋กœ
        @Value("${spring.servlet.multipart.location}")
        private String uploadPath;
    
        // ์ตœ๋Œ€ ํŒŒ์ผ ํฌ๊ธฐ
        private long MAX_SIZE = 52428800;
    
        // ํŒŒ์ผ ์ •๋ณด ์ €์žฅ ์‹œ, ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ
        @Autowired
        FileUploadRepository rpt;
    
        /**
         * ์—…๋กœ๋“œ ๊ฒฝ๋กœ ๊ตฌํ•˜๊ธฐ
         * 
         * @param type
         * @return
         */
        public Path getUploadPath(String type) {
            // ํŒŒ์ผ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‚ ์งœ ๊ธฐ์ค€ (yyyymmdd) ์œผ๋กœ ํด๋”๋ฅผ ๊ตฌ๋ถ„
            LocalDate ld = LocalDate.now();
            String date = ld.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            String typeFolder = "";
            // ํƒ€์ž…์— ๋”ฐ๋ผ ๋‚ ์งœ ๋‚ด๋ถ€์— ํด๋” ๊ตฌ๋ถ„
            if (type.equals("image")) {
                typeFolder = "image";
            } else if (type.equals("document")) {
                typeFolder = "document";
            } else {
                typeFolder = "";
            }
            // ์—…๋กœ๋“œ ๊ฒฝ๋กœ๋ฅผ ์กฐํ•ฉ
            uploadPath += File.separator + date + File.separator + typeFolder;
            // ์กฐํ•ฉ๋œ ๊ฒฝ๋กœ ์ฒดํฌ
            Path dir = Paths.get(uploadPath);
            // ํ•ด๋‹น ๊ฒฝ๋กœ ์กด์žฌํ•˜๋Š”์ง€ ์ฒดํฌ
            if (!Files.exists(dir)) {
                try {
                    // ๊ฒฝ๋กœ๊ฐ€ ์—†๋‹ค๋ฉด ์ƒ์„ฑ
                    Files.createDirectories(dir);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
            return dir;
        }
    
        /**
         * ์—…๋กœ๋“œ ํ•˜๊ธฐ
         * 
         * @param file
         * @param path
         */
        public HashMap<String, String> upload(MultipartFile file, Path path) {
            HashMap<String, String> result = new HashMap<String, String>();
            // ํŒŒ์ผ ์ •๋ณด
            String fileName = file.getOriginalFilename();
            String fileSize = Long.toString(file.getSize());
            String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1);
            String fileType = file.getContentType();
            String filePath = "";
            // ๊ฒฐ๊ณผ ์ •๋ณด
            String status = "";
            String message = "";
            // ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•˜๊ธฐ
    
            // 1. ํŒŒ์ผ ์‚ฌ์ด์ฆˆ
            if (file.getSize() > MAX_SIZE) {
                status = "fail";
                message = "file over max upload size";
                result.put("status", status);
                result.put("message", message);
                return result;
            }
    
            // 2. ํŒŒ์ผ ํ™•์žฅ์ž
            // ํ™”์ดํŠธ ๋ฆฌ์ŠคํŠธ ๋ฐฉ์‹์œผ๋กœ ํŒŒ์ผ ํ™•์žฅ์ž ์ฒดํฌ
            if (!Arrays.asList("jpg", "png", "gif", "jpeg", "bmp", "xlsx", "ppt", "pptx", "txt", "hwp")
                    .contains(fileType.toLowerCase())) {
                status = "fail";
                message = "file type is not allowed";
                result.put("status", status);
                result.put("message", message);
                return result;
            }
    
            // 3. ์ €์žฅ ํŒŒ์ผ ์ด๋ฆ„ ๋žœ๋คํ™”
            String tempName = fileName.substring(0, fileName.lastIndexOf("."));
            String encFileName = Base64.getEncoder().encodeToString(tempName.getBytes());
            // ์•”ํ˜ธํ™”๋œ ๊ฒฝ๋กœ๋กœ ํŒจ์Šค ์„ค์ •
            filePath = path.toString() + File.separator + encFileName + "." + fileExt;
    
            // 4. ํŒŒ์ผ์ •๋ณด ๋งต์— ๋‹ด๊ธฐ.
            HashMap<String, String> fileInfo = new HashMap<String, String>();
    
            fileInfo.put("fileName", fileName);
            fileInfo.put("encFileName", encFileName);
            fileInfo.put("fileSize", fileSize);
            fileInfo.put("fileExt", fileExt);
            fileInfo.put("fileType", fileType);
            fileInfo.put("filePath", filePath);
            
            try {
                InputStream is = file.getInputStream();
                Files.copy(is, path.resolve(encFileName + "." + fileExt), StandardCopyOption.REPLACE_EXISTING);
    
                // ํŒŒ์ผ ์ €์žฅ์— ์„ฑ๊ณตํ•˜๋ฉด DB์— ์ €์žฅํ•˜๊ธฐ
                int seq = rpt.insertFile(fileInfo);
    
                status = "success";
                message = "upload complete";
            } catch (Exception e) {
                e.printStackTrace();
                status = "fail";
                message = "upload fail";
            }
            result.put("status", status);
            result.put("message", message);
            return result;
        }
    }

     

    - FileUploadService.java

    @Override
        public HashMap<String, String> save3(MultipartRequest req) {
            HashMap<String, String> result = new HashMap<String, String>();
            MultipartFile file = req.getFile("singleFile3");
            Path uploadPath = fu.getUploadPath("image");
            result = fu.upload(file, uploadPath);
            return result;
        }

     

    ์œ„ ๊ธฐ๋Šฅ๋“ค์„ ์ถ”๊ฐ€ํ•˜๋ฉด์„œ ํŒŒ์ผ ์—…๋กœ๋“œ๊ฐ€ ์ ์  ๊ทธ๋Ÿด๋“ฏํ•ด๋ณด์ด๋Š”๋ฐ, ์•„์ง ํ•  ์ผ์€ ๋งŽ๋‹ค.

    ์šฐ์„ , ๋‹จ์ผ ์—…๋กœ๋“œ ์ƒํ™ฉ์—์„œ ์ด๋ ‡๊ฒŒ๊นŒ์ง€ ๊ตฌํ˜„ ํ•˜์˜€์œผ๋‹ˆ ๋‹ค์Œ์—๋Š” ์•„๋ž˜์˜ ๋‚ด์šฉ์„ ์ง„ํ–‰ํ•  ์ƒ๊ฐ์ด๋‹ค.

    1. ๋‹ค์ค‘ ์—…๋กœ๋“œ ๊ตฌํ˜„.

    2. ํŒŒ์ผ ์—…๋กœ๋“œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ (Vuejs) ์‚ฌ์šฉ.

    3. ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ / ์‚ญ์ œ ์ฒ˜๋ฆฌ

     

    ์Œ.. 2๋ฒˆ์€ ์ข€ ๊ฑธ๋ฆด ๊ฒƒ๊ฐ™์•„ 1,3๋ฒˆ์„ ๋‹ค ์ฒ˜๋ฆฌํ•œ ํ›„์— ํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

    ๋‚˜์ค‘์— ์‹œ๊ฐ„์ด ๋œ๋‹ค๋ฉด ์šด์˜ํ™˜๊ฒฝ ์ฒ˜๋Ÿผ, ๋ฆฌ๋ˆ…์Šค ์„œ๋ฒ„์— ์˜ฌ๋ ค์„œ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด์•„์•ผ๊ฒ ๋‹ค.

    ์šด์˜ ํ™˜๊ฒฝ์—์„œ๋Š” ๊ฒฝ๋กœ๊ฐ€ ์œˆ๋„์šฐ๋‚˜ ๋งฅ์ฒ˜๋Ÿผ ๋‚˜์˜ค์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๊ธฐ๋„ ํ•˜๊ณ , ํ˜„์žฌ ๊ฐœ์ธ ํ”„๋กœ์ ํŠธ๋กœ ์ง„ํ–‰ํ•œ Document ์‚ฌ์ดํŠธ์—์„œ ํ˜„์žฌ๋Š” ์—๋””ํ„ฐ ๋‚ด์— ์ผ๋ฐ˜ ๋ฌธ์ž์—ด๋ฐ์ดํ„ฐ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ์–ด ๊ณต๊ฐ„ ์ฐจ์ง€๊ฐ€ ์‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ์ชฝ์—๋‹ค๊ฐ€ ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ํ•œ ๋ฒˆ ๊ฐ€์ ธ๊ฐ€์„œ ์ ์šฉํ•ด๋ณผ ์ƒ๊ฐ์ด๋‹ค. 

     

    ์œ„ ํ”„๋กœ์ ํŠธ๋Š” ์•„๋ž˜์—์„œ ๋ฐ›์•„๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    https://github.com/Chiptune93/Library/tree/main/Spring/FileUpload/SpringBoot/example

     

    GitHub - Chiptune93/Library: Source Code Example

    Source Code Example. Contribute to Chiptune93/Library development by creating an account on GitHub.

    github.com

     

     

     

     

     

    728x90
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€