https://lucete-stellae.tistory.com/80
[Spring Boot] ํ์ผ ์ ๋ก๋ ๋ง๋ค๊ธฐ -3-
https://lucete-stellae.tistory.com/78 ์ง๋๋ฒ์ ์ด์ด์ ํ์ผ ์ ๋ก๋ ํจ์์ ๊ธฐ๋ฅ์ ์ถ๊ฐ ํ์๋ค. 1. ํ์ผ ์ ๋ก๋ ํจ์์ ๊ณตํตํ 2. ํ์ผ ์์ธ์ฒ๋ฆฌ (์ฌ์ด์ฆ,ํ์ฅ์) 3. ํ์ผ ์ด๋ฆ ์ํธํ 4. ํ์ผ ์ ์ฅ ์์น๋ฅผ
lucete-stellae.tistory.com
์ง๋ ๊ฒ์๊ธ์ ์ด์ด์, ํ์ผ ์ ๋ก๋ ๊ด๋ จ ์์ ์ ๋ค์ ์งํํ๋ค. ์ด๋ฒ์ ์ค์ ์ ๋ ๊ฒ์ ๋ค์ค ํ์ผ ์ ๋ก๋ ์ง์์ด๋ค.
ํด๋น ํ์ผ ์ ๋ก๋ ๊ธฐ๋ฅ์ ๋ค๋ฅธ Boot ํ๋ก์ ํธ์์ ๊ฐ์ ธ๋ค ์ฌ์ฉํ๋๋ฐ, ํด๋น ์๋น์ค์์๋ ํ์ผ ์ ๋ก๋ ํ ์ ๋ก๋ํ ํ์ผ ๋ฆฌ์คํธ๋ฅผ ๋ฆฌํดํ์ฌ, ์ ๋ณด๋ฅผ ํํํด์ฃผ์ด์ผ ํ๊ธฐ์ ์ด๋ฒ ๋ค์ค ํ์ผ ์ ๋ก๋ ํจ์์์๋ ํ์ผ ์ ๋ณด๋ค์ ๋ฆฌํดํ๋๋ก ์์ฑํ์๋ค.
- FileUploadController.java
@PostMapping("/upload4.do")
public HashMap upload4(@RequestParam MultipartFile[] files) {
System.out.println("๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ [LOG] : " + files + "๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ๏ผ");
return fsvc.save4(files);
}
๊ธฐ์กด MultipartRequest ๋ฅผ ๋ฐ๋ ๊ฒ๊ณผ๋ ๋ฌ๋ฆฌ, Multipart ํ์ผ ๋ฐฐ์ด์ @RequestParam์ ํตํด ์ ๋ฌ๋ฐ๋๋ค.
- FIleUploadService.java
@Override
public HashMap save4(MultipartFile[] files) {
// ์ต์ข
๊ฒฐ๊ณผ ๋ด์ ๋งต ๊ฐ์ฒด
HashMap result = new HashMap();
// ์
๋ก๋๋ ํ์ผ ๋ฆฌ์คํธ ๊ฐ์ฒด
List<HashMap> uploadFileList = new ArrayList<HashMap>();
// ์
๋ก๋ ๊ฒฝ๋ก ์ค์
Path uploadPath = fu.getUploadPath("image");
// ๋ค์ค ํ์ผ ์
๋ก๋ ๊ด๋ฆฌ๋ฅผ ์ํ master file seq
int masterSeq = fu.getMasterSeq();
// Array ๋ฐ๋ณต ํ์ฌ ํ์ผ ์
๋ก๋ ์คํ
Arrays.asList(files).stream().forEach(file -> {
uploadFileList.add(fu.multipleUpload(file, uploadPath, masterSeq));
});
// ๊ฒฐ๊ณผ๋ฅผ ๋ด์ result ๋ฆฌํด.
result.put("fileMasterSeq", masterSeq);
result.put("uploadFileList", uploadFileList);
return result;
}
์๋น์ค์์๋ ํ์ผ ๋ฐฐ์ด์ ๋ฐ์, ๊ฐ์ ๋งํผ ๋ฐ๋ณตํ๋ฉฐ ํ์ผ์ ์ ๋ก๋ ํ๊ณ , ๊ฐ ํ์ผ์ ์ ๋ก๋ ํจ์ผ๋ก์จ ๋ฆฌํด๋๋ ํ์ผ ์ ๋ณด๋ค์ ๊ฐ์ฒด์ ๋ด์ ๋ฆฌํดํ๊ฒ๋ ํ๋ค.
- FIleUtil.java
public HashMap<String, String> multipleUpload(MultipartFile file, Path path, int masterSeq) {
HashMap result = new HashMap();
// ํ์ผ ์ ๋ณด
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 = "";
String fileSeq = "";
// ์์ธ์ฒ๋ฆฌ ํ๊ธฐ
// 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(fileExt.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 fileInfo = new HashMap<String, String>();
HashMap<String, String> uploadedFileInfo = 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);
fileInfo.put("fileMasterSeq", masterSeq);
try {
InputStream is = file.getInputStream();
Files.copy(is, path.resolve(encFileName + "." + fileExt), StandardCopyOption.REPLACE_EXISTING);
// ํ์ผ ์ ์ฅ์ ์ฑ๊ณตํ๋ฉด DB์ ์ ์ฅํ๊ธฐ
fileSeq = Integer.toString(rpt.insertFile(fileInfo));
uploadedFileInfo = rpt.info(Integer.parseInt(fileSeq));
status = "success";
message = "upload complete";
} catch (Exception e) {
e.printStackTrace();
status = "fail";
message = "upload fail";
}
result.put("status", status);
result.put("message", message);
result.put("fileMasterSeq", masterSeq);
result.put("fileInfo", uploadedFileInfo);
return result;
}
์ค์ ์ ๋ก๋ ํ๋ ํจ์์ด๋ค. ๊ธฐ์กด๊ณผ ๋ค๋ฅธ ์ ์, ์ฌ๋ฌ๊ฐ์ ํ์ผ์ ํ ์์ฒญ์ ์ํด ์ฌ๋ฆฐ๊ฒ์ ํ์ ๋ฐ ๋ค๋ฅธ ๋ฐ์ดํฐ์ ์ฐ๋์์ ๊ทธ๋ฃน์ผ๋ก ์ฐพ๊ธฐ ์ฝ๋๋ก file_master ํ ์ด๋ธ์ ์ถ๊ฐํ๊ณ , master_seq ๋ฅผ ๊ฐ์ง๋๋ก ํ์ฌ ํ์ผ๊ฐ ๊ทธ๋ฃน์ ๊ตฌ๋ถ ํ๋ค๋ ๊ฒ์ด๋ค.
์ด๋ ๊ฒํ๋ฉด ์ฌ์ฉ์๊ฐ ํ์ผ์ ๋ก๋๋ฅผ ํ ๋ ์ผ๋ฐ์ ์ธ ๊ฒ์ํ์ด๋ผ๊ณ ํ๋ค๋ฉด, ๊ฒ์ํ์์ ์ฌ๋ฌ ๊ฐ ํน์ ๋จ์ผ ํ์ผ์ ๋ก๋ ์ ๋ฐ์ํ master_seq ๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉด ํด๋น ๊ฒ์๊ธ ์์ฑ ์, ์ ๋ก๋ ๋ ๋ชจ๋ ํ์ผ ์ ๋ณด๋ฅผ ์กฐํํ ์ ์๋ค.
ํด๋น ํ๋ก์ ํธ๋ ์๋์์ ๋ค์ด๋ก๋๊ฐ ๊ฐ๋ฅํ๋ค.
https://github.com/Chiptune93/spring-example/tree/main/FileUpload
GitHub - Chiptune93/spring-example: Spring Example
Spring Example. Contribute to Chiptune93/spring-example development by creating an account on GitHub.
github.com
'๐ฎSpring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SpringBoot] CORS ์ฒ๋ฆฌํ๊ธฐ (0) | 2022.06.28 |
---|---|
[AOP] AOP Aspect ๋ฅผ ์ด์ฉํ ๋ก๊ทธ ์ฒ๋ฆฌ ํ๊ธฐ (0) | 2022.04.18 |
[Spring Boot] ํ์ผ ์ ๋ก๋ ๋ง๋ค๊ธฐ -3- (0) | 2022.03.27 |
[Spring Boot] ํ์ผ ์ ๋ก๋ ๋ง๋ค๊ธฐ -2- (0) | 2022.03.25 |
[Spring Boot] ํ์ผ ์ ๋ก๋ ๋ง๋ค๊ธฐ -1- (0) | 2022.03.20 |
๋๊ธ