源:
开发asp.net程序,环境VS2005 我在一WebForm中有个button事件,该事件获取 远程网站页面中的<img..>,然后下载该img url所对应的图片到本地.
例如 img是:<img src="http://cn.yimg.com/sp/slideshow/sports/i20060809l132834q0-a.jpg">,当点button后,将把该图下载到本地e:\temp\test\下.问题是:我在开发的模式下,是可以正常运行的,但是发布到ISS上后就报以下异常:
System.Net.WebException: 在 WebClient 请求期间发生异常。 ---> System.UnauthorizedAccessException: 对路径“c:\windows\system32\inetsrv\i20060809l132834q0-a.jpg”的访问被拒绝。 在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) 在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) 在 System.Net.WebClient.DownloadFile(Uri address, String fileName) --- 内部异常堆栈跟踪的结尾 --- 在 System.Net.WebClient.DownloadFile(Uri address, String fileName) 在 System.Net.WebClient.DownloadFile(String address, String fileName) 在 _Default.DownloadFile(String URL) 位置 e:\CCKJ Workspace\TestGetPic2\Default.aspx.cs:行号 67
页面代码:
<asp:TextBox ID="TextBox1" runat="server" Height="46px" Width="730px" Text='aaa<img src="http://cn.yimg.com/sp/slideshow/sports/i20060809l132834q0-a.jpg">bbbaaa<img src="http://cn.yimg.com/sp/slideshow/sports/i20060809l132834q1-a.jpg">111<img src="http://cn.yimg.com/sp/slideshow/sports/i20060809l132834q2-a.jpg">bbb' TextMode="MultiLine"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="下载" /></div>后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Net;
using System.IO;
using System.Text;
using System.Drawing;
using System.Xml;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{
public WebClient client = new WebClient(); protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string text1 = this.TextBox1.Text.Trim();
MatchCollection collection1 = new Regex("<img.+?>", RegexOptions.IgnoreCase).Matches(text1);
string text2 = "";
foreach (Match match1 in collection1)
{
text2 = this.GetHttp(match1.Value);
this.DownloadFile(text2);
} } public string GetHttp(string str)
{
Match match1 = new Regex("http://.+?\"", RegexOptions.IgnoreCase).Match(str);
return match1.Value.Substring(0, match1.Value.Length - 1);
} private void RepalceHttp(string Url)
{
} private void DownloadFile(string URL)
{
int num1 = URL.LastIndexOf('/');
string text1 = URL;
string text2 = URL.Substring(num1 + 1, (URL.Length - num1) - 1);
int num2 = text2.LastIndexOf(".");
string text3 = text2.Substring(0, num2);
string text4 = text2.Substring(num2 + 1, (text2.Length - num2) - 1).ToLower();
string text5 = text3 + "." + text4; string filePath = "E:\\TestTemp\\"+text5; if (File.Exists(filePath))
{
File.Delete(filePath);
}
try
{
this.client.DownloadFile(text1, text2);
Stream str = client.OpenRead(URL);
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
//写入到BYTE数组中,起缓冲作用
while (allmybyte > 0)
{
int m = str.Read(mbyte, startmbyte, allmybyte);
if (m == 0)
break;
startmbyte += m;
allmybyte -= m;
} FileStream fstr = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(mbyte, 0, startmbyte);
str.Close();
fstr.Close();
}
catch (WebException exception1)
{
base.Response.Write(exception1.ToString());
Response.Write("<br>"); }
finally
{
client.Dispose();
}
}
}谢谢!
开发asp.net程序,环境VS2005 我在一WebForm中有个button事件,该事件获取 远程网站页面中的<img..>,然后下载该img url所对应的图片到本地.
例如 img是:<img src="http://cn.yimg.com/sp/slideshow/sports/i20060809l132834q0-a.jpg">,当点button后,将把该图下载到本地e:\temp\test\下.问题是:我在开发的模式下,是可以正常运行的,但是发布到ISS上后就报以下异常:
System.Net.WebException: 在 WebClient 请求期间发生异常。 ---> System.UnauthorizedAccessException: 对路径“c:\windows\system32\inetsrv\i20060809l132834q0-a.jpg”的访问被拒绝。 在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) 在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) 在 System.Net.WebClient.DownloadFile(Uri address, String fileName) --- 内部异常堆栈跟踪的结尾 --- 在 System.Net.WebClient.DownloadFile(Uri address, String fileName) 在 System.Net.WebClient.DownloadFile(String address, String fileName) 在 _Default.DownloadFile(String URL) 位置 e:\CCKJ Workspace\TestGetPic2\Default.aspx.cs:行号 67
页面代码:
<asp:TextBox ID="TextBox1" runat="server" Height="46px" Width="730px" Text='aaa<img src="http://cn.yimg.com/sp/slideshow/sports/i20060809l132834q0-a.jpg">bbbaaa<img src="http://cn.yimg.com/sp/slideshow/sports/i20060809l132834q1-a.jpg">111<img src="http://cn.yimg.com/sp/slideshow/sports/i20060809l132834q2-a.jpg">bbb' TextMode="MultiLine"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="下载" /></div>后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Net;
using System.IO;
using System.Text;
using System.Drawing;
using System.Xml;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{
public WebClient client = new WebClient(); protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string text1 = this.TextBox1.Text.Trim();
MatchCollection collection1 = new Regex("<img.+?>", RegexOptions.IgnoreCase).Matches(text1);
string text2 = "";
foreach (Match match1 in collection1)
{
text2 = this.GetHttp(match1.Value);
this.DownloadFile(text2);
} } public string GetHttp(string str)
{
Match match1 = new Regex("http://.+?\"", RegexOptions.IgnoreCase).Match(str);
return match1.Value.Substring(0, match1.Value.Length - 1);
} private void RepalceHttp(string Url)
{
} private void DownloadFile(string URL)
{
int num1 = URL.LastIndexOf('/');
string text1 = URL;
string text2 = URL.Substring(num1 + 1, (URL.Length - num1) - 1);
int num2 = text2.LastIndexOf(".");
string text3 = text2.Substring(0, num2);
string text4 = text2.Substring(num2 + 1, (text2.Length - num2) - 1).ToLower();
string text5 = text3 + "." + text4; string filePath = "E:\\TestTemp\\"+text5; if (File.Exists(filePath))
{
File.Delete(filePath);
}
try
{
this.client.DownloadFile(text1, text2);
Stream str = client.OpenRead(URL);
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
//写入到BYTE数组中,起缓冲作用
while (allmybyte > 0)
{
int m = str.Read(mbyte, startmbyte, allmybyte);
if (m == 0)
break;
startmbyte += m;
allmybyte -= m;
} FileStream fstr = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(mbyte, 0, startmbyte);
str.Close();
fstr.Close();
}
catch (WebException exception1)
{
base.Response.Write(exception1.ToString());
Response.Write("<br>"); }
finally
{
client.Dispose();
}
}
}谢谢!
1.仔细检查下载文件存放的目录是否是预期的路径
2.设定存放的目录给予ASPNET账户足够的权限