From 71273e1542deffa46b5ab2e8944ead224ee82dc2 Mon Sep 17 00:00:00 2001 From: minyeong <106754293+miinyeong@users.noreply.github.com> Date: Sat, 18 Nov 2023 19:43:24 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=97=85?= =?UTF-8?q?=EB=A1=9C=EB=93=9C=20=ED=8C=8C=EC=9D=BC=EB=AA=85=20=EB=9E=9C?= =?UTF-8?q?=EB=8D=A4=20=EC=83=9D=EC=84=B1=ED=9B=84=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../onestep/server/service/image/S3Uploader.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/onestep/server/service/image/S3Uploader.java b/src/main/java/com/onestep/server/service/image/S3Uploader.java index f146093..c76a383 100644 --- a/src/main/java/com/onestep/server/service/image/S3Uploader.java +++ b/src/main/java/com/onestep/server/service/image/S3Uploader.java @@ -20,6 +20,8 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.UUID; @Slf4j @@ -35,6 +37,7 @@ public class S3Uploader { // MultipartFile을 전달받아 File로 전환한 후 S3에 업로드 public String upload(MultipartFile multipartFile, String dirName) throws IOException { + log.info("multipartFile={}",multipartFile); File uploadFile = convert(multipartFile); // 파일 변환할 수 없으면 에러 return upload(uploadFile, dirName); @@ -42,6 +45,7 @@ public String upload(MultipartFile multipartFile, String dirName) throws IOExcep // s3로 업로드 private String upload(File uploadFile, String dirName) { + String fileName = dirName + "/" + uploadFile.getName(); String uploadImageUrl = putS3(uploadFile, fileName); @@ -69,8 +73,9 @@ private void removeNewFile(File targetFile) { } // 로컬에 파일 업로드 하기 - private File convert(MultipartFile file) throws IOException { - File convertFile = new File(file.getOriginalFilename()); + private File convert(MultipartFile file) throws IOException { + String storeFilename = UUID.randomUUID() + "." + extractExt(file.getOriginalFilename()); + File convertFile = new File(storeFilename); convertFile.createNewFile(); FileOutputStream fos = new FileOutputStream(convertFile); fos.write(file.getBytes()); @@ -84,4 +89,9 @@ public void delete(String fileKey) { amazonS3Client.deleteObject(bucket, fileKey); } + // 파일 확장자 추출 + private String extractExt(String originalFilename) { + int pos = originalFilename.lastIndexOf("."); + return originalFilename.substring(pos + 1); + } } \ No newline at end of file