Need to convert images (stored as an attachments on Lotus Notes documents) into text to be serializable via JSON ?

David Marko  25 February 2013 07:52:49
We have had recently a special requirement ... we needed to serve JSON product  data (from Lotus Notes database product catalogue) with images embedded inside. JSON is generally textual format so we have to find out the way how to embed the binary image there. Fortunately we are using xPages here so we can use JAVA libraries that are plenty available for many different use cases ...

For a few year we are successfully using JODD Library as a JAVA coders helper tool for our xPages applications. Before JODD library being started used in our company we were using Apache Commons libraries but we have had many problems with JAVA permissions issues in Domino in many situations. To solve this we tried to use JODD library and never turned back ... Its amazing project that can help xPages developer in many areas. JODD contains well balanced helper objects covering many daily use cases in web development. What's more JODD comes with very nice managed documentation and examples that are really necessary for adopting the library. (see yourself here http://jodd.org )

Our complete commented code covering our requirement is below ....

Image:Need to convert images (stored as an attachments on Lotus Notes documents) into text to be serializable via JSON ?

... then to view such encoded image in HTML you can simply use the following HTML image tag ...

< img src="data:image/jpeg;base64,encoded image as text goes here"  />

Comments

1Karsten Lehmann  25.02.2013 14:56:24  Need to convert images (stored as an attachments on Lotus Notes documents) into text to be serializable via JSON ?

Don't forget to recycle the embedded object "file" after using it. Otherwise, your server's temp directory gets filled with temporary files. The reason is that "EmbeddedObject.getInputStream()" just extracts the attachment temporarily and returns a stream to that temporary file. Unfortunately, it does not provide direct streaming of data.

TOPlist