做新闻文章,我们或多或少地转载别人的成果,那怎么样把图片也下载到自已的服务器里呢?我这有一段代码,敬请指教!

None.gif using  System;
None.gif
using  System.Text;
None.gif
using  System.Text.RegularExpressions;
None.gif
using  System.IO;
None.gif
None.gif
namespace  zhang.Common
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class HanlerFiles
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string[] GetImgTag(string htmlStr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Regex regObj 
= new Regex("<img.+?>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
InBlock.gif            
string[] strAry = new string[regObj.Matches(htmlStr).Count];
InBlock.gif            
int i = 0;
InBlock.gif            
foreach (Match matchItem in regObj.Matches(htmlStr))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strAry[i] 
= GetImgUrl(matchItem.Value);
InBlock.gif                i
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return strAry;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
private string GetImgUrl(string imgTagStr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string str = "";
InBlock.gif            Regex regObj 
= new Regex("http://.+.(?:jpg|gif|bmp|png)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
InBlock.gif            
foreach (Match matchItem in regObj.Matches(imgTagStr))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                str 
= matchItem.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return str;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根椐Html内空自动识别图像文件,并下载到服务器指定目录
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strHTML"></param>
InBlock.gif        
/// <param name="path"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public int SaveUrlPics(ref string strHTML, string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string[] imgurlAry = GetImgTag(strHTML);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for (int i = 0; i < imgurlAry.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//WebRequest req = WebRequest.Create(imgurlAry[i]);
InBlock.gif
                    string preStr = System.DateTime.Now.ToString() + "_";
InBlock.gif                    preStr 
= preStr.Replace("-""");
InBlock.gif                    preStr 
= preStr.Replace(":""");
InBlock.gif                    preStr 
= preStr.Replace(" """);
InBlock.gif                    WebClient wc 
= new WebClient();
InBlock.gif                    wc.DownloadFile(imgurlAry[i], HttpContext.Current.Server.MapPath(path) 
+ "/" + preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf("/"+ 1));
InBlock.gif                    strHTML 
= strHTML.Replace(imgurlAry[i], path + preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf("/"+ 1));
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//return ex.Message;
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
return imgurlAry.Length;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

至强工作室 www.haotaoci.com

转载于:https://www.cnblogs.com/zhang/archive/2007/07/25/830308.html

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐