【问题标题】:Flex File reading problemFlex文件读取问题
【发布时间】:2011-02-02 09:09:06
【问题描述】:

我在从 Flex 读取文件时遇到问题。该文件包含一个 base64 编码的字符串。当我读取文件时,我得到的长度为 47856,解码后的 base64 字节数组长度为 34157。

当我从 java 读取相同的文件时,我得到的长度分别为 48068 和 35733。

有什么问题?

 private function init():void{
        var file:File = File.desktopDirectory.resolvePath("Files/sample.txt");
        stream = new FileStream();
        stream.open(file, FileMode.READ);
        var str:String = stream.readUTFBytes(stream.bytesAvailable);
        stream.close();
        str = str.replace(File.lineEnding, "\n");
        contents.text = str;
        fileName.text = file.name;
    }


public function playSound(contents:String):void{



    try{
        var byteData: ByteArray;

        byteData = new ByteArray();
        byteData.writeUTFBytes(contents);
        var dec:Base64Decoder = new Base64Decoder();
        dec.decode(contents);
        byteData = dec.toByteArray();



        Alert.show("byte Array   " + byteData.toString().length +" ::  " +contents.length);


    }

这是我用于读取文件的 java 代码...无论我期望什么结果都可以在 java 端实现。

private static String readFile(String path) throws IOException {  
        FileInputStream stream = new FileInputStream(new File(path)); 
        try {   
            FileChannel fc = stream.getChannel(); 
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());   

            return Charset.defaultCharset().decode(bb).toString();   } 
        finally {     stream.close();   
        } 
    } 

我打印长度的Java代码

byte[] decodedBase64 = new byte[byteLength];
                String speexData = null;
                try {
                    speexData =   readFile(userDir +"//" +xmlFileName);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                // System.out.println("sa " + sa);

                try{

                    decodedBase64=  Base64.decodeToByteArray(speexData);
                    System.out.println("decodednase64 length " + decodedBase64.length +" :: " +speexData.length());
                }
                catch(Exception e){
                }

【问题讨论】:

    标签: java apache-flex actionscript


    【解决方案1】:

    您还必须发布您的 java 代码以显示您在那里所做的事情。

    但是,在不知道更多信息的情况下,我可以猜测一下,当您替换行尾时,您可能每次都删除一个字节(如果它是 \r\n 并且您正在制作它 \n,因为示例)。

    【讨论】:

    • 如果删除替换行尾的行会怎样?
    • @above: 实际上我得到了我需要的输出..但是我还是不明白为什么会有这样的差异。
    • 在您的闪存代码中,您为什么不查看 stream.bytesAvailable 和 byteData.bytesAvailable?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 2014-07-14
    • 2012-05-09
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多