java - Convert Image to Base64 using Apache Commons -


i'm using base64encoder of sun micro systems convert image base64 string. problem i'm getting warnings during build don't want

java code..

     public static string encodetostring(bufferedimage image, string type) {     string base64string = null;     bytearrayoutputstream bos = new bytearrayoutputstream();     try {     imageio.write(image, type, bos);     byte[] imagebytes = bos.tobytearray();     base64encoder encoder = new base64encoder();     base64string = encoder.encode(imagebytes);     bos.close();     } catch (ioexception e) {     e.printstacktrace();     }     return base64string;     } 

warnings:

  [javac] /users/lucy/dev/workspace/flsv2/src/util/bufferimage.java:53: warning: base64encoder internal proprietary api , may removed in future release    [javac] base64encoder encoder = new base64encoder();    [javac] ^    [javac] /users/lucy/dev/workspace/flsv2/src/util/bufferimage.java:53: warning: base64encoder internal proprietary api , may removed in future release    [javac] base64encoder encoder = new base64encoder();    [javac] ^    [javac] 2 warnings 

is there way use apache commons in above method no warnings generated? please help..

please not use internal com.sun.* packages. if on v6 or greater can use datatypeconverter. code below.

string base64string = datatypeconverter.printbase64binary(baos.tobytearray()); byte[] bytearray = datatypeconverter.parsebase64binary(base64string); 

or,

you can use: java.util.base64 added in java 1.8

string base64string = new string(base64.getencoder().encode(bytestoencode)); byte[] decordedvalue = base64.getdecoder().decode(base64string); 

or, can make method calling procedure in apache commons given below

import org.apache.commons.codec.binary.base64; import org.apache.commons.codec.binary.stringutils;  public string decode(string s) {     return stringutils.newstringutf8(base64.decodebase64(s)); } public string encode(string s) {     return base64.encodebase64string(stringutils.getbytesutf8(s)); } 

resource link:

  1. org.apache.commons.codec.binary.base64 example
  2. how avoid warning base 64?

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -