From d6c8fa19ca5244f7c8ac72107dc1f733e1afef4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dhji=28=EC=A7=80=EB=8C=80=ED=95=9C=29?= Date: Thu, 5 Oct 2023 17:33:09 +0900 Subject: [PATCH] =?UTF-8?q?image=20=EB=AA=BB=EC=9D=BD=EC=9D=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comn/file/service/ComnFileService.java | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/pav-server/src/main/java/com/palnet/biz/api/comn/file/service/ComnFileService.java b/pav-server/src/main/java/com/palnet/biz/api/comn/file/service/ComnFileService.java index fc09b581..bea0c7da 100644 --- a/pav-server/src/main/java/com/palnet/biz/api/comn/file/service/ComnFileService.java +++ b/pav-server/src/main/java/com/palnet/biz/api/comn/file/service/ComnFileService.java @@ -12,12 +12,14 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.IOUtils; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.time.Instant; import java.util.Base64; @@ -125,22 +127,43 @@ public class ComnFileService { public String imagesToBase64ForSrc(String path) { StringBuilder str = new StringBuilder(); ClassPathResource resource = new ClassPathResource(path); - File file = null; - try { - file = resource.getFile(); - System.out.println(file.getName()); - String ext = FilenameUtils.getExtension(file.getName()); - byte[] fileContent = FileUtils.readFileToByteArray(file); - String encodedString = Base64.getEncoder().encodeToString(fileContent); + + String ext = ""; + int lastIndex = path.lastIndexOf("."); + if (lastIndex >= 0) { + ext = path.substring(lastIndex + 1); + } + + try (InputStream is = resource.getInputStream();){ + byte[] imageBytes = IOUtils.toByteArray(is); + String encodedString = java.util.Base64.getEncoder().encodeToString(imageBytes); str .append("data:image/") .append(ext) .append(";base64,") .append(encodedString); - } catch (IOException e) { throw new RuntimeException(e); } + + // 이미지를 Base64로 변환 + +// File file = null; +// try { +// file = resource.getFile(); +// System.out.println(file.getName()); +// String ext = FilenameUtils.getExtension(file.getName()); +// byte[] fileContent = FileUtils.readFileToByteArray(file); +// String encodedString = Base64.getEncoder().encodeToString(fileContent); +// str +// .append("data:image/") +// .append(ext) +// .append(";base64,") +// .append(encodedString); +// +// } catch (IOException e) { +// throw new RuntimeException(e); +// } return str.toString(); }