自定义一个继承自httphandle的类
1
using System;2
using System.Web;3
using System.IO;4

5
namespace WebTj.Jhttphandle6


{7

/**//// <summary>8
/// Httphandles 的摘要说明。9
/// </summary>10
public class Httphandles:IHttpHandler11

{12
public Httphandles()13

{14
//15
// TODO: 在此处添加构造函数逻辑16
//17
}18
public void ProcessRequest(System.Web.HttpContext context)19

{20
FileInfo file = new System.IO.FileInfo(context.Request.PhysicalPath);21
if(context.Request.UrlReferrer !=null && context.Request.UrlReferrer.Host == "200.1.2.84")//判断是否是本地引用,如果是则返回给客户端正确的图片,这里的判断就是用到了前面所述的http请求中所记路的参考页信息22

{23
context.Response.Expires = 0;//设置客户端缓冲中文件过期时间为0,即立即过期。24
context.Response.Clear();//清空服务器端为此会话开辟的输出缓存25
context.Response.ContentType =GetContentType(context.Request.PhysicalPath); //获得文件类型26
context.Response.WriteFile(context.Request.PhysicalPath);//将请求文件写入到服务器端为此会话开辟的输出缓存中27
context.Response.End();//将服务器端为此会话开辟的输出缓存中的信息传送到客户端28
}29
else //如果不是本地引用,则属于盗链引用,返回给客户端错误的图片30

{31
context.Response.Expires = 0; //设置客户端缓冲中文件过期时间为0,即立即过期。32
context.Response.Clear();//清空服务器端为此会话开辟的输出缓存33
context.Response.ContentType = GetContentType("/Images/loading.gif"); //获得文件类型34
context.Response.WriteFile("/Images/loading.gif");//将特殊的报告错误的图片文件写入到服务器端为此会话开辟的输出缓存中35
context.Response.End();//将服务器端为此会话开辟的输出缓存中的信息传送到客户端36
}37
38

39
}40
public bool IsReusable41

{42
get43

{44
return true;45
}46
}47

48
protected static string GetContentType(string strFileName)49

{50
string strExtendName = strFileName.Substring(strFileName.LastIndexOf('.'));51
if (strExtendName=="")52

{53
return "";54
}55
string strRet = "";56
switch (strExtendName)57

{ 58
case ".*": strRet = "application/octet-stream";break;59
case ".001": strRet = "application/x-001";break; 60
case ".301": strRet = "application/x-301";break; 61
case ".323": strRet = "text/h323" ;break;62
case ".906": strRet = "application/x-906" ;break;63
case ".907": strRet = "drawing/907" ;break;64
case ".a11": strRet = "application/x-a11";break; 65
case ".acp": strRet = "audio/x-mei-aac" ;break;66
case ".ai": strRet = "application/postscript" ;break;67
case ".aif": strRet = "audio/aiff" ;break;68
case ".aifc": strRet = "audio/aiff" ;break;69
case ".aiff": strRet = "audio/aiff" ;break;70
case ".anv": strRet = "application/x-anv" ;break;71
case ".asa": strRet = "text/asa";break; 72
case ".asf": strRet = "video/x-ms-asf";break; 73
case ".asp": strRet = "text/asp";break; 74
case ".asx": strRet = "video/x-ms-asf";break; 75
case ".au": strRet = "audio/basic";break; 76
case ".avi": strRet = "video/avi";break; 77
case ".awf": strRet = "application/vnd.adobe.workflow";break; 78
case ".biz": strRet = "text/xml";break; 79
case ".bmp": strRet = "application/x-bmp";break; 80
case ".bot": strRet = "application/x-bot";break; 81
case ".c4t": strRet = "application/x-c4t";break; 82
case ".c90": strRet = "application/x-c90";break; 83
case ".cal": strRet = "application/x-cals";break; 84
case ".cat": strRet = "application/vnd.ms-pki.seccat";break; 85
case ".cdf": strRet = "application/x-netcdf";break; 86
case ".cdr": strRet = "application/x-cdr";break; 87
case ".cel": strRet = "application/x-cel";break; 88
case ".cer": strRet = "application/x-x509-ca-cert";break; 89
case ".cg4": strRet = "application/x-g4";break; 90
case ".cgm": strRet = "application/x-cgm";break; 91
case ".cit": strRet = "application/x-cit";break; 92
case ".class": strRet = "java/*";break; 93
case ".cml": strRet = "text/xml";break; 94
case ".cmp": strRet = "application/x-cmp";break; 95
case ".cmx": strRet = "application/x-cmx";break; 96
case ".cot": strRet = "application/x-cot";break; 97
case ".crl": strRet = "application/pkix-crl";break; 98
case ".crt": strRet = "application/x-x509-ca-cert";break; 99
case ".csi": strRet = "application/x-csi";break; 100
case ".css": strRet = "text/css";break; 101
case ".cut": strRet = "application/x-cut";break; 102
case ".dbf": strRet = "application/x-dbf";break; 103
case ".dbm": strRet = "application/x-dbm";break; 104
case ".dbx": strRet = "application/x-dbx";break; 105
case ".dcd": strRet = "text/xml";break; 106
case ".dcx": strRet = "application/x-dcx";break; 107
case ".der": strRet = "application/x-x509-ca-cert";break; 108
case ".dgn": strRet = "application/x-dgn";break; 109
case ".dib": strRet = "application/x-dib";break; 110
case ".dll": strRet = "application/x-msdownload";break; 111
case ".doc": strRet = "application/msword";break; 112
case ".dot": strRet = "application/msword";break; 113
case ".drw": strRet = "application/x-drw";break; 114
case ".dtd": strRet = "text/xml";break; 115
//case ".dwf": strRet = "Model/vnd.dwf";break; 116
case ".dwf": strRet = "application/x-dwf";break; 117
case ".dwg": strRet = "application/x-dwg";break; 118
case ".dxb": strRet = "application/x-dxb";break; 119
case ".dxf": strRet = "application/x-dxf";break; 120
case ".edn": strRet = "application/vnd.adobe.edn";break; 121
case ".emf": strRet = "application/x-emf";break; 122
case ".eml": strRet = "message/rfc822";break; 123
case ".ent": strRet = "text/xml";break; 124
case ".epi": strRet = "application/x-epi";break; 125
case ".eps": strRet = "application/x-ps";break; 126
//case ".eps": strRet = "application/postscript";break; 127
case ".etd": strRet = "application/x-ebx";break; 128
case ".exe": strRet = "application/x-msdownload";break; 129
case ".fax": strRet = "image/fax";break; 130
case ".fdf": strRet = "application/vnd.fdf";break; 131
case ".fif": strRet = "application/fractals";break; 132
case ".fo": strRet = "text/xml";break; 133
case ".frm": strRet = "application/x-frm";break; 134
case ".g4": strRet = "application/x-g4";break; 135
case ".gbr": strRet = "application/x-gbr";break; 136
case ".gcd": strRet = "application/x-gcd";break; 137
case ".gif": strRet = "image/gif";break; 138
case ".gl2": strRet = "application/x-gl2";break; 139
case ".gp4": strRet = "application/x-gp4";break; 140
case ".hgl": strRet = "application/x-hgl";break; 141
case ".hmr": strRet = "application/x-hmr";break; 142
case ".hpg": strRet = "application/x-hpgl";break; 143
case ".hpl": strRet = "application/x-hpl";break; 144
case ".hqx": strRet = "application/mac-binhex40";break; 145
case ".hrf": strRet = "application/x-hrf";break; 146
case ".hta": strRet = "application/hta";break; 147
case ".htc": strRet = "text/x-component";break; 148
case ".htm": strRet = "text/html";break; 149
case ".html": strRet = "text/html";break; 150
case ".htt": strRet = "text/webviewhtml";break; 151
case ".htx": strRet = "text/html";break; 152
case ".icb": strRet = "application/x-icb";break; 153
case ".ico": strRet = "image/x-icon";break; 154
//case ".ico": strRet = "application/x-ico";break; 155
case ".iff": strRet = "application/x-iff";break; 156
case ".ig4": strRet = "application/x-g4";break; 157
case ".igs": strRet = "application/x-igs";break; 158
case ".iii": strRet = "application/x-iphone";break; 159
case ".img": strRet = "application/x-img";break; 160
case ".ins": strRet = "application/x-internet-signup";break;161
case ".isp": strRet = "application/x-internet-signup";break; 162
case ".IVF": strRet = "video/x-ivf";break; 163
case ".java": strRet = "java/*";break; 164
case ".jfif": strRet = "image/jpeg";break; 165
case ".jpe": strRet = "image/jpeg";break; 166
//case ".jpe": strRet = "application/x-jpe";break; 167
case ".jpeg": strRet = "image/jpeg";break; 168
case ".jpg": strRet = "image/jpeg";break; 169
//case ".jpg": strRet = "application/x-jpg";break; 170
case ".js": strRet = "application/x-javascript";break; 171
case ".jsp": strRet = "text/html";break; 172
case ".la1": strRet = "audio/x-liquid-file";break; 173
case ".lar": strRet = "application/x-laplayer-reg";break; 174
case ".latex": strRet = "application/x-latex";break; 175
case ".lavs": strRet = "audio/x-liquid-secure";break; 176
case ".lbm": strRet = "application/x-lbm";break; 177
case ".lmsff": strRet = "audio/x-la-lms";break; 178
case ".ls": strRet = "application/x-javascript";break; 179
case ".ltr": strRet = "application/x-ltr";break; 180
case ".m1v": strRet = "video/x-mpeg";break; 181
case ".m2v": strRet = "video/x-mpeg";break; 182
case ".m3u": strRet = "audio/mpegurl";break; 183
case ".m4e": strRet = "video/mpeg4";break; 184
case ".mac": strRet = "application/x-mac";break; 185
case ".man": strRet = "application/x-troff-man";break; 186
case ".math": strRet = "text/xml";break; 187
case ".mdb": strRet = "application/msaccess";break; 188
//case ".mdb": strRet = "application/x-mdb";break; 189
case ".mfp": strRet = "application/x-shockwave-flash";break; 190
case ".mht": strRet = "message/rfc822";break; 191
case ".mhtml": strRet = "message/rfc822";break; 192
case ".mi": strRet = "application/x-mi";break; 193
case ".mid": strRet = "audio/mid";break; 194
case ".midi": strRet = "audio/mid";break; 195
case ".mil": strRet = "application/x-mil";break; 196
case ".mml": strRet = "text/xml";break; 197
case ".mnd": strRet = "audio/x-musicnet-download";break; 198
case ".mns": strRet = "audio/x-musicnet-stream";break; 199
case ".mocha": strRet = "application/x-javascript";break; 200
case ".movie": strRet = "video/x-sgi-movie";break; 201
case ".mp1": strRet = "audio/mp1";break; 202
case ".mp2": strRet = "audio/mp2";break; 203
case ".mp2v": strRet = "video/mpeg";break; 204
case ".mp3": strRet = "audio/mp3";break; 205
case ".mp4": strRet = "video/mpeg4";break; 206
case ".mpa": strRet = "video/x-mpg";break; 207
case ".mpd": strRet = "application/vnd.ms-project";break; 208
case ".mpe": strRet = "video/x-mpeg";break; 209
case ".mpeg": strRet = "video/mpg";break; 210
case ".mpg": strRet = "video/mpg";break; 211
case ".mpga": strRet = "audio/rn-mpeg";break; 212
case ".mpp": strRet = "application/vnd.ms-project";break; 213
case ".mps": strRet = "video/x-mpeg";break; 214
case ".mpt": strRet = "application/vnd.ms-project";break; 215
case ".mpv": strRet = "video/mpg";break; 216
case ".mpv2": strRet = "video/mpeg";break; 217
case ".mpw": strRet = "application/vnd.ms-project";break; 218
case ".mpx": strRet = "application/vnd.ms-project";break; 219
case ".mtx": strRet = "text/xml";break; 220
case ".mxp": strRet = "application/x-mmxp";break; 221
case ".net": strRet = "image/pnetvue";break; 222
case ".nrf": strRet = "application/x-nrf";break; 223
case ".nws": strRet = "message/rfc822";break; 224
case ".odc": strRet = "text/x-ms-odc";break; 225
case ".out": strRet = "application/x-out";break; 226
case ".p10": strRet = "application/pkcs10";break; 227
case ".p12": strRet = "application/x-pkcs12";break; 228
case ".p7b": strRet = "application/x-pkcs7-certificates";break; 229
case ".p7c": strRet = "application/pkcs7-mime";break; 230
case ".p7m": strRet = "application/pkcs7-mime";break; 231
case ".p7r": strRet = "application/x-pkcs7-certreqresp";break; 232
case ".p7s": strRet = "application/pkcs7-signature";break; 233
case ".pc5": strRet = "application/x-pc5";break; 234
case ".pci": strRet = "application/x-pci";break; 235
case ".pcl": strRet = "application/x-pcl";break; 236
case ".pcx": strRet = "application/x-pcx";break; 237
case ".pdf": strRet = "application/pdf";break; 238
case ".pdx": strRet = "application/vnd.adobe.pdx";break; 239
case ".pfx": strRet = "application/x-pkcs12";break; 240
case ".pgl": strRet = "application/x-pgl";break; 241
case ".pic": strRet = "application/x-pic";break; 242
case ".pko": strRet = "application/vnd.ms-pki.pko";break; 243
case ".pl": strRet = "application/x-perl";break; 244
case ".plg": strRet = "text/html";break; 245
case ".pls": strRet = "audio/scpls";break; 246
case ".plt": strRet = "application/x-plt";break; 247
case ".png": strRet = "image/png";break; 248
case ".pot": strRet = "application/vnd.ms-powerpoint";break; 249
case ".ppa": strRet = "application/vnd.ms-powerpoint";break; 250
case ".ppm": strRet = "application/x-ppm";break; 251
case ".pps": strRet = "application/vnd.ms-powerpoint";break; 252
case ".ppt": strRet = "application/vnd.ms-powerpoint";break; 253
case ".pr": strRet = "application/x-pr";break; 254
case ".prf": strRet = "application/pics-rules";break; 255
case ".prn": strRet = "application/x-prn";break; 256
case ".prt": strRet = "application/x-prt";break; 257
case ".ps": strRet = "application/x-ps";break; 258
case ".ptn": strRet = "application/x-ptn";break; 259
case ".pwz": strRet = "application/vnd.ms-powerpoint";break; 260
case ".r3t": strRet = "text/vnd.rn-realtext3d";break; 261
case ".ra": strRet = "audio/vnd.rn-realaudio";break; 262
case ".ram": strRet = "audio/x-pn-realaudio";break; 263
case ".ras": strRet = "application/x-ras";break; 264
case ".rat": strRet = "application/rat-file";break; 265
case ".rdf": strRet = "text/xml";break; 266
case ".rec": strRet = "application/vnd.rn-recording";break; 267
case ".red": strRet = "application/x-red";break; 268
case ".rgb": strRet = "application/x-rgb";break; 269
case ".rjs": strRet = "application/vnd.rn-realsystem-rjs";break; 270
case ".rjt": strRet = "application/vnd.rn-realsystem-rjt";break; 271
case ".rlc": strRet = "application/x-rlc";break; 272
case ".rle": strRet = "application/x-rle";break; 273
case ".rm": strRet = "application/vnd.rn-realmedia";break; 274
case ".rmf": strRet = "application/vnd.adobe.rmf";break; 275
case ".rmi": strRet = "audio/mid";break; 276
case ".rmj": strRet = "application/vnd.rn-realsystem-rmj";break; 277
case ".rmm": strRet = "audio/x-pn-realaudio";break; 278
case ".rmp": strRet = "application/vnd.rn-rn_music_package";break; 279
case ".rms": strRet = "application/vnd.rn-realmedia-secure";break; 280
case ".rmvb": strRet = "application/vnd.rn-realmedia-vbr";break; 281
case ".rmx": strRet = "application/vnd.rn-realsystem-rmx";break; 282
case ".rnx": strRet = "application/vnd.rn-realplayer";break; 283
case ".rp": strRet = "image/vnd.rn-realpix";break; 284
case ".rpm": strRet = "audio/x-pn-realaudio-plugin";break; 285
case ".rsml": strRet = "application/vnd.rn-rsml";break; 286
case ".rt": strRet = "text/vnd.rn-realtext";break; 287
case ".rtf": strRet = "application/x-rtf";break; 288
case ".rv": strRet = "video/vnd.rn-realvideo";break; 289
case ".sam": strRet = "application/x-sam";break; 290
case ".sat": strRet = "application/x-sat";break; 291
case ".sdp": strRet = "application/sdp";break; 292
case ".sdw": strRet = "application/x-sdw";break; 293
case ".sit": strRet = "application/x-stuffit";break; 294
case ".slb": strRet = "application/x-slb";break; 295
case ".sld": strRet = "application/x-sld";break; 296
case ".slk": strRet = "drawing/x-slk";break; 297
case ".smi": strRet = "application/smil";break; 298
case ".smil": strRet = "application/smil";break; 299
case ".smk": strRet = "application/x-smk";break; 300
case ".snd": strRet = "audio/basic";break; 301
case ".sol": strRet = "text/plain";break; 302
case ".sor": strRet = "text/plain";break; 303
case ".spc": strRet = "application/x-pkcs7-certificates";break; 304
case ".spl": strRet = "application/futuresplash";break; 305
case ".spp": strRet = "text/xml";break; 306
case ".ssm": strRet = "application/streamingmedia";break; 307
case ".sst": strRet = "application/vnd.ms-pki.certstore";break; 308
case ".stl": strRet = "application/vnd.ms-pki.stl";break; 309
case ".stm": strRet = "text/html";break; 310
case ".sty": strRet = "application/x-sty";break; 311
case ".svg": strRet = "text/xml";break; 312
case ".swf": strRet = "application/x-shockwave-flash";break; 313
case ".tdf": strRet = "application/x-tdf";break; 314
case ".tg4": strRet = "application/x-tg4";break; 315
case ".tga": strRet = "application/x-tga";break; 316
case ".tif": strRet = "image/tiff";break; 317
case ".tiff": strRet = "image/tiff";break; 318
case ".tld": strRet = "text/xml";break; 319
case ".top": strRet = "drawing/x-top";break; 320
case ".torrent": strRet = "application/x-bittorrent";break; 321
case ".tsd": strRet = "text/xml";break; 322
case ".txt": strRet = "text/plain";break; 323
case ".uin": strRet = "application/x-icq";break; 324
case ".uls": strRet = "text/iuls";break; 325
case ".vcf": strRet = "text/x-vcard";break; 326
case ".vda": strRet = "application/x-vda";break; 327
case ".vdx": strRet = "application/vnd.visio";break; 328
case ".vml": strRet = "text/xml";break; 329
case ".vpg": strRet = "application/x-vpeg005";break; 330
case ".vsd": strRet = "application/vnd.visio";break; 331
case ".vss": strRet = "application/vnd.visio";break; 332
case ".vst": strRet = "application/vnd.visio";break;333
case ".vsw": strRet = "application/vnd.visio";break; 334
case ".vsx": strRet = "application/vnd.visio";break; 335
case ".vtx": strRet = "application/vnd.visio";break; 336
case ".vxml": strRet = "text/xml";break; 337
case ".wav": strRet = "audio/wav";break; 338
case ".wax": strRet = "audio/x-ms-wax";break; 339
case ".wb1": strRet = "application/x-wb1";break; 340
case ".wb2": strRet = "application/x-wb2";break; 341
case ".wb3": strRet = "application/x-wb3";break; 342
case ".wbmp": strRet = "image/vnd.wap.wbmp";break; 343
case ".wiz": strRet = "application/msword";break; 344
case ".wk3": strRet = "application/x-wk3";break; 345
case ".wk4": strRet = "application/x-wk4";break; 346
case ".wkq": strRet = "application/x-wkq";break; 347
case ".wks": strRet = "application/x-wks";break; 348
case ".wm": strRet = "video/x-ms-wm";break; 349
case ".wma": strRet = "audio/x-ms-wma";break; 350
case ".wmd": strRet = "application/x-ms-wmd";break; 351
case ".wmf": strRet = "application/x-wmf";break; 352
case ".wml": strRet = "text/vnd.wap.wml";break; 353
case ".wmv": strRet = "video/x-ms-wmv";break; 354
case ".wmx": strRet = "video/x-ms-wmx";break; 355
case ".wmz": strRet = "application/x-ms-wmz";break; 356
case ".wp6": strRet = "application/x-wp6";break; 357
case ".wpd": strRet = "application/x-wpd";break; 358
case ".wpg": strRet = "application/x-wpg";break; 359
case ".wpl": strRet = "application/vnd.ms-wpl";break; 360
case ".wq1": strRet = "application/x-wq1";break; 361
case ".wr1": strRet = "application/x-wr1";break; 362
case ".wri": strRet = "application/x-wri";break; 363
case ".wrk": strRet = "application/x-wrk";break; 364
case ".ws": strRet = "application/x-ws";break; 365
case ".ws2": strRet = "application/x-ws";break; 366
case ".wsc": strRet = "text/scriptlet";break; 367
case ".wsdl": strRet = "text/xml";break; 368
case ".wvx": strRet = "video/x-ms-wvx";break; 369
case ".xdp": strRet = "application/vnd.adobe.xdp";break; 370
case ".xdr": strRet = "text/xml";break; 371
case ".xfd": strRet = "application/vnd.adobe.xfd";break; 372
case ".xfdf": strRet = "application/vnd.adobe.xfdf";break; 373
case ".xhtml": strRet = "text/html";break; 374
case ".xls": strRet = "application/vnd.ms-excel";break; 375
case ".xlw": strRet = "application/x-xlw";break; 376
case ".xml": strRet = "text/xml";break; 377
case ".xpl": strRet = "audio/scpls";break; 378
case ".xq": strRet = "text/xml";break; 379
case ".xql": strRet = "text/xml";break; 380
case ".xquery": strRet = "text/xml";break; 381
case ".xsd": strRet = "text/xml";break; 382
case ".xsl": strRet = "text/xml";break; 383
case ".xslt": strRet = "text/xml";break; 384
case ".xwd": strRet = "application/x-xwd";break; 385
case ".x_b": strRet = "application/x-x_b";break;386
case ".x_t": strRet = "application/x-x_t"; break;387
}388
return strRet;389
}390

391

392
}393
}接着把这个项目引入 ,在wenconfig里添加
<httpHandlers>
<add verb="*" path="*.jpg,*.gif" type="WebTj.Jhttphandle.Httphandles,Jhttphandle"/>
</httpHandlers>
在iis中 isapi 中添加gif,jpg的映射 c:\windows\microsoft.net\framework\v1.1.4322\aspnet_isapi.dll
浙公网安备 33010602011771号