泡泡饭

导航

未知扩展名图片下载

/// <summary>
        
/// 抓取远程图片 自动识别扩展名
        
/// </summary>
        
/// <param name="url"></param>
        
/// <returns></returns>
        private string getRemoteImg(string url)
        {
            
string rtnString = string.Empty;
            HttpWebRequest request 
= (HttpWebRequest)WebRequest.Create(url);
            request.Method 
= "GET";
            
try
            {
                
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    
string fileExt = string.Empty;

                    
string fileName = DateTime.Now.ToString("yyyyMMddhhmmssss");
                    
string FileExtNum = string.Empty;
                    
using (Stream stream = response.GetResponseStream())
                    {
                        
byte[] retBytes = null;
                        
using (MemoryStream ms = new MemoryStream())
                        {
                            
int b;
                            
int i = 0;
                            
while ((b = stream.ReadByte()) != -1)
                            {
                                i
++;
                                
if (i <= 2)
                                {
                                    FileExtNum 
+= b.ToString();
                                }
                                ms.WriteByte((
byte)b);
                            }
                            retBytes 
= ms.ToArray();
                        }
                        
switch (FileExtNum)
                        {
                            
case "255216":
                                fileExt 
= "jpg";
                                
break;

                            
case "7173":
                                fileExt 
= "gif";
                                
break;

                            
case "13780":
                                fileExt 
= "png";
                                
break;

                            
case "6677 ":
                                fileExt 
= "bmp";
                                
break;
                        }
                        
if (!string.IsNullOrEmpty(fileExt))
                        {

                            
if (!Directory.Exists(Application.StartupPath + "\\UploadFiles\\"))//若文件夹不存在则新建文件夹   
                            {
                                Directory.CreateDirectory(Application.StartupPath 
+ "\\UploadFiles\\"); //新建文件夹   
                            }
                            
string localPath = Application.StartupPath + "\\UploadFiles\\" + fileName + "." + fileExt + "";
                            
using (FileStream fs = new FileStream(localPath, FileMode.Create))
                            {
                                
using (BinaryWriter sr = new BinaryWriter(fs))
                                {
                                    sr.Write(retBytes, 
0, retBytes.Length);
                                    sr.Close(); fs.Close();
                                    
if (System.IO.File.Exists(localPath))
                                    {
                                        rtnString 
= "UploadFiles/" + fileName + "." + fileExt + "";
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
catch (Exception ex)
            {
                
throw ex;
            }
            
return rtnString;
        }

posted on 2011-03-25 10:56  泡泡饭  阅读(285)  评论(0编辑  收藏  举报