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);然后再进行读取

  • 相关阅读:
    CF1109F Sasha and Algorithm of Silence's Sounds LCT、线段树
    Solution -「CF 757F」Team Rocket Rises Again
    Solution -「ZJOI2012」「洛谷 P2597」灾难
    Solution -「CF 156D」Clues
    「矩阵树定理」学习笔记
    Solution -「JSOI2008」「洛谷 P4208」最小生成树计数
    Solution -「SHOI2016」「洛谷 P4336」黑暗前的幻想乡
    Solution -「Code+#2」「洛谷 P4033」白金元首与独舞
    Solution -「HDU 5498」Tree
    呐~「多项式」全家桶
  • 原文地址:https://www.cnblogs.com/nanxiaoxiang/p/2624627.html
Copyright © 2011-2022 走看看