asp.net 怎样下载远程图片
不用软件,要实例代码:HttpWebRequestmyReq=(HttpWebRequest)HttpWebRequest.Create("");myReq.Timeou...
不用软件,要实例代码:
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create("
"); myReq.Timeout = 8000; HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); //Response.Write(DateTime.Now + ""); Stream myStream = HttpWResp.GetResponseStream(); //Response.Write(DateTime.Now + ""); System.Drawing.Image oImage = System.Drawing.Image.FromStream(myStream); //Response.Write(DateTime.Now + ""); oImage.Save(Server.MapPath("~/userpic/111112.jpg"), System.Drawing.Imaging.ImageFormat.Gif); 我是用的上面的方法,但 System.Drawing.Image oImage = System.Drawing.Image.FromStream(myStream);这一步要40秒左右,有没有好的方法建议。 展开
"); myReq.Timeout = 8000; HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); //Response.Write(DateTime.Now + ""); Stream myStream = HttpWResp.GetResponseStream(); //Response.Write(DateTime.Now + ""); System.Drawing.Image oImage = System.Drawing.Image.FromStream(myStream); //Response.Write(DateTime.Now + ""); oImage.Save(Server.MapPath("~/userpic/111112.jpg"), System.Drawing.Imaging.ImageFormat.Gif); 我是用的上面的方法,但 System.Drawing.Image oImage = System.Drawing.Image.FromStream(myStream);这一步要40秒左右,有没有好的方法建议。 展开
2个回答
展开全部
#region 下载图片并上传至图片服务器
public string SaveUrlPics( string strHTML, string path)
{
string picserver = new CommonBLL().GetItemValue("PICSERVER");//获取图片服务器地址
string[] imgurlAry = GetImgTag(strHTML);//获取文章中的图片地址
try
{
WebClient wc = new WebClient();
for (int i = 0; i < imgurlAry.Length; i++)
{
//暂时处理避免多次进行添加处理 不处理本机的情况
if(imgurlAry[i].IndexOf(picserver)<0)
{
string preStr = DateTime.Now.ToString("yyyyMMddHHmmssfff");
preStr = preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf("."));//获取图片的属性 生成图片名称
//下载的图片存储在TEMP文件夹中
wc.DownloadFile(imgurlAry[i], HttpContext.Current.Server.MapPath(path) + "/" + preStr);
//把图片上传至图片服务器
preStr = picserver + "/" + upImg(preStr, dropArtType.SelectedValue);
strHTML = strHTML.Replace(imgurlAry[i], preStr);
}
}
}
catch (Exception ex)
{
ShowMessage(ex.Message);
}
return strHTML;
}
#endregion
public string SaveUrlPics( string strHTML, string path)
{
string picserver = new CommonBLL().GetItemValue("PICSERVER");//获取图片服务器地址
string[] imgurlAry = GetImgTag(strHTML);//获取文章中的图片地址
try
{
WebClient wc = new WebClient();
for (int i = 0; i < imgurlAry.Length; i++)
{
//暂时处理避免多次进行添加处理 不处理本机的情况
if(imgurlAry[i].IndexOf(picserver)<0)
{
string preStr = DateTime.Now.ToString("yyyyMMddHHmmssfff");
preStr = preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf("."));//获取图片的属性 生成图片名称
//下载的图片存储在TEMP文件夹中
wc.DownloadFile(imgurlAry[i], HttpContext.Current.Server.MapPath(path) + "/" + preStr);
//把图片上传至图片服务器
preStr = picserver + "/" + upImg(preStr, dropArtType.SelectedValue);
strHTML = strHTML.Replace(imgurlAry[i], preStr);
}
}
}
catch (Exception ex)
{
ShowMessage(ex.Message);
}
return strHTML;
}
#endregion
参考资料: 自己所做项目中使用的代码
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
展开全部
40秒应该是下载图片所需的时间。你试我这个方法看。与你的差不多
public bool DownFile( string httpURL, string localFile )
{
FileStream fs = null ;
WebClient webClient = null;
try
{
string path = new FileInfo( localFile ).DirectoryName ;
webClient = new WebClient();
webClient.Credentials = CredentialCache.DefaultCredentials;
if ( !Directory.Exists( path ) )
{
Directory.CreateDirectory( path ) ;
}
fs = new FileStream( localFile, FileMode.Create, FileAccess.Write, FileShare.Read ) ;
byte[] data = webClient.DownloadData( httpURL )
fs.Write( data, 0, data.Length ) ;
fs.Flush( ) ;
return true ;
}
catch( Exception )
{
return false ;
}
finally
{
if ( null != fs )
{
fs.Close();
fs = null;
}
if ( null != _webClient )
{
webClient.Dispose();
webClient = null;
}
}
}
public bool DownFile( string httpURL, string localFile )
{
FileStream fs = null ;
WebClient webClient = null;
try
{
string path = new FileInfo( localFile ).DirectoryName ;
webClient = new WebClient();
webClient.Credentials = CredentialCache.DefaultCredentials;
if ( !Directory.Exists( path ) )
{
Directory.CreateDirectory( path ) ;
}
fs = new FileStream( localFile, FileMode.Create, FileAccess.Write, FileShare.Read ) ;
byte[] data = webClient.DownloadData( httpURL )
fs.Write( data, 0, data.Length ) ;
fs.Flush( ) ;
return true ;
}
catch( Exception )
{
return false ;
}
finally
{
if ( null != fs )
{
fs.Close();
fs = null;
}
if ( null != _webClient )
{
webClient.Dispose();
webClient = null;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询