Decode Base64 data in Java

2018-12-31 00:51发布

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.

标签: java base64
19条回答
无与为乐者.
2楼-- · 2018-12-31 01:14

Here is a working example using Apache Commons codec:

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));
}

Maven / sbt repo: commons-codec, commons-codec, 1.8.

查看更多
不再属于我。
3楼-- · 2018-12-31 01:14

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...

查看更多
忆尘夕之涩
4楼-- · 2018-12-31 01:16

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.

查看更多
零度萤火
5楼-- · 2018-12-31 01:17

Guava now has Base64 decoding built in.

Use BaseEncoding.base64().decode()

As for dealing with possible whitespace in input use

BaseEncoding.base64().decode(CharMatcher.WHITESPACE.removeFrom(...));

See this discussion for more information

查看更多
ら面具成の殇う
6楼-- · 2018-12-31 01:19

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));
    }
}
查看更多
伤终究还是伤i
7楼-- · 2018-12-31 01:21

As of v6, Java SE ships with JAXB. javax.xml.bind.DatatypeConverter has static methods that make this easy. See parseBase64Binary() and printBase64Binary().

查看更多
登录 后发表回答