As a memo, I often forget how to display an image on the screen (html) from the image information saved in the DB with java.
File name: Sample image File path: The path where the image is stored Configuration like the table above
public imageDto getImage(String id) throws Exception {
//Get information from DB
List<TProductImg> imgList = imgRepository.findByid(Long.parseLong(id));
if (!ObjectUtils.isEmpty(imgList)) {
List<String> dispImgPath = new ArrayList<>();
for (TProductImg e : imgList) {
StringBuffer data = new StringBuffer();
data.append("data:image/jpeg;base64,");
productDto.setFilename(e.getFilename());
//Encode the acquired image information
String base64 = new String(Base64.encodeBase64(Files.readAllBytes(Paths.get(e.getFilepath()))), "ASCII");
data.append(base64);
dispImgPath.add(data.toString());
}
imageDto.setDispImgPath(dispImgPath);
}
}
Finally, display the encoded dispImgPath on the screen (html).
Recommended Posts