Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Base64无法将字符串解码为字节数组_Java_String_Base64_Byte_Lzw - Fatal编程技术网

Java Base64无法将字符串解码为字节数组

Java Base64无法将字符串解码为字节数组,java,string,base64,byte,lzw,Java,String,Base64,Byte,Lzw,我尝试使用Base64将字符串解码为字节数组。但它返回了null。代码如下: LZW lzw = new LZW(); String enkripEmbedFileString = Base64.encode(byteFile); List<Short> compressed = lzw.compress(enkripEmbedFileString); String kompress = ""; Iterator<Short> c

我尝试使用
Base64
字符串
解码为字节数组。但它返回了
null
。代码如下:

    LZW lzw = new LZW();
    String enkripEmbedFileString = Base64.encode(byteFile);
    List<Short> compressed = lzw.compress(enkripEmbedFileString);

    String kompress = "";
    Iterator<Short> compressIterator = compressed.iterator();
    while (compressIterator.hasNext()) {
        String sch = compressIterator.next().toString();
        int in = Integer.parseInt(sch);
        char ch = (char) in;
        kompress = kompress + ch;
    }

    byteFile = Base64.decode(kompress);

请给我一个解释。我的错在哪里?

Base64解码只能应用于包含Base64编码数据的字符串。由于先进行编码,然后进行压缩,因此结果不是Base64。当您看到先解压缩数据,然后解码Base64字符串时,您自己就证明了这一点。

您是否检查(即打印)了输入Base64解码器的字符串?它是有效的Base64吗?你的意思是,Base64只能将用Base64编码的字符串解码为?这是合乎逻辑的,是的。尝试将Base64解码应用于一个不是先前Base64编码输出的字符串会产生什么结果?这没有任何意义。所以,当然我的代码返回null,因为字符串已经用LZW修改,并且不再与Base64兼容?好的,非常感谢@Jim Garrison
    //LZW Compress      
    LZW lzw = new LZW();
    String enkripEmbedFileString = Base64.encode(byteFile);
    List<Short> compressed = lzw.compress(enkripEmbedFileString);

    String kompress = "";
    Iterator<Short> compressIterator = compressed.iterator();
    while (compressIterator.hasNext()) {
        String sch = compressIterator.next().toString();
        int in = Integer.parseInt(sch);
        char ch = (char) in;
        kompress = kompress + ch;
    }

    //Decompress        
    List<Short> kompressback = back(kompress);
    String decompressed = decompress(kompressback);

    byteFile = Base64.decode(decompressed);