using System; using System.IO; using System.Net; using System.Text; namespace Common { public class WebUpDown { /// <summary> /// 检查文件格式是否正确 /// </summary> /// <param name="FileName">文件名称</param> /// <param name="ForMart">格式</param> /// <returns></returns> public static bool FileFormat(string FileName, string[] ForMart) { bool success = false; String fileExtension = System.IO.Path.GetExtension(FileName).ToLower(); String[] allowedExtensions = ForMart; for (int i = 0; i < allowedExtensions.Length; i++) { if (fileExtension.ToUpper() == allowedExtensions[i].ToUpper()) { success = true; break; } } return success; } /// <summary> /// 将指定Excel文件中读取第一张工作表的名称 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static string GetSheetName(string filePath) { string sheetName = ""; System.IO.FileStream tmpStream = File.OpenRead(filePath); byte[] fileByte = new byte[tmpStream.Length]; tmpStream.Read(fileByte, 0, fileByte.Length); tmpStream.Close(); byte[] tmpByte = new byte[]{Convert.ToByte(11),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0), Convert.ToByte(11),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0), Convert.ToByte(30),Convert.ToByte(16),Convert.ToByte(0),Convert.ToByte(0)}; int index = GetSheetIndex(fileByte, tmpByte); if (index > -1) { index += 16 + 12; System.Collections.ArrayList sheetNameList = new System.Collections.ArrayList(); for (int i = index; i < fileByte.Length - 1; i++) { byte temp = fileByte[i]; if (temp != Convert.ToByte(0)) sheetNameList.Add(temp); else break; } byte[] sheetNameByte = new byte[sheetNameList.Count]; for (int i = 0; i < sheetNameList.Count; i++) sheetNameByte[i] = Convert.ToByte(sheetNameList[i]); sheetName = System.Text.Encoding.Default.GetString(sheetNameByte); } return sheetName; } /// <summary> /// 只供方法GetSheetName()使用 /// </summary> /// <returns></returns> private static int GetSheetIndex(byte[] FindTarget, byte[] FindItem) { int index = -1; int FindItemLength = FindItem.Length; if (FindItemLength < 1) return -1; int FindTargetLength = FindTarget.Length; if ((FindTargetLength - 1) < FindItemLength) return -1; for (int i = FindTargetLength - FindItemLength - 1; i > -1; i--) { System.Collections.ArrayList tmpList = new System.Collections.ArrayList(); int find = 0; for (int j = 0; j < FindItemLength; j++) { if (FindTarget[i + j] == FindItem[j]) find += 1; } if (find == FindItemLength) { index = i; break; } } return index; } /// <summary> /// 下载服务器文件至客户端 /// </summary> /// <param name="URL">被下载的文件地址,绝对路径</param> /// <param name="Dir">另存放的目录</param> public static void Download(string URL, string Dir) { WebClient client = new WebClient(); string fileName = URL.Substring(URL.LastIndexOf("/") + 1); //被下载的文件名 string Path = Dir; //另存为的绝对路径+文件名 try { client.DownloadFile(URL, Path); } catch (Exception ex) { throw ex; } } public static string Getjson(string URL) { try { Uri httpURL = new Uri(URL.ToString()); WebRequest webReq = WebRequest.Create(httpURL); WebResponse webResp = webReq.GetResponse(); Stream stream = webResp.GetResponseStream(); StreamReader sr = new StreamReader(stream, Encoding.UTF8); string html = sr.ReadToEnd(); sr.Close(); stream.Close(); return html; } catch (Exception ex) { throw ex; } } } }
判断文件格式和请求http网址
于 2018-04-04 11:31:33 首次发布