UpDownController
페이지 정보
작성자 관리자 작성일 24-06-26 12:45 조회 106 댓글 0본문
package web.controller;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.log4j.Log4j2;
import net.coobird.thumbnailator.Thumbnailator;
import web.dto.upload.UploadFileDTO;
import web.dto.upload.UploadResultDTO;
@RestController
@Log4j2
public class UpDownController {
@Value("${web1.upload.path}") // import 시에 springframework으로 시작하는 Value
private String uploadPath;
@Operation(
summary = "Upload POST",
description = "POST 방식으로 파일 등록",
requestBody = @RequestBody(
content = @Content(mediaType = "multipart/form-data",
schema = @Schema(
type = "object",
implementation = UploadFileDTO.class
)
)
)
)
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public List<UploadResultDTO> upload(UploadFileDTO uploadFileDTO) {
log.info(uploadFileDTO);
if(uploadFileDTO.getFiles() != null) {
final List<UploadResultDTO> list = new ArrayList<>();
uploadFileDTO.getFiles().forEach(multipartFile -> {
String originalName = multipartFile.getOriginalFilename();
log.info(originalName);
String uuid = UUID.randomUUID().toString();
Path savePath = Paths.get(uploadPath, uuid+"_"+originalName);
boolean image = false;
try {
multipartFile.transferTo(savePath);
//썸네일
if(Files.probeContentType(savePath).startsWith("image")) {
image = true;
File thumbFile = new File(uploadPath, "s_"+uuid+"_"+originalName);
Thumbnailator.createThumbnail(savePath.toFile(), thumbFile, 100,100);
}
} catch (Exception e) {
e.printStackTrace();
}
//반환
list.add(UploadResultDTO.builder()
.uuid(uuid)
.fileName(originalName)
.img(image)
.build()
);
});
return list;
}
return null;
}
}
댓글목록 0
등록된 댓글이 없습니다.