Notice
Recent Posts
Recent Comments
Link
개발 무지렁이
[Spring Boot] 업로드된 파일의 메타정보 본문
[article/write.html]
<!DOCTYPE html>
<html lang="ko"
xmlns:th="http://thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{layout/layout.html}"
>
<head>
<title>글쓰기</title>
</head>
<body>
<section layout:fragment="content">
<div class="container">
<h1>글쓰기</h1>
<form th:action method="post" enctype="multipart/form-data">
<div>
<span>제목</span>
<input type="text" name="subject">
</div>
<div>
<span>내용</span>
<textarea name="content"></textarea>
</div>
<div>
<span>본문이미지</span>
<input type="file" name="common__bodyImg__1" accept="image/png, image/gif, image/jpeg">
<input type="file" name="common__bodyImg__2" accept="image/png, image/gif, image/jpeg">
</div>
<div>
<input type="submit" value="작성">
</div>
</form>
</div>
</section>
</body>
</html>
[Util.java]
public class Util {
public static class date {
public static String getCurrentDateFormatted(String pattern) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
return simpleDateFormat.format(new Date());
}
}
public static class file {
public static String getExt(String filename) {
return Optional.ofNullable(filename)
.filter(f -> f.contains("."))
.map(f -> f.substring(filename.lastIndexOf(".") + 1))
.orElse("");
}
public static String downloadImg(String url, String filePath) {
// 부모 디렉터리가 없으면 만들어라
new File(filePath).getParentFile().mkdirs();
// url을 통해 이미지를 받고 저장해라
byte[] imageBytes = new RestTemplate().getForObject(url, byte[].class);
try {
Files.write(Paths.get(filePath), imageBytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
String mimeType = null;
try {
// 확장자 감지
mimeType = new Tika().detect(new File(filePath));
} catch (IOException e) {
throw new RuntimeException(e);
}
// 알아낸 확장자로 바꾸기
String ext = mimeType.replaceAll("image/", "");
ext = ext.replaceAll("jpeg", "jpg");
String newFilePath = filePath + "." + ext;
// 확장자까지 붙인 바뀐파일 경로를 리턴
new File(filePath).renameTo(new File(newFilePath));
return newFilePath;
}
public static String getFileExtTypeCodeFromFileExt(String ext) {
switch (ext) {
case "jpeg":
case "jpg":
case "gif":
case "png":
return "img";
case "mp4":
case "avi":
case "mov":
return "video";
case "mp3":
return "audio";
}
return "etc";
}
public static String getFileExtType2CodeFromFileExt(String ext) {
switch (ext) {
case "jpeg":
case "jpg":
return "jpg";
case "gif":
return ext;
case "png":
return ext;
case "mp4":
return ext;
case "avi":
return ext;
case "mov":
return ext;
case "mp3":
return ext;
}
return "etc";
}
}
}
[GenFileService.java]
@Service
@RequiredArgsConstructor
public class GenFileService {
private final GenFileRepository genFileRepository;
public void saveFiles(Article article, Map<String, MultipartFile> fileMap) {
String relTypeCode = "article";
long relId = article.getId();
for(String inputName : fileMap.keySet()) {
MultipartFile multipartFile = fileMap.get(inputName);
String[] inputNameBits = inputName.split("__");
String typeCode = inputNameBits[0];
String type2Code = inputNameBits[1];
String originFileName = multipartFile.getOriginalFilename();
String fileExt = Util.file.getExt(originFileName);
String fileExtTypeCode = Util.file.getFileExtTypeCodeFromFileExt(fileExt);
String fileExtType2Code = Util.file.getFileExtType2CodeFromFileExt(fileExt);
int fileNo = Integer.parseInt(inputNameBits[2]);
int fileSize = (int) multipartFile.getSize();
String fileDir = relTypeCode + "/" + Util.date.getCurrentDateFormatted("yyyy_MM_dd");
GenFile genFile = GenFile.builder()
.relTypeCode(relTypeCode)
.relId(relId)
.typeCode(typeCode)
.type2Code(type2Code)
.fileExtTypeCode(fileExtTypeCode)
.fileExtType2Code(fileExtType2Code)
.fileNo(fileNo)
.fileSize(fileSize)
.fileDir(fileDir)
.fileExt(fileExt)
.originFileName(originFileName)
.build();
genFileRepository.save(genFile);
}
}
}
'Backend > 스프링부트' 카테고리의 다른 글
[Spring Boot] Util클래스에서 URL 한글 인코딩하기 (0) | 2022.12.30 |
---|---|
[Spring Boot] 결과보고서 RsData (0) | 2022.12.29 |
[Spring Boot] 때에 따라 업로드 개수가 다를 경우, MultipartRequest (0) | 2022.12.26 |
[Spring Boot] 프로필 이미지 업데이트와 캐시 (0) | 2022.12.25 |
[Spring Boot] 업로드한 프로필 이미지 저장과 모듈화 (0) | 2022.12.25 |
Comments