res.setImg(preview(res.getImg()));
public String preview(String name) {
try {
String imagePath = "D:\\java-upload-Pic\\" + name;
File imageFile = new File(imagePath);
if (imageFile.exists()) {
FileInputStream fileInputStream = new FileInputStream(imageFile);
byte[] imageData = new byte[(int) imageFile.length()];
fileInputStream.read(imageData);
fileInputStream.close();
byte[] base64ImageData = Base64.getEncoder().encode(imageData);
String base64ImageString = new String(base64ImageData, StandardCharsets.UTF_8);
return "data:image/jpeg;base64," + base64ImageString;
} else {
return null;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}