์ด๋ฒ ์ฃผ์ Sonarqube๋ฅผ ๊ณต๋ถํ๊ณ ์ ์ฉํ๊ธฐ ์ ์ ์ฒด์ ์ธ ์ฝ๋๋ฅผ ๋ค์ ๊ฒํ ์ค์ด์๋ค.
๋นผ๋์ ํ์์ ์ธ ๊ธฐ๋ฅ์ด ์๋ค๋ ๊ฒ์ ์๊ฒ ๋์๋ค. ์ํ ํ ์ด๋ธ์๋ ์ด๋ฏธ์ง ํ์ผ ์ด๋ฆ ์นผ๋ผ์ด ์กด์ฌํ๋๋ฐ ์ด๋ฅผ ๊น๋ฐํ๋ ๊ฒ์ด๋ค.
ํ์ผ ์ ๋ก๋, ๋ค์ด๋ก๋ ๊ธฐ๋ฅ์ ๊ธํ๊ฒ ์ ์ฉํ๊ฒ ๋์๊ณ , ์ด ๊ณผ์ ์ ๊ธฐ๋ก์ผ๋ก ์์ฑํ๊ฒ ๋ค.
ํ์ผ ์ ๋ก๋
ํ์ผ์ ๋ฑ๋กํ๋๋ฐ ํ์ํ ํ์ ์ผ๋ก ์คํ๋ง์์๋ MultipartFile ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๋ค.
์ด๋ฅผ ํตํด ํ์ผ ์ ๋ก๋ ๋ฐ ๋ค์ด๋ก๋๋ฅผ ์ฝ๊ฒ ๊ตฌํํ ์ ์๋ค.
ํ์ผ ์ ๋ก๋๋ฅผ ์ด์ฉํ๋ ๋ด API๋ ์ํ ์ถ๊ฐ์ ์์ ์ด๋ค.
๋จผ์ ํ์ผ ์ ๋ก๋ํ ์์น๋ฅผ ๋ด๋ถ ๊ฒฝ๋ก์ ์ธ๋ถ ๊ฒฝ๋ก์ค ๊ณ ๋ฏผ์ ํ๋ค. ์ ๋ฒ ํ๋ก์ ํธ ๋๋ ํ๋ก์ ํธ ๋ด๋ถ๋ก ์ง์ ํ์ง๋ง, ์ด๋ ๊ฒ ํ๊ฒ ๋๋ฉด ๋ฐฐํฌ ์ ์ด๋ฏธ์ง ๊ฒฝ๋ก๋ฅผ ๋ชป ์ฐพ์ ์ ์๋ค๊ณ ํ๋ค.
๊ทธ๋์ ์ด๋ฒ์๋ ํ๋ก์ ํธ ์ธ๋ถ ๊ฒฝ๋ก๋ก ์ง์ ํ๊ธฐ๋ก ๊ฒฐ์ ํ๋ค.
์ฐ์ ,
ํ์ผ ์ ๋ก๋๋ฅผ ํ๊ธฐ ์ํด @RequestParam ์ ๋ ธํ ์ด์ ์ผ๋ก Multipartfile ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ์์จ๋ค.
public ResponseEntity<?> addAction(@ModelAttribute ProductDTO productDTO,
@RequestParam("image") MultipartFile file) throws IOException {
ํ์ผ ์ ๋ก๋๋ ๋น์ฆ๋์ค ๋ก์ง์ด๊ธฐ ๋๋ฌธ์ Service ํด๋์ค๋ฅผ ๋ฐ๋ก ์ ์ธํ์ฌ ๊ณ์ธต์ ๋ถ๋ฆฌํ๋ค.
public String uploadFile(MultipartFile productImageFile) throws IOException {
String originalFileName = productImageFile.getOriginalFilename();
UUID uuid = UUID.randomUUID();
String uploadFileName = uuid + "_" + originalFileName;
File file = new File(uploadDirectory, uploadFileName);
productImageFile.transferTo(file);
return uploadFileName;
}
=> ์ค๋ณต ํ์ผ๋ช ์ ๋ฐฉ์งํ๊ธฐ ์ํด UUID ํด๋์ค๋ฅผ ์ด์ฉํด ์ ๋ก๋ ํ ํ์ผ๋ช ์ ์ ์ํด ์ ์ฅํ๋ค.
ํ์ผ ์ญ์ ์ ๊ฒฝ์ฐ๋ ๊ฐ๋จํ ๊ตฌํ ๊ฐ๋ฅํ๋ค.
public void deleteFile(String productImageName) {
File existingFile = new File(uploadDirectory, productImageName);
if (existingFile.exists()) {
existingFile.delete();
}
}
ํ์ผ ๋ค์ด๋ก๋
ํ์ผ ์ ๋ก๋๋ฅผ ํตํด ์๋ฒ์ ํ์ผ์ ์ ์ฅ์์ผฐ๋ค.
API๋ฅผ ํตํด ์ด๋ฏธ์ง๋ฅผ ์ป์ผ๋ ค๋ฉด ์ ์ฒด ๋ฐ์ดํฐ์ ํจ๊ป ํ์ผ์ ์๋ตํ๋ ๊ฒ์ ์๋นํ ๋นํจ์จ์ ์ด๋ผ ํ๋จ์ ํ๋ค.
๊ทธ๋์ ๊ธฐ๋ณธ์ ์ธ ์ํ ๋ฐ์ดํฐ๋ฅผ ์ป๊ธฐ ์ํด Get ์์ฒญ ์์๋ ๋ค์ด๋ก๋ URI๋ฅผ ๋๊ฒจ์ฃผ๊ณ ,
์ด URI๋ฅผ ์ด์ฉํด ์ด๋ฏธ์ง๋ฅผ ๋ค์ด๋ก๋ ํ ์ ์๊ฒ ์ค๊ณํ๋ค.
=> ์ํ ์กฐํ๋ฅผ ํ๋ฉด ๋ค์ด๋ก๋ URI๋ฅผ ๋๊ธด๋ค.
์ด๋ฅผ ํตํด Get ์์ฒญ์ ๋ณด๋ด๋ณด์
=> ๋นจ๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ์ ๋ก๋ํ์ผ๋ฏ๋ก ์ด ๊ทธ๋ฆผ์ ์๋ต๋ฐ์ ์ ์๋ค.
public Resource loadFile(String fileName) throws FileNotFoundException {
try {
Path directoryLocation = Paths.get(uploadDirectory)
.toAbsolutePath().normalize();
Path file = directoryLocation.resolve(fileName).normalize();
Resource resource = new UrlResource(file.toUri());
if (resource.exists() || resource.isReadable()) {
return resource;
} else {
throw new FileNotFoundException(FILE_NOT_FOUNT);
}
} catch (MalformedURLException e) {
throw new FileNotFoundException(FAILED_DOWNLOAD);
}
}
=> ํ์ผ ๋ค์ด๋ก๋๋ฅผ ์ํด ๋ฆฌ์์ค๋ฅผ ๋ฐํํ๋ ๋ฉ์๋
@GetMapping("/download/{fileName}")
public ResponseEntity<Resource> getDownloadFile(@PathVariable String fileName, HttpServletRequest request) throws IOException {
Resource resource = fileService.loadFile(fileName);
String contentType;
try {
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
} catch (IOException e) {
log.error(FAILED_FILE_CONTENT);
throw new IOException(FAILED_FILE_CONTENT);
}
if (contentType == null) {
contentType = DEFAULT_CONTENT_TYPE;
}
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename())
.body(resource);
}
=> ํธ๋ค๋ฌ ๋ฉ์๋
ํ์ผ ์ ๋ก๋์ ๋ค์ด๋ก๋๋ฅผ ํตํด ํ ์คํธ ์ฝ๋๋ฅผ ์์ ํ๋ค.
๋จ์ผ ์ํ ์กฐํ
์ํ ๋ชฉ๋ก ์กฐํ
'๐ Spring Framework > Spring Project' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฆฌํฉํฐ๋ง ใAuthentication(์ธ์ฆ)ใ (0) | 2022.07.11 |
---|---|
์คํ๋ง ๋ถํธ ์๋ํ๋ธ(SonarQube) ์ ์ฉ + PostgreSql (0) | 2022.07.05 |
ใ์ปจํธ๋กค๋ฌ ๋จ์ ํ ์คํธใ (0) | 2022.06.23 |
ใํ ์คํธ ์ฝ๋ & Spring REST Docsใ (0) | 2022.06.20 |
OutBox Pattern & Saga Pattern & Transaction (0) | 2022.06.13 |