判断文件格式和请求http网址

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;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值