CSDN博客

img kingapex1

防盗连

发表于2004/9/28 11:55:00  939人阅读

分类: asp

/////////////////////////////////////////////////
0000:<%
0001:'******************************
0002:'Write By: DLL
0003:'NetBuilder 出品
0004:'文件名使用URL参数/表单项传递,项名为FileName,对GIF和JPG直接输出图片流,其他文件则一律弹出下载提示框
0005:'******************************
0006:On Error Resume Next
0007:Response.Buffer = True
0008:Response.Clear
0009:
0010:Const HidDir = "../XBB2003DFSDADA/"   '根据你的文件所在目录修改
0011:
0012:SUB UseStream(FileName,FileNameString)
0013:     Dim FileStream,File,FileContentType,IsAttachment
0014:     Set FileStream = Server.CreateObject("ADODB.Stream")
0015:     FileStream.Open
0016:     FileStream.Type = 1
0017:     File = server.MapPath(FileName)
0018:     FileStream.LoadFromFile(File)
0019:     FileContentType = GetContentType(FileName)
0020:     If FileContentType <> "image/gif" And FileContentType <> "image/jpeg" Then
0021:           IsAttachment = "attachment; "
0022:     Else
0023:           IsAttachment = ""
0024:     End If
0025:     Response.AddHeader "Content-Disposition", IsAttachment & "filename=" & FileNameString
0026:     Response.AddHeader "Content-Length", FileStream.Size
0027:     Response.Charset = "UTF-8"
0028:     Response.ContentType = FileContentType
0029:     Response.BinaryWrite FileStream.Read
0030:     Response.Flush
0031:
0032:     FileStream.Close
0033:     Set FileStream = Nothing
0034:End SUB
0035:
0036:Function GetFilePath(FileName,HiddenDir)       '限制盗链的函数,当来源地址中的域名和当前文件地址的域名不同时则输出自定义错误图片NoImg.gif,您也可以设置为用Session限制
0037:     Dim Server_v1,Server_v2
0038:     Server_v1 = Cstr(Request.ServerVariables("HTTP_REFERER"))
0039:     Server_v2 = Cstr(Request.ServerVariables("SERVER_NAME"))
0040:     'If Server_v1<>"" And Mid(Server_v1,8,Len(Server_v2)) = Server_v2 Then
0041:           GetFilePath = HiddenDir & FileName
0042:     'Else
0043:           'GetFilePath = "NoImg.gif"
0044:     'End If
0045:End Function
0046:
0047:Function GetContentType(FlName)
0048:     Select Case lcase(Right(flName, 4))
0049:     Case ".asf"
0050:           GetContentType = "video/x-ms-asf"
0051:     Case ".avi"
0052:           GetContentType = "video/avi"
0053:     Case ".doc"
0054:           GetContentType = "application/msword"
0055:     Case ".zip"
0056:           GetContentType = "application/zip"
0057:     Case ".xls"
0058:           GetContentType = "application/vnd.ms-excel"
0059:     Case ".gif"
0060:           GetContentType = "image/gif"
0061:     Case ".jpg", "jpeg"
0062:           GetContentType = "image/jpeg"
0063:     Case ".wav"
0064:           GetContentType = "audio/wav"
0065:     Case ".mp3"
0066:           GetContentType = "audio/mpeg3"
0067:     Case ".mpg", "mpeg"
0068:           GetContentType = "video/mpeg"
0069:     Case ".rtf"
0070:           GetContentType = "application/rtf"
0071:     Case ".htm", "html"
0072:           GetContentType = "text/html"
0073:     Case ".txt"
0074:           GetContentType = "text/plain"
0075:     Case Else
0076:           GetContentType = "application/octet-stream"
0077:     End Select
0078:End Function
0079:
0080:Dim FileName,FilePath
0081:FileName = Trim(Request("FileName"))
0082:FilePath = GetFilePath(FileName,HidDir)
0083:
0084:If Lcase(Right(FilePath, 4)) = ".gif" Then             '如果是GIF文件则可直接用Server.Execute输出它的二进制流.
0085:     Response.AddHeader "Content-Disposition", "filename=" & FileName
0086:     Response.AddHeader "Content-Length", FileStream.Size
0087:     Response.Charset = "UTF-8"
0088:     Response.ContentType = GetContentType(FileName)
0089:     Server.Execute(FilePath)
0090:     If err.Number <> 0 Then
0091:           err.Clear
0092:           Server.Execute("NoImg2.gif")
0093:           Response.End()
0094:     End If
0095:Else    '如果不是GIF图象则使用ADODB.STREAM对象输出其二进制流
0096:     UseStream FilePath,FileName
0097:     If Err.Number <> 0 Then
0098:           Err.Clear
0099:           Server.Execute("NoImg2.gif")
0100:     End If
0101:End If
0102:
0103:如果程序出错则输出自定义错误图片NoImg2.gif
0104:
0105:%>
0106:
阅读全文
0 0

相关文章推荐

img
取 消
img