UpDownController
페이지 정보
본문
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.HashMap;
import java.util.List;
import java.util.Map;
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.DeleteMapping;
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.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);
}
//첨부파일 삭제
@Operation(summary = "remove 파일", description = "DELETE 방식으로 첨부파일 삭제")
@DeleteMapping("/remove/{fileName}")
public Map<String,Boolean> removeFile(@PathVariable("fileName") String fileName){
Resource resource = new FileSystemResource(uploadPath + File.separator + fileName);
String resourceName = resource.getFilename();
Map<String, Boolean> resultMap = new HashMap<>();
boolean removed = false;
try {
String contentType = Files.probeContentType(resource.getFile().toPath());
removed = resource.getFile().delete();
//썸네일 존재 시
if(contentType.startsWith("image")) {
File thumbFile = new File(uploadPath+File.separator, "s_"+fileName);
thumbFile.delete();
}
} catch (Exception e) {
log.error(e.getMessage());
}
resultMap.put("result", removed);
return resultMap;
}
}
- 이전글● 첨부파일 삭제 24.06.26
- 다음글▲ localhost:8080/swagger-ui/index.html ( 업로드 파일 삭제 ) 24.06.26
댓글목록
등록된 댓글이 없습니다.