UpDownController
페이지 정보
작성자 관리자 작성일 24-06-26 15:52 조회 114 댓글 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.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.Parameter;
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;
}
//첨부파일 조회
@Operation(summary = "view 파일", description = "GET 방식으로 첨부파일 조회")
@GetMapping("/view/{fileName}")
public ResponseEntity<Resource> viewFileGet(@PathVariable("fileName") String fileName){
Resource resource = new FileSystemResource(uploadPath + File.separator + fileName);
String resourceName = resource.getFilename();
HttpHeaders headers = new HttpHeaders();
try {
headers.add("Content-Type", Files.probeContentType(resource.getFile().toPath()));
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
return ResponseEntity.ok().headers(headers).body(resource);
}
}
댓글목록 0
등록된 댓글이 없습니다.