Creating an embedded image using java gwt

I am trying to write an embedded image using Java GWT. However, the image is not displayed:

<img src="data:unknown;base64,Pz9QAQSkZJRgABAQAAAQABAA9Q9AAUDBAcJCggFBQkGCAYGBggFBQUFCAgGBQYGBwUHBwUGBgUHChALBwgaCQUFDBUMDhERHxMfBxciGBYSGBAeEx4BBQUFCAcIDwgJCBIIDA4SEhISEhISEh4SEhIeEhIeHhISEh4eEhIeHh4SHh4eEhISEhISHh4eEhIeEh4eEhISHfz9ABEIAGAAYAMBIgACEQEDEQD9QAcAAACAQUBAAAAAAAAAAAAAAAAAQcCAwQGCAT9QAvEAACAQIEBAMHBQAAAAAAAAAAAQIDEQQFEiEGBzFBE1FyIjJhYnD9RRSPz9P0AGwEBAAIDAQEAAAAAAAAAAAAAAAECBAUHBgL9QAgEQEAAgEFAAMBAAAAAAAAAAAAAQIDBAUREiETMmExP0ADAMBAAIRAxEAPw9M0zKv1jbfz9bP0SUlJdkZtD0RV4JeRluEP0jP0tP0URVGNiorZSf9R9MP0dT0aVH9RznWgcfz9M379RET9Pz9GXo/EzNuW1FYUv9SXN1Wfz9Dj8/UpcHP05RjsQVM2S9XPz9Rf9Vb9Pz9SxxVL9JpDcWoNRzxuXYQXAYmFwAZV79WNiOfz9JX4vZEY8XoyPz9EnR6Lvz9X8PPz9My9vVv9P00c0lyRl8dOz9Ogr9P1GP0RBcG4YS79P07MF9dv09P1fRxwWk6R0hpLPz9XP1/Ux80cv1bf0CPz9OxH9UFwAP1qR3H9TP0CJg/Ffz9Pz9TlYVSkpxS39WL9S0T9a04cm8cZX4NSU39P0XEv0+1r9TPz9Pz9P1TTw9aEpWTv06Nfz9VP07NFn9cM3fF1eP1GAzRzP0hL0wSX9X04Jg5aZSL9P1rWr9Yv1fEi5ZOv1zMf0ND2UjJfz9Pz9HnZ9VoqFPymIPz9af1yD2QOIWJGP1ZQf1NXdmQBxXP0SUfz9P11aP0tMjv9SUpP1/Rf1BP1NTD9Yz9Pz9TT9X9HvX9XSwiT1sQT9NTT9W0zbv0mXv06P0ZP1RSfz9Rb9f0ADExgVQYAABcBDQ9cTH9Fgb9Sy79YzsPS0l8GE8gQAEAYCAYAAAAAAAAAPz9"> 

It seems to me that the reason for this is that I am encoding it incorrectly with base64 or that the encoding is incorrect.

If, when returning the following pic code that I found on the Internet, everything works (so it isn’t there, that my browser cannot display embedded images):

 <img src="data:unknown;base64,R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=="> 

This is how I create my pic code. I open the url and try to base64 encode the result:

 try { IoProvider.get().makeRequestAsText(url, new ResponseReceivedHandler<String>() { public void onResponseReceived(ResponseReceivedEvent<String> event) { final Response<String> response = event.getResponse(); if (response.getStatusCode() == HTTP_OK){ callback.onSuccess("data:unknown;base64," + Base64.encode(response.getText())); } } }, options); } catch ... 

This is a base64 encoder that I'm using: http://snipt.net/tweakt/gwt-base64/

Any ideas why the generated image is invalid?

EDIT:

I rewrote my code a bit based on your answers. Here's what it looks like now:

 IoProvider.get().makeRequest(url, new ResponseReceivedHandler<Object>() { public void onResponseReceived(ResponseReceivedEvent<Object> event) { final Response<Object> response = event.getResponse(); if (response.getStatusCode() == HTTP_OK) { // not working callback.onSuccess("data:image/jpeg;base64," + Base64Utils.toBase64(response.getText().getBytes())); // working image // callback.onSuccess("data:unknown;base64,R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=="); } } }, options); 

The edited code produces the following code:

 <img src="data:image/jpeg;base64,77+977+977+977+9ABBKRklGAAEBAAABAAEAAO+/ve+/vQDvv70ABQMEBwkKCAUFCQYIBgYGCAUFBQUICAYFBgYHBQcHBQYGBQcKEAsHCBoJBQUMFQwOEREfEx8HFyIYFhIYEB4THgEFBQUIBwgPCAkIEggMDhISEhISEhISHhISEh4SEh4eEhISHh4SEh4eHhIeHh4SEhISEhIeHh4SEh4SHh4SEhIe77+977+9ABEIAGAAYAMBIgACEQEDEQHvv73vv70AHAAAAgEFAQAAAAAAAAAAAAAAAAEHAgMEBggF77+977+9AC8QAAIBAgQEAwcFAAAAAAAAAAABAgMRBAUSIQYHMUETUXIiMmFice+/ve+/vRRS77+977+977+977+977+9ABsBAQACAwEBAAAAAAAAAAAAAAABAgQFBwYD77+977+9ACARAQACAQUAAwEAAAAAAAAAAAABAgMEBRESIRMyYTHvv73vv70ADAMBAAIRAxEAPwDvv70w77+9Myvvv71jbu+/ve+/vcWt77+9Eu+/vUlJdkZtDO+/vRHvv71eyIl5GW4R77+9I++/ve+/vS3vv73vv70U77+9FUY2KitlKO+/ve+/vRDvv70x77+9HUzvv70a77+9Uu+/vUTvv73Hp++/vWgc4a+m77+977+9M3/vv71ERe+/ve+/ve+/ve+/vRl6PxMzbu+/vW1FYUzvv71Jc3Va77+977+9Dj8/77+9Slwd77+9OUY7EO+/vVM277+9L1fvv73vv73vv73vv70Y77+977+9V++/ve+/ve+/ve+/ve+/vSxx77+9U++/ve+/vdKaQ3FqDUc8bu+/vXYQXAYmFwAZ77+9X++/ve+/vWNiOu+/ve+/vSV+L2RGPO+/vXoy77+977+977+9EnR6L++/ve+/ve+/vX8P77+977+977+9My9v77+9XO+/ve+/ve+/vTRzSXJGXx0477+977+9Ogvvv73vv73vv71GP0RB1bBuGO+/vS/vv73vv73vv707MFDvv71377+9Pe+/ve+/vV/vv70ccO+/vWnnrLpHSGkt77+977+9Xe+/vX9THzRz77+9W86y77+9Au+/ve+/ve+/vTsS77+977+9QXAB77+9akdy77+9Te+/vQLvv73GmD8W77+977+977+977+977+9TlYVSkpx77+9Lu+/ve+/vWPvv71LRe+/vWjvv704cm8cZX4NSU7vv73vv73vv70XE++/vT7Oq1vvv71N77+977+977+977+977+977+977+9U++/vTw9aEpWT++/vTo277+977+9Ve+/vTs0Wu+/vXHPjtufFO+/vV7vv73vv71GAzRz77+977+9ISzvv70w77+9Ju+/vVzvv704Jg5aZSPvv73vv73vv71ry6Fr77+9Y++/vV8SLlk777+9czLvv70ND2UjJu+/ve+/ve+/ve+/ve+/vR52fe+/vVoqFe+/vcKmIe+/ve+/vWrvv71yD2QOIWJG77+977+9WULvv71N77+9d2ZAHFfvv73vv70SUu+/ve+/ve+/ve+/vXVp77+9LTI877+977+9JSnvv73vv71/Ru+/vUHvv73vv71N77+9Me+/vWDvv73vv73vv73vv73vv73vv70177+977+9cO+/vR/Dtu+/vV0sIu+/vT1s77+9Be+/vTU177+9WO+/vTNv77+9Jl/vv70677+977+9Ge+/ve+/vVFK77+977+977+9F++/vXzvv70ADExgVQYAABcBDQDvv71xMu+/vRYH77+9Sy/vv71jOw9LSXwYTyBAAQBgIBgAAAAAAAAB77+977+9"> 

I also tried converting the encoding:

 try { callback.onSuccess("data:image/jpeg;base64," + Base64Utils.toBase64(response.getText().getBytes("ISO-8859-1"))); } catch (UnsupportedEncodingException e) { } 

What produces this code:

 <img src="data:image/jpeg;base64,/f39/QAQSkZJRgABAQAAAQABAAD9/QD9AAUDBAcJCggFBQkGCAYGBggFBQUFCAgGBQYGBwUHBwUGBgUHChALBwgaCQUFDBUMDhERHxMfBxciGBYSGBAeEx4BBQUFCAcIDwgJCBIIDA4SEhISEhISEh4SEhIeEhIeHhISEh4eEhIeHh4SHh4eEhISEhISHh4eEhIeEh4eEhISHv39ABEIAGAAYAMBIgACEQEDEQH9/QAcAAACAQUBAAAAAAAAAAAAAAAAAQcCAwQGCAX9/QAvEAACAQIEBAMHBQAAAAAAAAAAAQIDEQQFEiEGBzFBE1FyIjJhYnH9/RRS/f39/f0AGwEBAAIDAQEAAAAAAAAAAAAAAAECBAUHBgP9/QAgEQEAAgEFAAMBAAAAAAAAAAAAAQIDBAUREiETMmEx/f0ADAMBAAIRAxEAPwD9MP0zK/1jbv39bf0S/UlJdkZtDP0R/V4JeRluEf0j/f0t/f0U/RVGNiorZSj9/RD9Mf0dTP0a/VL9RP3n/Wgc5v39M3/9REX9/f39GXo/EzNu/W1FYUz9SXN1Wv39Dj8//UpcHf05RjsQ/VM2/S9X/f39/Rj9/Vf9/f39/Sxx/VP9/ZpDcWoNRzxu/XYQXAYmFwAZ/V/9/WNiOv39JX4vZEY8/Xoy/f39EnR6L/39/X8P/f39My9v/Vz9/f00c0lyRl8dOP39Ogv9/f1GP0RBcG4Y/S/9/f07MFD9d/09/f1f/Rxw/Wk6R0hpLf39Xf1/Ux80c/1bsv0C/f39OxL9/UFwAf1qR3L9Tf0C/Zg/Fv39/f39TlYVSkpx/S79/WP9S0X9aP04cm8cZX4NSU79/f0XE/0+q1v9Tf39/f39/f1T/Tw9aEpWT/06Nv39Vf07NFr9cc7fFP1e/f1GAzRz/f0hLP0w/Sb9XP04Jg5aZSP9/f1r4Wv9Y/1fEi5ZO/1zMv0ND2UjJv39/f39HnZ9/VoqFf2mIf39av1yD2QOIWJG/f1ZQv1N/XdmQBxX/f0SUv39/f11af0tMjz9/SUp/f1/Rv1B/f1N/TH9YP39/f39/TX9/XD9H/b9XSwi/T1s/QX9NTX9WP0zb/0mX/06/f0Z/f1RSv39/Rf9fP0ADExgVQYAABcBDQD9cTL9Fgf9Sy/9YzsPS0l8GE8gQAEAYCAYAAAAAAAAAf39"> 

How to pass response to Base64 encoder without converting it to String?

response.getData () is an object, and I like to have it as byte [].

+6
source share
7 answers

Jochen

I conducted extensive tests with various Base64 encoding libraries:

Test code:

 public static void main(String [] args) throws IOException { File file = new File("./resources/so.png"); BufferedInputStream bufRead = new BufferedInputStream(new FileInputStream(file)); ByteBuffer buffer = ByteBuffer.allocate(30*1024) ; // x kb byte[] c = new byte[1]; while ((bufRead.read(c))>0) { //1 byte/time to avoid buffer arithmetics buffer.put(c); } byte[] data = new byte[buffer.position()]; buffer.flip(); buffer.get(data); String dataAsSt = new String(data); // transform the data to a string -- encoding error-prone //gwt-base64 //String gwtBase64 = GwtBase64.encode(dataAsSt); //doesn't work //google base64 impl String googleBase64 = Base64Utils.toBase64(data); //apache base64 codec Base64 base64codec = new Base64(-1); String apacheBase64 = base64codec.encodeToString(data); System.out.println("Google:"+googleBase64); System.out.println("Apache:"+apacheBase64); //System.out.println("GWTb64:"+gwtBase64); } 

Conclusions:

  • (gwt-base64) Doesn't work at all. This failed: java.lang.StringIndexOutOfBoundsException: String index out of range on every image I tried. Please note that I commented on the code.

  • (google) The Base64 encoding that it creates cannot be understood by the browser.

  • (apache) . Works with this constructor: new Base64 (-1) = no output lines, no URLs.

Credit point . You can do the work of implementing google if you change the last 2 characters of the character map: '$', '_' to '+', '/'.

My main conclusion is that the Base64 library you are using is an error. I suggest that when searching for alternative implementations try to keep the byte stream between the image and the base64 encoder in binary format (byte []). A string can work if the same encoding / decoding is used, but it is risky if encoding and decoding are performed in two different places (for example, client / server).

Good luck

PS: Try the following :-)

 <img src="data:unknown;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBhQRERQSDxQVFRUWGR4YGRgYFhgdFxUcHxgYGR4fIBsYGysfICEvGhgZHy8sIyswLCwsISIxNTIrNSYsLC0BCQoKDgwOGQ8NGjUjHiQ1KikzMTEwMDUvKTU1NTArNTU1NSkxNDUwKjA1MSo1LCo1NTUsMDU0LDYvKTYpNjUpLP/AABEIAEMA8AMBIgACEQEDEQH/xAAbAAEAAwEBAQEAAAAAAAAAAAAABAUGBwMIAv/EAEMQAAEDAgQDBAUICAUFAAAAAAEAAgMEEQUGEiEHEzFBUWFxFCIygZEIFiNCcqGx0TRUkpOjssHwUlOCs+EVJjNiov/EABoBAQACAwEAAAAAAAAAAAAAAAAEBQECAwb/xAAjEQEAAgICAQQDAQAAAAAAAAAAAQIDEQQhMRMiUXFCcpEU/9oADAMBAAIRAxEAPwDuCIiAiLL57im5QfE4iMe20beRuN7f8Ljny+ljm+t6d+Ph9bJGPettQst88HenClMbQ3Xo1XJJ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VTYIanKRfa7qed8rfs6tLvucT7lX0ta7HZMJw67g2CItlPda9z+7YweZK79heUoKehFBGDyuW6M36u1AhxPiSSVjeFnCV+FTzT1EkcjnMDI9Gr1Rqu4nUOps3p4oOPVmZJIcLmweS/MZV9LfVGrU3960H3r6SyVgfoVBTU9rFkY1fbPrO/+iVh8b4NGfGW14fGIDIyR8Z1ay5tr22tYlo+JXU0BERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREH/2Q=="/> 

Edit: Further research

I spent more time than reasonably trying to find a solution. The GWT wrapper will not allow you to get raw data, but you can use JSNI to force the browser to get binary data:

 native String getBinaryResource(String url) /*-{ // ...implemented with JavaScript var req = new XMLHttpRequest(); req.open("GET", url, false); // The last parameter determines whether the request is asynchronous -> this case is sync. req.overrideMimeType('text/plain; charset=x-user-defined'); req.send(null); if (req.status == 200) { return req.responseText; } else return null }-*/; 

Love to crack a hard nut, but I applied all this, but, however, base64 encoding does not work. JS and Java wrappers still have an encoding problem, and I can't get the string to decode back to the correct byte []. Tried all possible coding combinations. A possible way would be to use base64 req.responseText using your own javascript library and return String to a java copy.

So far for your initial question.

Now, looking at some alternative ideas and requirements behind your question: during my research, I saw that Base64 is very often used for embedded images on the server side , to avoid the additional HTTP overhead to get images on the client. This seems to be a popular alternative to CSS-inlining.

In the context of this question, the code works with the client (browser), and these reasons do not apply. Using the basic XMLHTTPRequest to retrieve the binary image file creates an additional HTTP request from your browser to the server. Given that in the client context, you obviously have the image URL (passed to IoProvider.get().makeRequest(*url*,...) ), there is a reason why we could not allow the browser to do the job with a simple dynamic update if the image object:

(this may not be the best code sample, but it gives you an image (sorry for the pun ;-))

 void setImage(String url) { final HTML imageHolder = new HTML(); String imgTag = "<IMG src='"+url+"' />'"; imageHolder.setHTML(imgTag); RootPanel.get("imageContainer").add(imageHolder); // imageContainer is a div } 

Edit: nut cracked

Finally, I found the last missing piece of the puzzle: how to correctly decode binary data from a JavaScript string. Note some caveats: it will not work in IE if they do not support the overrideMimeType method in native XMLHTTPRequest.

 native String getBinaryResource(String url) /*-{ var req = new XMLHttpRequest(); req.open("GET", url, false); // The last parameter determines whether the request is asynchronous. req.overrideMimeType('text/plain; charset=x-user-defined'); req.send(null); if (req.status == 200) { return req.responseText; } else return null }-*/; private void sendRequestBinary() { String url = URL.encode("/Computer_File_030.gif"); String data = getBinaryResource(url); if (data != null) { // The secret sauce: Method to decode the binary data in the response string byte[] binData = new byte[data.length()]; for (int i=0;i<data.length();i++) { binData[i] = (byte)(data.charAt(i) & 0xff); } final HTML imageHolder = new HTML(); String base64=Base64Utils.toBase64(binData); String imgTag = "<IMG src='data:image/gif;base64,"+base64+"' />'"; imageHolder.setHTML(imgTag); RootPanel.get("imageContainer").add(imageHolder); errorLabel.setText("Base64:"); } else { errorLabel.setText("Another error :-("); } } 
+6
source

Your encoded image The string appears to be incorrect you missed, including the type of image content.

http://en.wikipedia.org/wiki/Data_URI_scheme

data: [] [; encoding =] [; base64],

0
source

Are you sure that the image data you received is correct? Since you request the image as text, perhaps the GWT processes binary data before calling your callback (for example, avoiding non-printable characters).

0
source

unknown not a valid MIME type. Your data URI should start with data:image/png or whatever else is appropriate for the image to be displayed. The wrong MIME type, despite this, the file utility does not recognize decoded data from the first URL that you posted. What file should it be?

0
source

Here is a little sample code from which I needed to create the server side of Captcha as a means of containing the bot. You seem to know that you are doing this in order to be brief, I leave the servlet setup.

In any case, there are 2 places, I see that everything is going wrong.

1) Your response.getText () is not the correct encoding of the image. Only problem is if you are loading from the database or using a tool that generates an image as an instance of the Java Image class.

2) You must set the mime type. data: unknown must be a valid mimetype type, for example, data: image / jpeg (Reference http://www.w3schools.com/media/media_mimeref.asp ) If you did not specify a mimetype type, the browser should guess, and if it is not correctly guessed, you will not see the image.

I suggest using the installed codec with the appropriate mime type (jpeg, gif, png), provided that you are not loading a file from byte [], and I also suggest using sun.misc.BASE64Encoder (). encode (byte []), as you can be sure that it is working as intended.

Client side

 ThecoderslibraryGWT.loginService.capImage( new AsyncCallback<String>() { @Override public void onFailure(Throwable caught) { error.setHTML(caught.getMessage()); } @Override public void onSuccess(String result) { String base64EncodedImage = result; img.setUrl("data:image/jpg;base64," + base64EncodedImage); vp.setVisible(true); } }); 


Server side
 /** * 64 Bit encoded captcha image */ public String capImage() { byte[] data = null; // the output stream to render the captcha image as jpeg ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream(); try { // get the session id that will identify the generated captcha. //the same id must be used to validate the response, the session id is a good candidate! String captchaId = this.getThreadLocalRequest().getSession().getId(); BufferedImage challenge = CaptchaServiceSingleton.getInstance().getImageChallengeForID(captchaId, this.getThreadLocalRequest().getLocale()); // a jpeg encoder JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream); jpegEncoder.encode(challenge); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(challenge, "jpg", os); data= os.toByteArray(); } catch (IllegalArgumentException e) { e.printStackTrace(); throw new IllegalArgumentException("Unable to create validation image."); } catch (CaptchaServiceException e) { e.printStackTrace(); throw new IllegalArgumentException("Unable to create validation image."); } catch (IOException e) { e.printStackTrace(); throw new IllegalArgumentException("Unable to create validation image."); } return new sun.misc.BASE64Encoder().encode(data); } 
0
source

2 possible reasons:

1) Your server-side code, viewing the image and sending it via HTTP, fiddles with the encoding of the binary image format before entering Base64.encode (response.getText ()). If you have access to the server side, Base64.encode your image on the server and simply transfer it through:

 callback.onSuccess("data:unknown;base64,"+ response.getText()); // response already in Base64 

2) If you do not have access to the server side, try to avoid the line feed response.getText ().

 new ResponseReceivedHandler<byte[]>() { public void onResponseReceived(ResponseReceivedEvent<byte[]> event) { final Response<byte[]> response = event.getResponse(); if (response.getStatusCode() == HTTP_OK){ callback.onSuccess("data:unknown;base64," + Base64.encode(**response.getData()**)); } } }, options); } catch ... 
0
source

for which it is worth publishing a solution for sending images to the server. He used the RPC calls that I read, you cannot use, but maybe this helps.

//Client

 public void onModuleLoad() { HTML html = new HTML( "<img src=\"data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0 vr4MkhoXe0rZigAAAABJRU5ErkJggg==\" alt=\"Red dot\">"); RootPanel.get().add(html); greetingService.greetServer("", new AsyncCallback<String>() { @Override public void onSuccess(String result) { HTML html = new HTML( "<img src=\"data:image/png;base64, "+ result + "\" alt=\"Window Logo\">"); RootPanel.get().add(html); RootPanel.get().add(new Label(result)); } @Override public void onFailure(Throwable caught) { // TODO Auto-generated method stub } }); } : image / png; base64, iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs + 9AAAABGdBTUEAALGP C / xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV + IA AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq ch9 // q1uH4TLzw4d6 + ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0 vr4MkhoXe0rZigAAAABJRU5ErkJggg == \ "alt = \" Red dot \ ">"); public void onModuleLoad() { HTML html = new HTML( "<img src=\"data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0 vr4MkhoXe0rZigAAAABJRU5ErkJggg==\" alt=\"Red dot\">"); RootPanel.get().add(html); greetingService.greetServer("", new AsyncCallback<String>() { @Override public void onSuccess(String result) { HTML html = new HTML( "<img src=\"data:image/png;base64, "+ result + "\" alt=\"Window Logo\">"); RootPanel.get().add(html); RootPanel.get().add(new Label(result)); } @Override public void onFailure(Throwable caught) { // TODO Auto-generated method stub } }); } 

//server

public String greetServer (String input) throws IllegalArgumentException {

  // BASE64Encoder try{FileInputStream img = new FileInputStream( "C:\\icon_a.png"); ByteArrayBuffer bab = new ByteArrayBuffer(0); int eof = 0; while (eof != -1) { eof = img.read(); bab.append(eof); } String rets = new BASE64Encoder().encode(bab.toByteArray()); return rets; }catch (Exception e) { // TODO: handle exception } return null; } 

PS: image http://www.drweb.de/icons/twitter/pd_twitter_iconset/pd_twitter_iconset/PNG/256/icon_a.png

0
source

Source: https://habr.com/ru/post/890912/


All Articles