๋ ์ฌ์ฉํ์ง๋ง, ์ง์ ๋ง๋ค์ด ๋ณด์ง๋ ๋ชปํ๋ ํ์ผ ์ ๋ก๋ ๋ง๋ค๊ธฐ๋ฅผ ํด ๋ณผ ์๊ฐ์ด๋ค.
์์ ํ๊ฒฝ์ ๋ค์๊ณผ ๊ฐ๋ค.
1. Java 11
2. Spring Boot 2.6.4
3. Gradle 7.2
4. War ๋ฐฐํฌ ์ฌ์ฉ
FileUpload / Download ์๋น์ค๋ ๋ค์์ ์ ์ ๋ก ๊ฐ๋ฐํ๊ธฐ๋ก ํ๋ค.
1. Front์์ MultipartRequest๋ก ํ์ผ ์์ฒญ์ ๋ฐ๋๋ค. ํผ ํ๊ทธ ๋ด์์ Submitํ๋ ๋ฐฉ์์ ์ฐ์ ์ ์ผ๋ก ๊ตฌํํ๋ค.
2. Back์์๋ ์ด๋ฅผ ๋ฐ์ ์๋ฒ์ ์ ์ฅํ๊ณ , ๊ด๋ จ ์ ๋ณด๋ฅผ DB์ ์ ์ฅํ๋ค.
3. ์๋ฒ ๊ฒฝ๋ก์ ์ ์ฅํ๋ ๊ณผ์ ์๋ ํ์ฅ์ ๋ฑ์ ๋ํด ์์ธ์ฒ๋ฆฌ๋ ์ค์ ํ๋ค.
4. ํ์ผ ์ ๋ก๋๋ ๋ค์ค ์ ๋ก๋๋ฅผ ์ง์ํด์ผ ํ๋ฉฐ, ํ์ ์ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ก๋ ํ๋ค.
5. ์ถํ, 1-4 ๊ตฌํ ์ Form Submit ์ธ์ Axios ๋ฑ์ ์ด์ฉํ ajax ํธ์ถ๋ก๋ ๊ตฌํ์ ์ค์ํ๋ค.
๊ทธ๋ฆฌํ๋ฉฐ, ๋ ๊ฐ์ง ๋ฐฉ์์ ์ ๋ถ ์ง์ํ๋๋ก ํ์ฌ ์ต์ข
์ ์ผ๋ก๋ ๋ชจ๋๋ก ๋ถ๋ฆฌํ์ฌ ์ฌ์ฉ๊ฐ๋ฅํ๋๋ก ํ๋ค.
์ฐ์ ๊ธฐ๋ณธ์ ์ผ๋ก ํ๋ก์ ํธ ์ธํ ๋ฐ ๊ธฐ๋ณธ ๊ธฐ๋ฅ ํ ์คํธ ๋ถํฐ ์งํํ๋ค.
ํ๋ก์ ํธ๋ vs code์์ initailize gradle project๋ฅผ ์ด์ฉํด ์์ฑํ๊ณ ์๋์ ๊ฐ์ ๋ด์ฉ์ ์ถ๊ฐ ํ๋ค.
- build.gradle
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'
}
tasks.named('test') {
useJUnitPlatform()
}
- application.yml
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
- indexController.java
package com.file.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
public class IndexController {
/*
* @GetMapping("/")
* public ModelAndView index() {
* return new ModelAndView("/index.html");
* }
*/
@GetMapping("/")
public ModelAndView index2() {
return new ModelAndView("/index");
}
}
- 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;
@PostMapping("/upload.do")
public void upload(MultipartRequest req) {
System.out.println("๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ [LOG] : " + req + "๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ");
fsvc.save(req);
}
}
- FileUploadService.java - FileStorageService ์์.
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 com.file.example.ifc.FileStorageService;
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;
@Override
public void init() {
}
@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 !");
}
}
}
- FileStorageService.java
package com.file.example.ifc;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
public interface FileStorageService {
void init();
void save(MultipartRequest req);
}
์ฐ์ ์ด ์ ๋๊น์ง๋ง ์์ฑํ๊ณ ํ ์คํธ ํด๋ณด์์ ๋๋, ์ ์์ ์ผ๋ก ๋์ํ๋ค. application.yml ์ ๊ฒฝ๋ก๋ ํด๋น ํ๋ก์ ํธ ๋ด๋ถ ๊ฒฝ๋ก๋ก ์์๋ก ์ก์๋ค. ๋ง์ฝ ํด๋น ํ๊ฒฝ์ด ์ด์์ผ๋ก ๋์ด๊ฐ๋ค๋ฉด, ์ ๋ก๋ ํ๊ณ ์ ํ๋ ๊ณณ์ ๊ฒฝ๋ก๋ฅผ ์ ๋๊ฒฝ๋ก๋ก ์ฝ์ ํ๋ฉด ๋ ๊ฒ์ด๋ค.
๋ฒ ์ด์ค๋ง ์ก์๋๊ณ , ์ฒ์ฒํ ์คํ ๋ฐ์ด ์คํ ์ผ๋ก ์งํํ ์์ ์ด๋ค.
'๐ฎSpring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring Boot] ํ์ผ ์ ๋ก๋ ๋ง๋ค๊ธฐ -3- (0) | 2022.03.27 |
---|---|
[Spring Boot] ํ์ผ ์ ๋ก๋ ๋ง๋ค๊ธฐ -2- (0) | 2022.03.25 |
[Spring Boot] Tiles ์ฌ์ฉํ๊ธฐ. (0) | 2022.02.02 |
[Spring Boot] Tag Library ์ฌ์ฉํ๊ธฐ (0) | 2022.02.02 |
[Spring Boot] Application Context ์ฌ์ฉํ๊ธฐ (0) | 2022.02.02 |
๋๊ธ