获取远端图片到本地服务器--asp.net实现
2012-03-21 11:22
232 查看
static public string ReplaceRemoteUrl(string strContent)
{
string resultStr=strContent;
string SaveFilePath = "/images/upload/";
if (SaveFilePath.Substring(SaveFilePath.Length-1,1) !="/")
{
SaveFilePath =SaveFilePath +"/";
}
string regex = "((http|https|ftp|rtsp|mms):(//////|////////){1}((//w)+[.]){1,}(net|com|cn|org|cc|tv|[0-9" +
"]{1,3})({1}(gif|jpg|png|bmp]//S*///)((//S)+[.]{1}(gif|jpg|png|bmp)))";
System.Text.RegularExpressions.RegexOptions options = ((System.Text.RegularExpressions.RegexOptions.ExplicitCapture | System.Text.RegularExpressions.RegexOptions.Multiline)
| System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex, options);
System.Text.RegularExpressions.MatchCollection RemoteFileMc = reg.Matches(strContent);
foreach(Match RemoteFileurl in RemoteFileMc)
{
string RemoteFile =RemoteFileurl.ToString();
string webHost=System.Web.HttpContext.Current.Request.ServerVariables["Server_Name"];
if (RemoteFile.IndexOf(webHost)>0)
{
continue;
}
else
{
string[] arrSaveFileName= RemoteFile.Split('.');
string SaveFileType = arrSaveFileName[arrSaveFileName.Length-1];
System.Random rd=new Random(100);
string ranNum =rd.Next(1000).ToString();
string SaveFileName = SaveFilePath+ System.DateTime.Now.ToFileTimeUtc() + ranNum+"."+SaveFileType;
string SaveFileNameFull= System.Web.HttpContext.Current.Request.ApplicationPath +SaveFileName;
SaveRemoteFile(SaveFileNameFull, RemoteFile);
resultStr = strContent.Replace(RemoteFile, SaveFileNameFull);
}
}
return resultStr;
}
static private void SaveRemoteFile(string LocalFileName, string RemoteFileUrl)
{
WebClient wb=new WebClient();
string PhysicalLocalFileName = System.Web.HttpContext.Current.Server.MapPath(LocalFileName);
wb.DownloadFile(RemoteFileUrl, PhysicalLocalFileName);
}
{
string resultStr=strContent;
string SaveFilePath = "/images/upload/";
if (SaveFilePath.Substring(SaveFilePath.Length-1,1) !="/")
{
SaveFilePath =SaveFilePath +"/";
}
string regex = "((http|https|ftp|rtsp|mms):(//////|////////){1}((//w)+[.]){1,}(net|com|cn|org|cc|tv|[0-9" +
"]{1,3})({1}(gif|jpg|png|bmp]//S*///)((//S)+[.]{1}(gif|jpg|png|bmp)))";
System.Text.RegularExpressions.RegexOptions options = ((System.Text.RegularExpressions.RegexOptions.ExplicitCapture | System.Text.RegularExpressions.RegexOptions.Multiline)
| System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex, options);
System.Text.RegularExpressions.MatchCollection RemoteFileMc = reg.Matches(strContent);
foreach(Match RemoteFileurl in RemoteFileMc)
{
string RemoteFile =RemoteFileurl.ToString();
string webHost=System.Web.HttpContext.Current.Request.ServerVariables["Server_Name"];
if (RemoteFile.IndexOf(webHost)>0)
{
continue;
}
else
{
string[] arrSaveFileName= RemoteFile.Split('.');
string SaveFileType = arrSaveFileName[arrSaveFileName.Length-1];
System.Random rd=new Random(100);
string ranNum =rd.Next(1000).ToString();
string SaveFileName = SaveFilePath+ System.DateTime.Now.ToFileTimeUtc() + ranNum+"."+SaveFileType;
string SaveFileNameFull= System.Web.HttpContext.Current.Request.ApplicationPath +SaveFileName;
SaveRemoteFile(SaveFileNameFull, RemoteFile);
resultStr = strContent.Replace(RemoteFile, SaveFileNameFull);
}
}
return resultStr;
}
static private void SaveRemoteFile(string LocalFileName, string RemoteFileUrl)
{
WebClient wb=new WebClient();
string PhysicalLocalFileName = System.Web.HttpContext.Current.Server.MapPath(LocalFileName);
wb.DownloadFile(RemoteFileUrl, PhysicalLocalFileName);
}
相关文章推荐
- asp.net实现服务器文件下载到本地
- 用ASP.NET实现下载远程图片保存到本地的方法 保存抓取远程图片的方法
- 用ASP.NET实现下载远程图片保存到本地的方法 保存抓取远程图片的方法
- Asp.Net使用服务器控件Image/ImageButton显示本地图片的方法
- 总结 asp.net 和 javascript获取本地IP(MAC)和服务器IP(MAC)的方法
- 用ASP实现抓取图片到本地服务器
- asp.net使用服务器控件Image/ImageButton显示本地图片
- asp.net实现服务器文件下载到本地的方法
- 用ASP.NET实现下载远程图片保存到本地的方法 保存抓取远程图片的方法
- 【ASP.NET】10.解决百度富文本编辑器UEditor本地上传图片正常,放到服务器上以后图片上传按钮变灰不能点击的问题
- asp.net实现上传图片显示本地绝对路径图片
- ASP.NET 实现通过URL 获取远程的图片或者文件方法
- asp.net 怎样才能自动获取图的各种参数(宽,高)实现图片打印
- Asp.net 获取服务器信息
- 用asp.net实现将上传的图片变小存入数据库!(暑假里就开始想做的,很兴奋了)
- ASP.NET实现自适应图片大小的弹出窗口
- (转)在ASP.NET 中实现单点登录(利用Cache, 将用户信息保存在服务器缓存中)
- Asp.net 图片异步上传的简单实现
- ASP.NET获取服务器信息
- ASP.NET(C#)实现一次性上传多张图片(多个文件)