๐Ÿ”ฎSpring

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

harry.93 2022. 3. 25. 00:47
๋ฐ˜์‘ํ˜•

์ด๋ฒˆ์— ์ง„ํ–‰ํ•œ ๊ฒƒ์€ ๊ฐ„๋‹จํ•˜๊ฒŒ, ์—…๋กœ๋“œ ๋œ ํŒŒ์ผ์˜ ์ •๋ณด๋ฅผ DB์— ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ–ˆ๋‹ค.

๋ณ„๋‹ค๋ฅธ ๋กœ์ง์€ ์—†๊ณ  ๋‹จ์ˆœ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€ DB์— ์ €์žฅํ•œ๋‹ค.

 

- build.gradle : DB๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ (DB๋Š” Docker ๋ฒ ์ด์Šค์˜ Postgres ์‚ฌ์šฉ)

plugins {
	id 'org.springframework.boot' version '2.6.4'
	id 'io.spring.dependency-management' version '1.0.11.RELEASE'
	id 'java'
	id 'war'
}

group = 'com.file'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	
	// dev tool ( ์ •์  ๋ฆฌ์†Œ์Šค ์žฌ์‹œ์ž‘์—†์ด ์ ์šฉ )
	implementation group: 'org.springframework.boot', name: 'spring-boot-devtools'

	// jstl 
	implementation group: 'javax.servlet', name: 'jstl', version: '1.2'

	// jasper
	implementation group: 'org.apache.tomcat', name: 'tomcat-jasper', version: '9.0.56'

	// DB๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ. log4j๋กœ ์ถ”ํ›„ ๋กœ๊ทธ ์‚ฌ์šฉ ๊ณ ๋ ค
	// mybatis-spring-boot
	implementation group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '2.1.4'

	// jdbc ( postgresql - jdbc driver )
	implementation group: 'org.postgresql', name: 'postgresql', version: '42.3.1'

	// log4j2
	implementation group: 'org.bgee.log4jdbc-log4j2', name: 'log4jdbc-log4j2-jdbc4.1', version: '1.16'   
}

tasks.named('test') {
	useJUnitPlatform()
}

 

- application.yml : DB์„ค์ • ์ถ”๊ฐ€

spring:
    application:
        name: file-upload-test
    servlet:
        multipart:
            location: /Users/dk/Documents/GitHub/Library/Spring/FileUpload/SpringBoot/example/src/main/resources/upload
    mvc:
        view:
            prefix: /WEB-INF/views/
            suffix: .jsp
    # DB ์ถ”๊ฐ€.
    datasource:
        hikari:
            jdbc-url: jdbc:log4jdbc:postgresql://localhost:5432/postgres
            driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
            username: postgres
            password: postgres
            maximum-pool-size: 5

 

- FileUploadController.java

package com.file.example.controller;

import com.file.example.service.FileUploadService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

@RestController
public class FileUploadController {

    @Autowired
    FileUploadService fsvc;

    /**
     * ํŒŒ์ผ์—…๋กœ๋“œ1 - ๋‹จ์ˆœ ํŒŒ์ผ ์„œ๋ฒ„ ๊ฒฝ๋กœ(ํ”„๋กœ์ ํŠธ ๊ฒฝ๋กœ) ์—…๋กœ๋“œ
     * 
     * @param req
     */
    @PostMapping("/upload.do")
    public void upload(MultipartRequest req) {
        System.out.println("๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ [LOG] : " + req + "๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ");
        fsvc.save(req);
    }

    /**
     * ํŒŒ์ผ์—…๋กœ๋“œ2 - ์„œ๋ฒ„์— ์—…๋กœ๋“œ ํ›„, ํŒŒ์ผ ์ •๋ณด DB ์ €์žฅ
     * 
     * @param req
     */
    @PostMapping("/upload2.do")
    public void upload2(MultipartRequest req) {
        System.out.println("๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ [LOG] : " + req + "๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ");
        fsvc.save2(req);
    }
}

 

- FileUploadService.java

package com.file.example.service;

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.util.HashMap;

import com.file.example.ifc.FileStorageService;
import com.file.example.repository.FileUploadRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

@Service
public class FileUploadService implements FileStorageService {

    @Value("${spring.servlet.multipart.location}")
    private String uploadPath;

    @Autowired
    FileUploadRepository rpt;

    @Override
    public void init() {

    }

    /**
     * ํŒŒ์ผ์—…๋กœ๋“œ1
     */
    @Override
    public void save(MultipartRequest req) {
        MultipartFile file = req.getFile("singleFile");
        try {
            if (file.isEmpty()) {
                throw new Exception("ERROR : file is empty");
            }
            Path root = Paths.get(uploadPath);
            System.out.println("๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ [LOG] : " + root + "๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ");
            System.out.println("๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ [LOG] : " + uploadPath + "๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ๏ผƒ");
            if (!Files.exists(root)) {
                try {
                    Files.createDirectories(Paths.get(uploadPath));
                } catch (Exception e) {
                    throw new Exception("ERROR : can't makr dir");
                }
            }
            try {
                InputStream is = file.getInputStream();
                Files.copy(is, root.resolve(file.getOriginalFilename()), StandardCopyOption.REPLACE_EXISTING);
            } catch (Exception e) {
                throw new Exception("ERROR : can't makr dir");
            }
        } catch (Exception e) {
            throw new RuntimeException("ERROR : can't save file !");
        }
    }

    /**
     * ํŒŒ์ผ์—…๋กœ๋“œ2
     */
    @Override
    public void save2(MultipartRequest req) {
        MultipartFile file = req.getFile("singleFile2");
        try {
            if (file.isEmpty()) {
                throw new Exception("ERROR : file is empty");
            }
            Path root = Paths.get(uploadPath);
            if (!Files.exists(root)) {
                try {
                    Files.createDirectories(Paths.get(uploadPath));
                } catch (Exception e) {
                    throw new Exception("ERROR : can't makr dir");
                }
            }
            try {
                InputStream is = file.getInputStream();
                Files.copy(is, root.resolve(file.getOriginalFilename()), StandardCopyOption.REPLACE_EXISTING);
            } catch (Exception e) {
                throw new Exception("ERROR : can't makr dir");
            }
            // ํŒŒ์ผ์ •๋ณด ์ €์žฅ
            String fileName = file.getOriginalFilename();
            String fileSize = Long.toString(file.getSize());
            String fileType = file.getContentType();
            String filePath = uploadPath + "/" + fileName;

            HashMap<String,String> fileMap = new HashMap<String,String>();
            fileMap.put("fileName", fileName);
            fileMap.put("fileSize", fileSize);
            fileMap.put("fileType", fileType);
            fileMap.put("filePath", filePath);

            rpt.insertFile(fileMap);

        } catch (Exception e) {
            throw new RuntimeException("ERROR : can't save file !");
        }
    }

}

 

- FileUploadRepository.xml (Repository๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์žก์•„์„œ ์ƒ๋žต)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.file.example.repository.FileUploadRepository">
    <insert id="insertFile" parameterType="HashMap">
        insert into file (
            seq
            ,file_name
            ,file_path
            ,file_type
            ,file_size
        ) values (
            nextval('seq')
            ,#{fileName}
            ,#{filePath}
            ,#{fileType}
            ,#{fileSize}
        )
    </insert>
</mapper>

 

- FileStorageService.java

package com.file.example.ifc;

import org.springframework.web.multipart.MultipartRequest;

public interface FileStorageService {
    void init();
    
    /**
     * Basic File Upload
     * jsp form --> multipartRequest
     * Server File Dir upload 
     * @param req
     */
    void save(MultipartRequest req);

    /**
     * File Upload 2
     * upload 1 + file information + insert DB
     */
    void save2(MultipartRequest req);

}

 

- ์—…๋กœ๋“œ ํ™”๋ฉด

 

- DB์— ์ €์žฅ๋œ ๋ชจ์Šต

 

DB์— ํŒŒ์ผ ์ €์žฅํ•˜๋Š” ๊ฒƒ๊นŒ์ง€ ํ–ˆ์œผ๋‹ˆ, ๋‹ค์Œ๋ฒˆ์—๋Š” ์•„๋ž˜ ๋‚ด์šฉ์„ ์ง„ํ–‰ํ•  ์˜ˆ์ •์ด๋‹ค.

1. ์„œ๋ฒ„ ์‚ฌ์ด๋“œ์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ (ํ™•์žฅ์ž ๋“ฑ)

2. ํŒŒ์ผ ์ด๋ฆ„ ๋ฌด์ž‘์œ„๋กœ ๋ณ€๊ฒฝ (ํŠน์ •๋žœ๋ค๋ฌธ์ž์—ด)

3. ํŒŒ์ผ ์—…๋กœ๋“œ ํ•จ์ˆ˜ ๊ณตํ†ต ์ฒ˜๋ฆฌ(๋ชจ๋“ˆํ™”)

 

์ดํ›„, ๋‹ค์ค‘ ์—…๋กœ๋“œ ๋ฐ ๊ธฐํƒ€ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

์‚ฌ์šฉ๋œ ์†Œ์Šค๋Š” ์•„๋ž˜์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

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
๋ฐ˜์‘ํ˜•