zoukankan      html  css  js  c++  java
  • 通过流来判断文件的类型

     int count = 4;//视频用4byte
                byte[] byteTemp = new byte[count];
                int readCn = stream.Read(byteTemp, 0, 4);
                for (int i = 0; i < readCn; i++)
                {
                    s += byteTemp[i].ToString();
                }

                if (type == 1)
                {  //flv mpg  rmvb  wmv   avi 3gp  视频的判断
                    if (s == "00028" || s == "001186" || s == "46827770" || s == "4838178117" || s == "82737070")
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else if (type == 0)
                {
                    s = byteTemp[0].ToString() + byteTemp[1].ToString();
                    //jpg  png gif bmp图片的类型判断
                    if (s == "255216" || s == "13780" || s == "7173" || s == "6677")
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }

    这样的判断防止 修改后缀名来改变类型。

    2.当读取流的时候,注意流的开始位置

    当两个函数,都对一个流进行操作的时候,流的指针的位置很关键,当第二个函数也是从流的开始读取,在第一个函数里,或者第二个函数的开始,要让流的位置回到起点

    //stream.Seek(0, SeekOrigin.Begin); //设置当前流的位置 为流的开始

    或者

     stream.Position = 0;//设置当前流的位置 为流的开始
     int readCn = stream.Read(byteTemp, 0, 4);然后再进行读取

  • 相关阅读:
    linux下通过命令行把文件拷贝到U盘上
    Fuzzy finder(fzf+vim) 使用入门指南
    利器: Mac自带的图片工具Sips
    C/C++性能测试工具GNU gprof
    ubuntu 16.04安装perf
    带你了解SDL
    Android USB Headset: Device Specification
    程序猿的看迪士尼
    音频处理贤内助--libsndfile
    蓝牙协议中的SBC编解码原理和仿真
  • 原文地址:https://www.cnblogs.com/nanxiaoxiang/p/2624627.html
Copyright © 2011-2022 走看看