DoubleMalt's answer (accepted at the time of writing) is unsuccessful because it uses two violations to do the right thing. This does not help the Apache Commons Codec make it so easy to do the wrong thing :(
Base64 is basically an encoding from binary data to text - as such, it should almost always be used to convert byte[] to String . Your problem is that you convert byte[] to another byte[] , but later you want to use this data as a string. It would be better to convert once, in the right direction.
Now you can choose exactly when you convert to base64 (and string). You can do this earlier in your Java code, in which case I would use:
<img src="data:image/jpg;base64,<c:out value='${bean.imageBase64}'/>" />
Alternatively, you can only store binary data in a bean and encode in JSP. I haven't written JSP for a long time, so I'm not going to write code for this here.
But basically, you need to decide whether your bean should store the original binary data as byte[] , or the data with the underlying 64 code as String . Everything else is misleading, IMO.
Jon skeet
source share