代码之家  ›  专栏  ›  技术社区  ›  Josh K

Java/Groovy中的Base64编码

  •  26
  • Josh K  · 技术社区  · 14 年前

    在Java中,将byte[]转换为Base64字符串的正确方法是什么?最好是Grails/Groovy,因为它告诉我 encodeAsBase64() 函数已弃用。这个 sun.misc.BASE64Encoder 不建议使用包,并在某些Windows平台上输出不同大小的字符串。

    5 回复  |  直到 14 年前
        2
  •  87
  •   ataylor    14 年前

    在groovy中,首选的方法是:

     def encoded = "Hello World".bytes.encodeBase64().toString()
     assert encoded == "SGVsbG8gV29ybGQ="
     def decoded = new String("SGVsbG8gV29ybGQ=".decodeBase64())
     assert decoded == "Hello World"
    
        3
  •  2
  •   Mark O'Connor    14 年前

    你可以使用开源 Base64Coder 图书馆

    import biz.source_code.base64Coder.Base64Coder
    
    @Grab(group='biz.source_code', module='base64coder', version='2010-09-21')
    
    String s1 = Base64Coder.encodeString("Hello world")
    String s2 = Base64Coder.decodeString("SGVsbG8gd29ybGQ=")
    
        4
  •  1
  •   srsajid    10 年前

    实现自己的方法如下:)

    public class Coder {
    private static final String base64code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    
    public static String encodeAsBase64(String toEncode) {
        return encodeAsBase64(toEncode.getBytes())
    }
    
    public static String encodeAsBase64(byte[] toEncode) {
        int pos = 0;
        int onhand = 0;
    
        StringBuffer buffer = new StringBuffer();
        for(byte b in toEncode) {
            int read = b;
            int m;
            if(pos == 0) {
                m = (read >> 2) & 63;
                onhand = read & 3;
                pos = 1;
            } else if(pos == 1) {
                m = (onhand << 4) + ((read >> 4) & 15);
                onhand = read & 15;
                pos = 2;
            } else if(pos == 2) {
                m = ((read >> 6) & 3) + (onhand << 2);
                onhand = read & 63;
                buffer.append(base64code.charAt(m));
                m = onhand;
                onhand = 0;
                pos  = 0;
            }
            buffer.append(base64code.charAt(m));
        }
        while(pos > 0 && pos < 4) {
            pos++;
            if(onhand == -1) {
                buffer.append('=');
            } else {
                int m = pos == 2 ? onhand << 4 : (pos == 3 ? onhand << 2 : onhand);
                onhand = -1;
                buffer.append(base64code.charAt(m));
            }
        }
        return buffer.toString()
    }
    

    }

        5
  •  0
  •   Stefan Becker    6 年前

    (将此添加到该线程中,希望其他人能对此有所帮助,而不必浪费宝贵的时间)

    今天,当我试图在Grails 2.3.11/groovy2.1.9应用程序中添加

    String src = render(
            model:    ...,
            template: ...,
        )
        .encodeAsBase64()
    

    作为一个 data- 属性设置为DOM元素。但是 atob() 在相应的JavaScript中,即从data属性解码Base64字符串的代码,一直抱怨非法字符,而其他解码器,例如。 base64 -d 接受相同的Base64字符串没有问题。

    解决方法是强制 render() 将值返回到单个字符串,然后应用Base64编码,即。

    String src = render(
            model:    ...,
            template: ...,
        )
        .toString()
        .encodeAsBase64()
    

    或者(如果你考虑 encodeAsBase64() 已弃用):

    String src = render(
            model:    ...,
            template: ...,
        )
        .toString()
        .bytes
        .encodeBase64() // add 'true' for chunked output