• 凌晨01:30 发一个求助 SOS 抓取远程无扩展名的图片


    非常感谢园子里的各位朋友的关注与帮助

    已近找到解决方案了  by 软件~民工

    解决方案:

    ---------------------------------------------

    通过判断文件内容的头2个字节可以确定文件格式
    255216是jpg //7173是gif //6677是BMP //13780是PNG

    结贴!!

    ---------------------------------------------------

    这是代码:

     1 <summary>
     2         /// 抓取远程图片 自动识别扩展名
     3         /// </summary>
     4         /// <param name="url"></param>
     5         /// <returns></returns>
     6         private string getRemoteImg(string url)
     7         {
     8             string rtnString = string.Empty;
     9             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    10             request.Method = "GET";
    11             try
    12             {
    13                 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    14                 {
    15                     string fileExt = string.Empty;
    16                     string fileName = DateTime.Now.ToString("yyyyMMddhhmmssss");
    17                     string FileExtNum = string.Empty;
    18                     using (Stream stream = response.GetResponseStream())
    19                     {
    20                         byte[] retBytes = null;
    21                         using (MemoryStream ms = new MemoryStream())
    22                         {
    23                             int b;
    24                             int i = 0;
    25                             while ((b = stream.ReadByte()) != -1)
    26                             {
    27                                 i++;
    28                                 if (i <= 2)
    29                                 {
    30                                     FileExtNum += b.ToString();
    31                                 }
    32                                 ms.WriteByte((byte)b);
    33                             }
    34                             retBytes = ms.ToArray();
    35                         }
    36                         switch (FileExtNum)
    37                         {
    38                             case "255216":
    39                                 fileExt = "jpg";
    40                                 break;
    41 
    42                             case "7173":
    43                                 fileExt = "gif";
    44                                 break;
    45 
    46                             case "13780":
    47                                 fileExt = "png";
    48                                 break;
    49 
    50                             case "6677 ":
    51                                 fileExt = "bmp";
    52                                 break;
    53                         }
    54                         if (!string.IsNullOrEmpty(fileExt))
    55                         {
    56 
    57                             if (!Directory.Exists(Application.StartupPath + "\\UploadFiles\\"))//若文件夹不存在则新建文件夹   
    58                             {
    59                                 Directory.CreateDirectory(Application.StartupPath + "\\UploadFiles\\"); //新建文件夹   
    60                             }
    61                             string localPath = Application.StartupPath + "\\UploadFiles\\" + fileName + "." + fileExt + "";
    62                             using (FileStream fs = new FileStream(localPath, FileMode.Create))
    63                             {
    64                                 using (BinaryWriter sr = new BinaryWriter(fs))
    65                                 {
    66                                     sr.Write(retBytes, 0, retBytes.Length);
    67                                     sr.Close(); fs.Close();
    68                                     if (System.IO.File.Exists(localPath))
    69                                     {
    70                                         rtnString = "UploadFiles/" + fileName + "." + fileExt + "";
    71                                     }
    72                                 }
    73                             }
    74                         }
    75                     }
    76                 }
    77             }
    78             catch (Exception ex)
    79             {
    80                 throw ex;
    81             }
    82             return rtnString;

    83         } 

    这样的问题不应该发到首页来,但我实在是找不到方法了,百度GG都翻遍了,都没有找到方法来下载一个没有扩展名的图片。

    由于图片没有扩展名 所下载的时候就不知道以什么样的格式来保存到本地。

    图片如下: 可以点击右键去看下地址(因为直接发出地址,会有人说我广告嫌疑。)

    所以请求园子里的高手写一个方法 能实现下载无扩展名的图片,并保存到本地(本地名称可以随便取)。若图片是JPG的 就保存为jpg,GIF的就保存为GIF

    我在网上找了唯一一个说是下载无扩展名的图片的方法,但没用,我贴在下面供大家参考下。

    代码
            private void getImage(string url) {
                HttpWebRequest request 
    = (HttpWebRequest)WebRequest.Create(url);
                request.Method 
    = "GET";
                
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    
    string fileName = response.Headers.Get("Content-Disposition");
                    
                    fileName 
    = fileName.Substring(fileName.IndexOf("\"") + 1, fileName.LastIndexOf("\""- fileName.IndexOf("\"") - 1);
                    byte[] buf = new byte[response.ContentLength];
                    
    using (Stream stream = response.GetResponseStream())
                    {
                        stream.Read(buf, 
    0, buf.Length);
                    }
                    
    using (FileStream fs = File.Open(@"D:\" + fileName, FileMode.Create))
                    {
                        fs.Write(buf, 
    0, buf.Length);
                        fs.Flush();
                    }
                }
            }
  • 相关阅读:
    聚焦WCF行为的扩展
    软件设计经典书籍推荐
    善变者常新
    开发WCF/Silverlight须知
    面向对象设计讲义
    站立会议变形记
    敏捷开发思想之拥抱变化
    WCF 4.0中的WSDiscovery
    QCon日记
    创投“黑帮”,必须的
  • 原文地址:https://www.cnblogs.com/chjf2008/p/1811693.html
Copyright © 2020-2023  润新知