Browse Source

image 못읽음

pull/10/head
지대한 12 months ago
parent
commit
d6c8fa19ca
  1. 39
      pav-server/src/main/java/com/palnet/biz/api/comn/file/service/ComnFileService.java

39
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();
}

Loading…
Cancel
Save