我是采用普通的類繼承的方式來實現這個功能的由於這個功能剛剛出來,沒有經過完善,因此不提供源碼下載,僅僅貼出部分核心代碼,以供大家參考:
1.基類代碼FileSystem,這個類是一個最基礎的Web文件系統對象,這個對象實現的功能就是將文件直接通過Response輸出,不進行任何保存操作,它的靜態函數Create用來根據當前訪問的文件路徑和系統配置判斷采用哪一個文件系統來處理。
FileSystem.cs
1using System;
2using System.IO;
3using System.Web;
4namespace Step1.WebFileSystem
5{
6 public class FileSystem
7 {
8 protected string path, physicsPath;
9 protected Handler handle;
10 protected HttpContext httpContext = null;
11 protected Stream stream=null;
12 public FileSystem(string path, Handler handle)
13 {
14 httpContext = HttpContext.Current;
15 this.path = path;
16 this.physicsPath = httpContext.Server.MapPath(path);
17 this.handle = handle;
18 }
19 public virtual bool Exists()
20 {
21 return false;
22 }
23 public virtual DateTime GetLastWriteTime()
24 {
25 return DateTime.Now;
26 }
27 public virtual Stream GetWriter()
28 {
29 stream = httpContext.Response.OutputStream;
30 if (this.handle.UseGzip)
31 {
32 return (Stream)(new GZipOutputStream(stream));
33 }
34 else
35 {
36 return stream;
37 }
38 }
39 public virtual Stream GetReader()
40 {
41 return null;
42 }
43 public virtual void TransmitFile()
44 {
45 return;
46 }
47 public virtual void Close()
48 {
49 if (stream != null)
50 {
51 stream.Close();
52 stream=null;
53 }
54 }
55 public virtual void Dispose()
56 {
57 Close();
58 }
59 public void TransmitStream(bool unZip)
60 {
61 Stream reader = unZip ? new GZipInputStream(this.GetReader()) : this.GetReader();
62 byte[] buffer = new byte[1024];
63 int p;
64 while ((p = reader.Read(buffer, 0, 1024)) > 0)
65 {
66 httpContext.Response.OutputStream.Write(buffer, 0, p);
67 httpContext.Response.Flush();
68 }
69 this.Close();
70 }
71 public void ResponseFile()
72 {
73 HttpRequest request = httpContext.Request;
74 string acceptEncoding = request.Headers["Accept-Encoding"];
75 if (this.handle.UseGzip && acceptEncoding != null && acceptEncoding.IndexOf("gzip") >= 0)
76 {//返回gzip格式
77 httpContext.Response.AddHeader("Content-Encoding", "gzip");
78 this.TransmitFile();
79 }
80 else
81 {//返回明文格式
82 if (this.handle.UseGzip)
83 {
84 this.TransmitStream(true);
85 }
86 else
87 {
88 this.TransmitFile();
89 }
90 }
91 }
92 protected void CreateFolder(string path, bool createCurrent)
93 {
94 string folder = Path.GetDirectoryName(path);
95 if (!Directory.Exists(folder))
96 {
97 CreateFolder(folder, true);
98 }
99 if (createCurrent)
100 {
101 Directory.CreateDirectory(path);
102 }
103 }
104 public static FileSystem Create(string path)
105 {
106 Configuration config = Configuration.Instance();
107 if (config != null)
108 {
109 for(int i = 0; i < config.handlers.Length; i++)
110 {
111 if (config.handlers[i].UrlRegex.IsMatch(path))
112 {
113 switch(config.handlers[i].HandlerType)
114 {
115 case "FFS":
116 return new FileSystemFFS(path, config.handlers[i]);
117 case "OS":
118 return new FileSystemOS(path, config.handlers[i]);
119 default:
120 return new FileSystem(path, config.handlers[i]);
121 }
122 }
123 }
124
125 }
126 return new FileSystem(path,new Handler());
127 }
128 }
129}
130