asp.net儲存遠程圖片的代碼

來源:互聯網
上載者:User

注意:並沒有實現CSS中的圖片採集,且圖片的正則還有待完善。 複製代碼 代碼如下:using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

//引入空間
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;

/// <summary>
/// 採集
/// </summary>
public class caiji
{
public caiji()
{
//
// TODO: 在此處添加建構函式邏輯
//
}

/// <summary>
/// 要採集的網頁的串連地址
/// </summary>
/// <param name="url">url</param>
/// <returns></returns>
public static string caijiByUrl(string url,string chargest,string path)
{
string str = GetSourceTextByUrl(url,chargest);

ArrayList lib = new ArrayList();

int i = 0;
//根據url取得網站網域名稱
Uri uri = new Uri(url);

//Scheme或者協議,一般為http,Host為取得網域名稱
string baseurl = uri.Scheme + "://" + uri.Host + "/";

//提取出url,包括src等資訊
//\S匹配任何非空白字元
Regex g = new Regex(@"(src=(""|\')\S+\.(gif|jpg|png|bmp)(""|\'))", RegexOptions.Multiline | RegexOptions.IgnoreCase);

MatchCollection m = g.Matches(str);

foreach (Match math in m)
{
//已經提取到圖片的路徑了,但還需要分絕對路徑,相對路徑,以及尾碼名是否為圖片,因為可能為.asp,.aspx這些,比如驗證碼圖片
string imgUrl = math.Groups[0].Value.ToLower();//轉成小寫,=號之間可能有不定的空格

//去除src與單引號,雙引號
imgUrl = imgUrl.Replace("src","");
imgUrl = imgUrl.Replace("\"","");
imgUrl = imgUrl.Replace("'","");
imgUrl = imgUrl.Replace("=","");
imgUrl = imgUrl.Trim();

//路徑處理
if (imgUrl.Substring(0, 4) != "http")
{
//需要判斷是否是絕對路徑還是相對路徑
if (imgUrl.Substring(0, 1) == "/")
{
imgUrl = baseurl + imgUrl;
}
else
{
imgUrl = url.Substring(0,url.LastIndexOf("/") + 1) + imgUrl;
}
}

//判斷元素是否已經存在,-1為不存在
if (lib.IndexOf(imgUrl) == -1)
{
lib.Add(imgUrl);
}
}

string str_ = string.Empty;
WebClient client = new WebClient();

for (int j = 0; j < lib.Count; j++)
{
string savepath = path + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Minute + DateTime.Now.Second + j + lib[j].ToString().Substring((lib[j].ToString().Length) -4,4);
try
{
client.DownloadFile(new Uri(lib[j].ToString()), savepath);
str_ += lib[j].ToString() + "<br /> 儲存路徑為:" + savepath + "<br /><br />";
}
catch (Exception e)
{
str_ += e.Message;
}

}

return str_;
}

public static string GetSourceTextByUrl(string url,string chargest)
{
WebRequest request = WebRequest.Create(url);
request.Timeout = 20000;//20秒逾時
WebResponse response = request.GetResponse();

Stream resStream = response.GetResponseStream();
StreamReader sr = new StreamReader(resStream,Encoding.GetEncoding(chargest));
return sr.ReadToEnd();
}
}

使用:比如我是儲存到upload檔案夾中的: 複製代碼 代碼如下:string path = Server.MapPath("~/upload/");
Response.Write(caiji.caijiByUrl(http://www.jb51.net, "utf-8", path));

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.