做文章类的网站,难免存在抄袭或修改别人的文章,占为己有的可能!
并非是偷者有罪,而是奉行拿来主义;毕竟个人的观点和学问都有所限制,不可能面面俱到;拿别人的不等于偷,更有可能的就是,在别人的基础之上在进行升华,以求精益求精!
为此,众多站长可能遇到过,“抄袭”别人的网站的时候,存在图片,如何将其自动保存到自己网站的目录呢?
以下为网上抄的源代码!
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
namespace zhang.Common
{
public class HanlerFiles
{
private string[] GetImgTag(string htmlStr)
{
Regex regObj = new Regex("<img.+?>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string[] strAry = new string[regObj.Matches(htmlStr).Count];
int i = 0;
foreach (Match matchItem in regObj.Matches(htmlStr))
{
strAry[i] = GetImgUrl(matchItem.Value);
i++;
}
return strAry;
}
private string GetImgUrl(string imgTagStr)
{
string str = "";
Regex regObj = new Regex("http://.+.(?:jpg|gif|bmp|png)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (Match matchItem in regObj.Matches(imgTagStr))
{
str = matchItem.Value;
}
return str;
}
/**//// <summary>
/// 根椐Html内空自动识别图像文件,并下载到服务器指定目录
/// </summary>
/// <param name="strHTML"></param>
/// <param name="path"></param>
/// <returns></returns>
public int SaveUrlPics(ref string strHTML, string path)
{
string[] imgurlAry = GetImgTag(strHTML);
try
{
for (int i = 0; i < imgurlAry.Length; i++)
{
//WebRequest req = WebRequest.Create(imgurlAry[i]);
string preStr = System.DateTime.Now.ToString() + "_";
preStr = preStr.Replace("-", "");
preStr = preStr.Replace(":", "");
preStr = preStr.Replace(" ", "");
WebClient wc = new WebClient();
wc.DownloadFile(imgurlAry[i], HttpContext.Current.Server.MapPath(path) + "/" + preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf("/") + 1));
strHTML = strHTML.Replace(imgurlAry[i], path + preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf("/") + 1));
}
}
catch (Exception ex)
{
//return ex.Message;
}
return imgurlAry.Length;
}
}
}