[JAVA] Display an image on the base64 screen

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.

Database information

スクリーンショット 2020-12-27 203555.png

File name: Sample image File path: The path where the image is stored Configuration like the table above

logic

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

Display an image on the base64 screen
Display text on top of the image
Display the list in setDetails on the screen with spring-security
[Rails] How to display an image in the view
Display an error screen during the download process to response
How to display products by category on the same list screen
The image has been uploaded, but it doesn't appear on the screen! ??
How the website is displayed on the screen
Pre-processing to display on the browser (compiler)
Display the background image in the production environment
[Swing] How to display an arbitrary name on the menu bar on Mac
[Android] Get the tapped position (coordinates) on the screen
Display the System Monitor applet on the Ubuntu 20.04 taskbar
[Android] List all setting items on the setting screen
Display Mat type image on GUI with Swing
JavaFX8 image display
[Ubuntu 20.04] Display the day of the week on the date / clock
[No.003] Create an order list screen for the orderer
Import the instance and use it on another screen
How to move to the details screen by clicking the image
[Android] Display images and characters on the ViewPager tab
Mandels to create an image of the Mandelbrot set
[Introduction] Display Android Studio Hello World on the emulator
Switch the display screen when hovering the tab with jQuery
Screen recording on Ubuntu 20.04
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
[Java] Throw a request and display the screen ② (GET / POST)
Solution when the image data becomes nil via the confirmation screen
I want to use screen sharing on the login screen on Ubuntu 18
[Ruby on Rails] Quickly display the page title in the browser
[Java] Throw a request and display the screen (GET / POST)
(Ruby on Rails6) Display of the database that got the id of the database
[Android] Try to display the effect on the touched part of the screen, which is common in games.
The story of setting up an Oracle V $ -like thing from 0 on PostgreSQL: 12 of the official Docker image