base64 to image in gwt client

2,259 views
Skip to first unread message

Amruta Deshpande

unread,
Mar 16, 2012, 5:41:28 AM3/16/12
to Google Web Toolkit
I have a image in form of byte array on my server. I m encoding it
with base64 as follows

byte[] bytes = bos.toByteArray();
String base64 =
com.google.gwt.user.server.Base64Utils.toBase64(bytes);
base64 = "data:image/jpg;base64,"+base64;

I am using GWT on client side. I want to display this image and trying
to build image object follws:

public void setInput(DataSource input) {
Image image = new Image(myUser.getImg());
this.setWidget(i, 4, image);
}

but this is not working... can somebody help me?

Jens

unread,
Mar 16, 2012, 8:55:45 AM3/16/12
to google-we...@googlegroups.com
GWT's Base64Utils does not use the standard MIME-Base64 characters (see Base64Utils.base64Chars Javadoc) so browsers do not understand your data URI. You have to choose a standard conform MIME-Base64 encoding. 
Also keep in mind that IE6/7 do not support data URIs and IE8 has a 32kb limit.

-- J.

Derek

unread,
Mar 16, 2012, 9:29:52 AM3/16/12
to Google Web Toolkit
When I needed to do that, I just copied and pasted the code out of
GWT's ClientBundle image stuff:

private String toBase64(byte[] data) {
// This is bad, but I am lazy and don't want to write _another_
encoder
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
String base64Contents = enc.encode(data).replaceAll("\\s+", "");
return "data:image/jpeg;base64," + base64Contents;
}

(And I don't have to worry about IE since we were able to convince
management that IE users could install ChromeFrame!)

Thad

unread,
Mar 17, 2012, 3:23:46 PM3/17/12
to google-we...@googlegroups.com
I've used this Base64 class for both images and URL-safe encoding:  http://iharder.net/base64

It is in the public domain.

Ramanan Nathamuni

unread,
Jul 14, 2016, 5:38:23 AM7/14/16
to GWT Users
Hello Thad Humphries,

How to use this folder please let me know the steps to use this folder in GWT
Reply all
Reply to author
Forward
0 new messages