判断URL文件是不是在于在。

public class FileHandler
{
public static bool DownLoadFile(string path, string fileName)
{
bool result = false;
if (!string.IsNullOrEmpty(path))
{
if (path.StartsWith("http") || path.StartsWith("https"))
{
result = InternetDownload(path, fileName);
}
else
{
result = LocalDownload(path, fileName);
}
}
return result;
} private static bool LocalDownload(string path, string fileName)
{
bool result = false;
//Physical path "D:\InvoicePDF\"
string filePath = path + fileName;
if (File.Exists(filePath))
{
result = true;
}
else
{
try
{
//Relative path "~/InvoicePDF/"
filePath = HttpContext.Current.Server.MapPath(filePath);
if (File.Exists(filePath))
{
result = true;
}
}
catch
{
result = false;
}
}
if (result)
{
try
{
//string filePath = HttpContext.Current.Server.MapPath(path + fileName);
FileInfo fileInfo = new FileInfo(filePath);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
HttpContext.Current.Response.WriteFile(fileInfo.FullName);
HttpContext.Current.Response.Flush();
}
catch (Exception e)
{
}
finally
{
HttpContext.Current.Response.End();
}
}
return result;
} private static bool InternetDownload(string path, string fileName)
{
string url = path + fileName;
if (UrlIsExist(url))
{
ResponseRedirect.Redirect(HttpContext.Current.Response, url, "_blank", "");
//System.Web.HttpContext.Current.Response.Redirect(url,false);
}
return false;//Never use default behavior
} private static bool UrlIsExist(string url)
{
System.Uri u = null;
try
{
u = new Uri(url);
}
catch { return false; }
bool isExist = false;
System.Net.HttpWebRequest r = System.Net.HttpWebRequest.Create(u) as System.Net.HttpWebRequest;
r.Method = "HEAD";
try
{
System.Net.HttpWebResponse s = r.GetResponse() as System.Net.HttpWebResponse;
if (s.StatusCode == System.Net.HttpStatusCode.OK)
{
isExist = true;
}
}
catch (System.Net.WebException x)
{
try
{
isExist = ((x.Response as System.Net.HttpWebResponse).StatusCode != System.Net.HttpStatusCode.NotFound);
}
catch { isExist = (x.Status == System.Net.WebExceptionStatus.Success); }
}
return isExist;
}
}

DownLoadFile - FileHandler的更多相关文章

  1. WebClient.DownloadFile(线程机制,异步下载文件)

    线程机制(避免卡屏),异步下载文件. 我做网站的监控,WebClient.DownloadFile这个方法是我经常用到的,必要的时候肯定是要从网上下载些什么(WebRequest 也可以下载网络文件, ...

  2. Tomcat报java.lang.ClassNotFoundException: 1catalina.org.apache.juli.FileHandler

    最近在生产环境部署Tomcat的时候,在启动的时候,在控制台报"java.lang.ClassNotFoundException: 1catalina.org.apache.juli.Fil ...

  3. 多种下载文件方式 Response.BinaryWrite(byte[] DocContent);Response.WriteFile(System.IO.FileInfo DownloadFile .FullName);Response.Write(string html2Excel);

    通过html给xls赋值,并下载xls文件 一.this.Response.Write(sw.ToString());System.IO.StringWriter sw = new System.IO ...

  4. 警惕使用WebClient.DownloadFile(string uri,string filePath)方法

    原文:警惕使用WebClient.DownloadFile(string uri,string filePath)方法 WebClient.DownloadFile(string uri,string ...

  5. 下载文件downloadFile

    public static void downLoadFile(InputStream inStream, String fileName) { if (StringUtils.isBlank(fil ...

  6. wx.downloadFile问题

    http://www.wxapp-union.com/forum.php?mod=viewthread&tid=2988(copy) 这个问题,研究者甚少,以至于相关问题直到今天,仍然属于未知 ...

  7. IIS下载,WebClient().DownloadFile下载

    new System.Net.WebClient().DownloadFile(serverPath, localPath); 有时候使用的时候,文件下载不下来.需要设置一下服务器上IIS的权限

  8. TFS二次开发05——下载文件(DownloadFile)

    前面介绍了怎样读取TFS上目录和文件的信息,怎么建立服务器和本地的映射(Mapping). 本节介绍怎样把TFS服务器上的文件下载到本地. 下载文件可以有两种方式: using Microsoft.T ...

  9. Tomcat报错java.lang.ClassNotFoundException: 2localhost.org.apache.juli.FileHandler

    Can't load log handler "1catalina.org.apache.juli.FileHandler" java.lang.ClassNotFoundExce ...

随机推荐

  1. 在 Perl 中使用 Getopt::Long 模块来接收用户命令行参数

    我们在linux常常用到一个程序需要加入参数,现在了解一下 perl 中的有关控制参数的模块 Getopt::Long ,比直接使用 @ARGV 的数组强大多了.我想大家知道在 Linux 中有的参数 ...

  2. [linux basic 基础]----同步互斥量

    互斥量,运行程序元锁住某个对象,使得每次只能有一个线程访问它:为了控制对关键代码的访问,必须在进入这段代码之前锁住一个互斥量,然后在完成操作之后解锁它 :基本函数与用于信号量的函数非常相似#inclu ...

  3. 【SQL Server】系统学习之三:逻辑查询处理阶段-六段式

    一.From阶段 针对连接说明: 1.笛卡尔积 2.on筛选器 插播:unknown=not unknuwn 缺失的值: 筛选器(on where having)把unknown当做FALSE处理,排 ...

  4. DBA_Oracle Erp版本升级12.1.1到R12.1.3(案例)

    20150506 Created By BaoXinjian

  5. UVA 11137 Ingenuous Cubrency(dp)

    Ingenuous Cubrency 又是dp问题,我又想了2 30分钟,一点思路也没有,最后又是看的题解,哎,为什么我做dp的题这么烂啊! [题目链接]Ingenuous Cubrency [题目类 ...

  6. python入门,猜数

    #this is a sample guess program import random guesses_made =0 name = raw_input('Hello! whats your na ...

  7. MySQL的时间进位问题

    mysql更新到5.6.4 之后 , 新增了一个叫factional seconds的特性 , 可以记录时间的毫秒值. 但是目前的数据库是不记录毫秒值的 , 所以会产生一个java中时间的Millis ...

  8. 金蝶BOS

    1, 金蝶BOS 金蝶BOS是一个开放的集成与应用平台,是金蝶企业管理软件解决方案.合作伙伴解决方案以及客户定制应用的技术平台.能够为企业灵活而迅速的设计.构建.实施和执行一套随需应变的企业管理软件系 ...

  9. Python深入03 对象的属性

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python一切皆对象(object),每个对象都可能有多个属性(attribut ...

  10. 庭审精彩语录整理 z

    公诉人:用百度搜索淫秽关键字+快播,搜索结果得出超过4200万结果,可见快播在传播淫秽视频方面的巨大影响.王欣:这个没有任何意义,您可以用百度搜索淫秽关键字+QQ看有多少结果. 新浪科技讯 1月8日下 ...