Decode Base64 data in Java

前端 未结 20 1456
时光说笑
时光说笑 2020-11-21 06:04

I have an image that is Base64 encoded. What is the best way to decode that in Java? Hopefully using only the libraries included with Sun Java 6.

相关标签:
20条回答
  • 2020-11-21 06:41

    You can simply try this.

    byte[] data = Base64.getDecoder().decode(base64fileContent);
    

    "Base64.getDecode()" returns a Base64 Decoder that can be decoded. Then you need to decode that again using ".decode()"

    0 讨论(0)
  • 2020-11-21 06:42

    You can write or download file from encoded Base64 string:

    Base64 base64 = new Base64(); 
    String encodedFile="JVBERi0xLjUKJeLjz9MKMSAwIG9iago8PCAKICAgL1R5cGUgL0NhdGFsb2cKICAgL1BhZ2VzIDIgMCBSCiAgIC9QYWdlTGF5b3V0IC9TaW5"; 
                  byte[] dd=encodedFile.getBytes();
                byte[] bytes = Base64.decodeBase64(dd);
    
     response.setHeader("Content-disposition", "attachment; filename=\""+filename+"\"");
                response.setHeader("Cache-Control", "no-cache");
                response.setHeader("Expires", "-1");
    
                // actually send result bytes
                response.getOutputStream().write(bytes);
    

    Worked for me and hopefully for you also...

    0 讨论(0)
  • 2020-11-21 06:44

    The Java 8 implementation of java.util.Base64 has no dependencies on other Java 8 specific classes.

    I am not certain if this will work for Java 6 project, but it is possible to copy and paste the Base64.java file into a Java 7 project and compile it with no modification other than importing java.util.Arrays and java.util.Objects.

    Note the Base64.java file is covered by the GNU GPL2

    0 讨论(0)
  • 2020-11-21 06:45

    Another late answer, but my benchmarking shows that Jetty's implementation of Base64 encoder is pretty fast. Not as fast as MiGBase64 but faster than iHarder Base64.

    import org.eclipse.jetty.util.B64Code;
    
    final String decoded = B64Code.decode(encoded, "UTF-8");
    

    I also did some benchmarks:

          library     |    encode    |    decode   
    ------------------+--------------+-------------
     'MiGBase64'      |  10146001.00 |  6426446.00
     'Jetty B64Code'  |   8846191.00 |  3101361.75
     'iHarder Base64' |   3259590.50 |  2505280.00
     'Commons-Codec'  |    241318.04 |   255179.96
    

    These are runs/sec so higher is better.

    0 讨论(0)
  • 2020-11-21 06:46

    If You are prefering performance based solution then you can use "MiGBase64"

    http://migbase64.sourceforge.net/

    public class Base64Test {
        public static void main(String[] args) {
        String encodeToString = Base64.encodeToString("JavaTips.net".getBytes(), true);
        System.out.println("encodeToString " + encodeToString);
        byte[] decodedBytes = Base64.decode(encodeToString.getBytes());
        System.out.println("decodedBytes " + new String(decodedBytes));
        }
    }
    
    0 讨论(0)
  • 2020-11-21 06:47

    As of Java 8, there is an officially supported API for Base64 encoding and decoding. In time this will probably become the default choice.

    The API includes the class java.util.Base64 and its nested classes. It supports three different flavors: basic, URL safe, and MIME.

    Sample code using the "basic" encoding:

    import java.util.Base64;
    
    byte[] bytes = "Hello, World!".getBytes("UTF-8");
    String encoded = Base64.getEncoder().encodeToString(bytes);
    byte[] decoded = Base64.getDecoder().decode(encoded);
    

    The documentation for java.util.Base64 includes several more methods for configuring encoders and decoders, and for using different classes as inputs and outputs (byte arrays, strings, ByteBuffers, java.io streams).

    0 讨论(0)
提交回复
热议问题