public List saveFiles(List files) throws RuntimeException {
if (files == null || files.isEmpty())
return Collections.EMPTY_LIST;
List uploadNames = new ArrayList<>();
files.stream().forEach(file -> {
String savedName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
Path savePath = Paths.get(uploadPath, savedName);
try {
Files.copy(file.getInputStream(), savePath, StandardCopyOption.REPLACE_EXISTING);
uploadNames.add(savedName);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
return uploadNames;
public List saveFiles(List files) throws RuntimeException {
if (files == null || files.isEmpty())
return Collections.EMPTY_LIST;
return files.stream().map(this::uploadFile).toList();
}
private String uploadFile(MultipartFile file) throws RuntimeException {
if (file == null || file.isEmpty())
throw new RuntimeException("File is empty");
String savedName = UUID.randomUUID() + "_" + file.getOriginalFilename();
Path savePath = Paths.get(uploadPath, savedName);
try {
// 파일 복사
Files.copy(file.getInputStream(), savePath, StandardCopyOption.REPLACE_EXISTING);
// 썸네일 처리 - MIME 유형으로 이미지 확인 후 복사
if (file.getContentType() != null && file.getContentType().startsWith("image/")) {
Path thumbnailPath = Paths.get(uploadPath, "s_" + savedName);
Thumbnails.of(savePath.toFile()).size(200, 200).toFile(thumbnailPath.toFile());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return savedName;
}
public ResponseEntity getFile(String fileName) {
Resource resource = new FileSystemResource(uploadPath + File.separator + fileName);
if (!resource.isReadable())
resource = new FileSystemResource(uploadPath + File.separator + "default.ico");
HttpHeaders headers = new HttpHeaders();
try {
headers.add("Content-Type", Files.probeContentType(resource.getFile().toPath()));
} catch (IOException e) {
throw new RuntimeException(e);
}
return ResponseEntity.ok().headers(headers).body(resource);
}