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
'๐ฎSpring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[AOP] AOP Aspect ๋ฅผ ์ด์ฉํ ๋ก๊ทธ ์ฒ๋ฆฌ ํ๊ธฐ (0) | 2022.04.18 |
---|---|
[Spring Boot] ํ์ผ ์ ๋ก๋ ๋ง๋ค๊ธฐ -4- (0) | 2022.03.30 |
[Spring Boot] ํ์ผ ์ ๋ก๋ ๋ง๋ค๊ธฐ -2- (0) | 2022.03.25 |
[Spring Boot] ํ์ผ ์ ๋ก๋ ๋ง๋ค๊ธฐ -1- (0) | 2022.03.20 |
[Spring Boot] Tiles ์ฌ์ฉํ๊ธฐ. (0) | 2022.02.02 |
๋๊ธ