判断文件类型
用后缀名判断文件类型是不够安全的
http://www.xueit.com/html/2009-12-21/21-1615393093875.html
http://xbing612.blog.sohu.com/138527552.html
代码
private string IsPicture(Stream filestream)//filePath是文件的完整路径
{
try
{
//FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(filestream);
string fileClass;
byte buffer;
buffer = reader.ReadByte();
fileClass = buffer.ToString();
buffer = reader.ReadByte();
fileClass += buffer.ToString();
reader.Close();
filestream.Close();
//.Close();
return fileClass;
//if (fileClass == "255216 ")//255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
//{
// return true;
//}
//else
//{
// return false;
//}
}
catch
{
return "";
}
}
{
try
{
//FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(filestream);
string fileClass;
byte buffer;
buffer = reader.ReadByte();
fileClass = buffer.ToString();
buffer = reader.ReadByte();
fileClass += buffer.ToString();
reader.Close();
filestream.Close();
//.Close();
return fileClass;
//if (fileClass == "255216 ")//255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
//{
// return true;
//}
//else
//{
// return false;
//}
}
catch
{
return "";
}
}