腾讯文件下载|文件下载的一个类

更新时间:2017-09-19 来源:软件评测 点击:

【www.hzclsc.cn--软件评测】


            
             
              
               
               
             
            
            



            ///
/// 下载文件
///

public class BDDownLoadFile
{
private string Url; //要下载的文件URL地址
private string SavePath; //要保存的文件的目录
private string errMsg; //保存错误信息
private string RegValue = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
private string SaveFile; //生成的文件

public BDDownLoadFile(string url,string path)
{
Url = url;
SavePath = path;
this.SaveFile = GetFileName();
}

///
/// 返回错误信息
///

public string ErrorMessage
{
get
{
return this.errMsg;
}
}

public string GetSaveFile
{
get
{
return this.SaveFile;
}
}
public bool DownLoadFile()
{
bool result = true;
if( !CheckUrl(this.Url) )
{
this.errMsg = "网址不合法!";
return false;
}
WebClient objWC = new WebClient();
objWC.Credentials = CredentialCache.DefaultCredentials;
try
{
byte[] tmpData = objWC.DownloadData(this.Url);
if(tmpData.Length > 0)
{
FileStream objFS = new FileStream(this.SaveFile,FileMode.Create);
objFS.Write(tmpData,0,(int)tmpData.Length); //向文件写入数据
objFS.Close();
this.errMsg = "有数据!";
}
else
{
result = false;
this.errMsg = "没有接收到任何数据!";
}
}
catch(System.Net.WebException e)
{
this.errMsg += "下载数据时发生错误!" + e.Message;
return false;
}
catch(System.UriFormatException e)
{
this.errMsg += "访问的网址无效!" + e.Message;
return false;
}
catch(Exception e)
{
this.errMsg = "错误信息:."+e.Message;
result = false;
}
finally
{
objWC.Dispose();
}
return result;
}

///
/// 检查网址是否合法
///

/// 要检查的网址
///
private bool CheckUrl(string chkUrl)
{
Regex reg = new Regex(RegValue);
Match match = reg.Match(chkUrl);
return match.Success;
}

///
/// 取得网址是否有文件,如果有文件,则处理,若没有,则返回.html
///

///
///
private string GetFileType(string chkUrl)
{
if(chkUrl.LastIndexOf("/") == (chkUrl.Length-1)) //没有具体文件
return "html";
int j = 0;
for(int i = 0; i < chkUrl.Length; i++)
{
if(chkUrl.IndexOf("/",i)>-1)
{
i = chkUrl.IndexOf("/",i);
j++;
}
}
if( j < 3)
return "html";
//取得"/"后的字符,然后得出文件类型
string end = chkUrl.Substring(chkUrl.LastIndexOf(".")+1);
switch(end)
{
case "asp":
case "aspx":
case "jsp":
case "php":
return "html";
default:
return end;
}
}

private string GetFileName()
{
string fileName = this.SavePath + "\\" + System.DateTime.Now.Year.ToString()+System.DateTime.Now.Month.ToString()+System.DateTime.Now.Day.ToString()+System.DateTime.Now.Minute.ToString()+System.DateTime.Now.Second.ToString()+Common.MakeRandom(4).ToString()+"."+GetFileType(this.Url);
for(;File.Exists(fileName);)
{
fileName = this.SavePath + "\\" + System.DateTime.Now.Year.ToString()+System.DateTime.Now.Month.ToString()+System.DateTime.Now.Day.ToString()+System.DateTime.Now.Minute.ToString()+System.DateTime.Now.Second.ToString()+Common.MakeRandom(4).ToString()+"\\"+GetFileType(this.Url);
}
return fileName;
}
} //BDDownLoadFile end

本文来源:http://www.hzclsc.cn/ruanjianzixun/2062.html

为您推荐

[rainway什么意思]Rainway软件下载地址 rainway串流switch软件在哪下载

您的位置:首页 → 单机游戏 → 游戏资讯 → Rainway软件下载地址 rainway串流switch软件在哪下载 Rainway软件下载地址 rainway串流switch软件在哪下载时间:20游戏资讯

2020-12-16 11:04:57  

苹果电脑安全性偏好设置_苹果电脑安全漏洞是什么 苹果电脑安全漏洞无需密码解锁解决办法

就在近日,有用户发现苹果电脑出现了一个重大的安全漏洞,不需要输入密码即可进入系统,但是很多用户都还不知道苹果电脑安全漏洞是什么,好奇的用户马上点击查看苹果电脑安全漏洞无需密码解锁解决办法来防范于未然教你一招

2020-12-10 23:05:05   电脑安全软件   电脑安全证书过期怎么办   顺丰寄电脑安全吗  

【nero】Nelo游戏配置要求高吗 Nelo推荐配置一览

Nelo游戏配置要求高吗?Nelo是一款由虚幻4打造的超快节奏的动作游戏,玩家可以使用四种武器形成各式各样的攻击方式,本文为大家带来Nelo售价和配置要求介绍,下面就一起来看看Nelo推荐配置一览吧。游戏资讯

2020-12-10 11:04:41   nero8中文破解版   nero刻录软件   nero10序列号  

抢红包神器抢最大的_抢红包怎么抢最大的 抢红包助手下载哪个好

时近年末,相信大家的“节目单”上除了与好友聚会叙旧、与客户更加频繁地应酬之外,还有一项必不可少的活动,那就是抢红包。记得木心有首现代诗中是这样写的,“从前的日色变得慢,车马、信件都很慢 ”的软件评测

2020-12-09 17:04:22   抢红包怎么能抢最大的   抢红包最大金额软件  

[wegame修复游戏在哪里]腾讯wegame怎么修复游戏 wegame平台修复游戏教程

您的位置:首页 → 资讯 → 游戏软件 → 腾讯wegame怎么修复游戏 wegame平台修复游戏教程 腾讯wegame怎么修复游戏 wegame平台修复游戏教程时间:2017 10 20 16:00游戏软件

2020-12-04 07:04:50   wegame游戏修复在哪里   wegame如何修复游戏