[Spring Boot] ํ์ผ ์ ๋ก๋ ๋ง๋ค๊ธฐ -2-
์ด๋ฒ์ ์งํํ ๊ฒ์ ๊ฐ๋จํ๊ฒ, ์ ๋ก๋ ๋ ํ์ผ์ ์ ๋ณด๋ฅผ 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