求助C#文件下载的问题
代码如下:
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
private static WebClient client = new WebClient();
private static StreamWriter writer = null;
private static Thread thThread;
private static string url;
private static string app;
private static string website;
private static string filename;
private static void StartDownload()
{
string URL = app;
int n = URL.LastIndexOf("/");
string URLAddress = URL.Substring(0, n);
string fileName = URL.Substring(n + 1, URL.Length - n - 1);
string Dir = Environment.CurrentDirectory;
string Path = Dir.ToString() + "\\" + fileName;
try
{
WebRequest myre = WebRequest.Create(URLAddress);
}
catch (WebException exp)
{
Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", website);
}
try
{
//statusStrip1.Text = "开始下载文件...";
client.DownloadFile(URLAddress, fileName);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
//statusStrip1.Text = "正在接收数据...";
FileStream fstr = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write);
writer = new StreamWriter(fstr);
while (reader.Peek() >= 0)
{
char[] c = new char[10240];
reader.Read(c,0, c.Length);
writer.Write(c);
}
writer.Flush();
str.Close();
fstr.Close();
//statusStrip1.Text = "下载完毕!";
MessageBox.Show("下载完毕");
}
catch (WebException exp)
{
MessageBox.Show(exp.Message, "Error");
//statusStrip1.Text = "";
}
url = "\\JDK6.url";
app = "http://www.java.net/download/jdk6/6u10/promoted/b28/binaries/jdk-6u10-rc-bin-b28-windows-i586-p-21_jul_2008.exe";
website = "http://java.sun.com/";
filename = "JDK6.exe";
StartDownload(); 展开
直接用WebClient下载不就行了吗,干嘛还要定义一个WebRequest如下
using System.Net;
WebClient web = new WebClient();
private void button1_Click(object sender, EventArgs e)//下载
{
string app = "http://www.java.net/download/jdk6/6u10/promoted/b28/binaries/jdk-6u10-rc-bin-b28-windows-i586-p-21_jul_2008.exe";
string filename = "D:\\JDK6.exe";
web.DownloadFileCompleted += new AsyncCompletedEventHandler(web_DownloadFileCompleted);
web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(web_DownloadProgressChanged);
web.DownloadFileAsync(new Uri(app), filename);
}
private void button2_Click(object sender, EventArgs e)//取消
{
web.CancelAsync();
}
void web_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
label1.Text = string.Format("开始下载文件... 已下载:{0}Mb 剩余:{1}Mb 已完成:{2}%",
e.BytesReceived / 1024 / 1024,
e.TotalBytesToReceive / 1024 / 1024,
e.ProgressPercentage.ToString("N2"));
}
void web_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
label1.Text = "下载取消";
else
label1.Text = "下载完毕";
}
我感觉这个大小,像个 html 的错误页面。
如果是错误页面,打开看看就知道哪里错了~
--------
string URLAddress = URL.Substring(0, n);
这时, URLAddress 已经被截断了
变成:
http://www.java.net/download/jdk6/6u10/promoted/b28/binaries/
了
这个 url 下载下来正好是19.9k
你直接
WebRequest myre = WebRequest.Create(app);
应该就好了
如果不知道链接就先分析html文件获得链接,然后下载。
文件下载的代码:
WebClient wc = new WebClient(); //创建网络通信实例
byte[] by = new byte[32]; //接收数据的数组
FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write); //创建文件
BinaryWriter bw = new BinaryWriter(fs);
while ((by = wc.DownloadData(dz)) != null) //写文件
{
bw.Write(by, 0, by.Length);
}
FileInfo f = new FileInfo(filepath);
f.MoveTo(filepath.Remove(filepath.LastIndexOf(".temp")) + ".swf"); //更名
bw.Close();
fs.Close(); //关闭数据流
ric.AppendText(filepath + "下载完毕");
client.DownloadFile(URL, fileName);
//DownloadFile方法执行完后,文件已经下载到本地了,后面的代码是另一种下载方法,可以注释掉.
//文件大的话DownloadFile方法会卡死,有个异步下载方法DownloadFileAsync