How to Display Image in Jsp Using Spring | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

Method 01 : Using Controller Method

Jsp

<img src="/admin/getImage/${userPhoto}" width="150" height="150" title="${user.firstName} ${user.lastName}">

Controller

@RequestMapping(value = "/getImage/{imageId}")
@ResponseBody
public byte[] getImage(@PathVariable String imageId, HttpServletRequest request) {
	try {
		return attachmentUtil.getImageBytes(request.getSession().getServletContext().getRealPath("/"), "profile_pic", imageId, request);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return new byte[0];
}

Util

public byte[] getImageBytes(String path, String directoryName, String fileName, HttpServletRequest request) {
	ByteArrayOutputStream byteArrayOutputStream;
	BufferedImage bufferedImage;
	byte[] imageInByte;
	try {
		byteArrayOutputStream = new ByteArrayOutputStream();
		bufferedImage = ImageIO.read(new File(path + directoryName + fileName));
		ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);
		byteArrayOutputStream.flush();
		imageInByte = byteArrayOutputStream.toByteArray();
		byteArrayOutputStream.close();
		return imageInByte;
	} catch (FileNotFoundException fnfe) {
		fnfe.printStackTrace();
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}

Method 02 : Using base64

Jsp

<img src="data:image/jpeg;base64,${userPhoto}" width="150" height="150" title="Testing">

Controller

@GetMapping(value = "/editUser/{id}")
public String editUser(@PathVariable Long id, Model model, HttpServletRequest request) {
	try {
		User user = userRepository.getOne(id);
		model = userMasterCombo(model);
		model.addAttribute("user", user);
		model.addAttribute("isEdit", true);
		model.addAttribute("userPhoto", attachmentUtil.convertFileIntoBase64String(request.getSession().getServletContext().getRealPath("/"), 
				"profile_pic", user.getUserPhoto(), request));
	} catch (Exception e) {
		e.printStackTrace();
	}
	return "admin/addUser";
}

Util

public String convertFileIntoBase64String(String path, String directoryName, String fileName, HttpServletRequest request) throws IOException {
	File file;
	FileInputStream fileInputStream = null;
	ByteArrayOutputStream byteArrayOutputStream = null;
	try {
		file = new File(path + directoryName + fileName);
		fileInputStream = new FileInputStream(file);
		byteArrayOutputStream = new ByteArrayOutputStream();
		int b;
		byte[] buffer = new byte[1024];
		while ((b = fileInputStream.read(buffer)) != -1) {
			byteArrayOutputStream.write(buffer, 0, b);
		}
			
		byte[] fileBytes = byteArrayOutputStream.toByteArray();
		fileInputStream.close();
		byteArrayOutputStream.close();

		byte[] encoded = Base64.encodeBase64(fileBytes);
		String encodedString = new String(encoded);
		return encodedString;
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if(byteArrayOutputStream != null) {
			byteArrayOutputStream.close();
		}
		if(fileInputStream != null) {
			fileInputStream.close();
		}
	}
	return new String();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s