Tag Archives: sun.misc.BASE64Decoder

IDEA compile error: sun.misc.Base64decoder upgrade processing

Idea compile error report sun.misc.BASE64Decoder upgrade processing
warn:
17:01:15 /deploy/jenkins/workspace/auto-java-test/utils/ImageBase64Utils.java:67: warning: BASE64Encoder is internal proprietary API and may be removed in a future release
17:01:15 BASE64Encoder encoder = new BASE64Encoder();
import sun.misc.BASE64Decoder;
Alternative writing:
//Since JDK 1.8, the JDK public APIs of java.util.Base64.Decoder and java.util.Base64.Encoder have been provided, which can replace the JDK internal APIs of sun.misc.BASE64Decoder and sun.misc.BASE64Encoder.
//byte[] bytes = new BASE64Decoder().decodeBuffer(base64);
byte[] bytes = Base64.getDecoder().decode(base64);

//Or use method
import org.apache.commons.codec.binary.Base64;
return Base64.encodeBase64String(encrypted);

demo test class

@Test
    public void test2() throws Exception{
        //Decoder and java.util.Base64.Encoder are available from JDK 1.8 onwards, replacing the internal JDK APIs of sun.misc. .
        //byte[] bytes = new BASE64Decoder().decodeBuffer(base64);

        System.out.println("-----------------------Early Write ----------------------");
        String text = "String text";
         BASE64Encoder encoder = new BASE64Encoder();
         BASE64Decoder decoder = new BASE64Decoder();
         byte[] textByte = text.getBytes("UTF-8");
        //Code
         String encodedText = encoder.encode(textByte);
        System.out.println(encodedText);
        //Decoding
        System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8"));

        /**
         * Compared with the Base64 codecs provided by sun.mis c suite and Apache Commons Codec, the Base64 provided by Java 8 has better performance. In the actual test of encoding and decoding speed, Base64 provided by Java 8 is at least 11 times faster than that provided by sun.mis c suite and at least 3 times faster than that provided by Apache Commons Codec. So if you want to use Base64 in Java, the Base64 category provided by the java .util package under Java 8 is definitely the first choice!
         * https://blog.csdn.net/zhou_kapenter/article/details/62890262
         */
        System.out.println("-----------------------New writing style----------------------");
        byte[] test11 = Base64.getEncoder().encode(textByte);
        System.out.println("test11_string="+new String(test11, "UTF-8"));
        byte[] bytes11 = Base64.getDecoder().decode(test11);
        System.out.println("test11="+new String(bytes11, "UTF-8"));

        System.out.println("-----------------------apache writing style----------------------");
        String test22 = org.apache.tomcat.util.codec.binary.Base64.encodeBase64String(textByte);
        System.out.println("test22_string="+test22);
        byte[] bytes22 = org.apache.tomcat.util.codec.binary.Base64.decodeBase64(test22);
        System.out.println("test22="+new String(bytes22, "UTF-8"));

        /**
         * Print results: Consistent results
         * ----------------------- early write ----------------------
         * 5a2X5Liy5paH5a2X
         * String text
         * ----------------------- new write ----------------------
         * test11 string = 5a2X5Liy5paH5a2X
         * test11=string literal
         * -----------------------apache writeup ----------------------
         * test22 string=5a2X5Liy5paH5a2X
         * test22=word string literal
         */
    }

Sun.misc.base64decoder import exception and handling ideas

Java background save Base64 image data

use byte [] bytes = new base64decoder(). Decodebuffer (STR); Sun.misc.base64decoder needs to be introduced, but it is prompted in eclipse that this war package does not exist

It is mentioned in an article after consulting:

All along, base64 encryption and decryption use base64encoder under sun.misc package and sun.misc.base64encoder/base64decoder class of base64decoder. This class is an internal method of Sun company and has not been disclosed in Java API. It does not belong to JDK standard library. However, it is included in JDK and can be used directly. But you can’t find this class if you use it directly in eclipse and MyEclipse. “

The solution is as shown in the figure: right click Project – properties – Java build path – JRE system library – access rules – resolution, select accessible, fill in * * below and click OK

In addition, there are corresponding alternative objects and methods on the Internet, such as using

Org. Apache. Commons. Codec. Binary. Base64, etc. You can check it yourself

change to org.apache.commons.codec.binary.base64 instead

String base64Code = request.getParameter("base64Code");
Base64 base64 = new Base64();
byte[] bytes = base64.decodeBase64(new String(base64Code).getBytes());

Personal test, the effect is the same