判断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. activiti自定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义

    注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建        (2)创建流程模型:activiti自定义流程之Spring ...

  2. C#跟踪和调试程序-Debug类使用

    摘要: 怎样在 Visual C# .NET 中跟踪和调试?当程序运行时,您可以使用 Debug 类的方法来生成消息,以帮助您监视程序执行顺序.检测故障或提供性能度量信息.默认情况下,Debug 类产 ...

  3. 黄聪:解决丢失api-ms-win-crt-runtime-|1-1-0.dll的问题:vc_redist.x64

    解决无法启动程序,因计算机中丢失api-ms-win-crt-runtime-|1-1-0.dll的问题 安装:Microsoft Visual C++ 2015 RC Redistributable ...

  4. 多线程同步 wait notify

    package test; public class Test implements Runnable{ public static int j =0; @Override public void r ...

  5. 蜘蛛纸牌存档修改——python3.4.3

    #encoding:utf-8 import struct myfile = open("D:\\Backup\\我的文档\\spider.sav","rb+" ...

  6. webuploader在同一个页面支持多个按钮实例

    之前在时候用到webuploader ,起初是支持单实例,后来要求支持多实例. webuploder API网址,如果不懂我说的可以去查看http://fex.baidu.com/webuploade ...

  7. vim常用命令笔记(转载)

    添加多行注释:   1. 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区块)模式;   2. 在行首使用上下键选择需要注释的多行;   3. 按下键盘(大写)“I”键,进入插入模式 ...

  8. java多线程之从任务中获取返回值

    package wzh.test; import java.util.ArrayList; import java.util.concurrent.Callable; import java.util ...

  9. 用block响应button的点击事件

    1.继承UIButton : 2.在自己定义的button类中的方法 addTarget:(id)target action:(SEL)action forControlEvents:(UIContr ...

  10. KVC , KVO , KVB

    来源:http://www.cnblogs.com/jay-dong/archive/2012/12/13/2815778.html 熟悉oc语法的同学也许都会懂得这么一点:在oc中,类的成员变量或是 ...