ASP防盗链及防下载的方法如果我们知道一个静态文件的实际路径如:windows.pdf">http://www.xx.com/download/51windows.pdf,如果服务器没有作特别的限制设置,我们就可以毫不费力的把它下载下来!当网站提供51windows.pdf下载时,怎么样才能让下载者无法得到他的实际路径呢!本文就来介绍如何使用Asp来隐藏文件的实际下载路径。 
ASP防盗链及防下载的方法

ASP防盗链及防下载的方法  我们在管理网站文件时,可以把扩展名一样的文件放在同一个目录下,起一个比较特别名字,例如放pdf文件目录为the_pdf_file_s,把下面代码另存为down.asp,他的网上路径为http:
//www.xx.com/down.asp,我们就可以用windows.pdf">http://www.xx.com/down.asp?FileName=51windows.pdf来下载这个文件了,而且下载者无法看到这个文件实际下载路径的!在down.asp中我们还可以设置下载文件是否需要登陆,判断下载的来源页是否为外部网站,从而可以做到防止文件被盗链。
ASP防盗链及防下载的方法

ASP防盗链及防下载的方法示例代码:
ASP防盗链及防下载的方法
<%
ASP防盗链及防下载的方法From_url 
= Cstr(Request.ServerVariables("HTTP_REFERER"))
ASP防盗链及防下载的方法Serv_url 
= Cstr(Request.ServerVariables("SERVER_NAME"))
ASP防盗链及防下载的方法
if mid(From_url,8,len(Serv_url)) <> Serv_url then
ASP防盗链及防下载的方法response.write 
"非法链接!" '防止盗链
ASP防盗链及防下载的方法
response.end
ASP防盗链及防下载的方法
end if
ASP防盗链及防下载的方法
ASP防盗链及防下载的方法
if Request.Cookies("Logined")="" then
ASP防盗链及防下载的方法response.redirect 
"/login.asp" '需要登陆!
ASP防盗链及防下载的方法
end if
ASP防盗链及防下载的方法
Function GetFileName(longname)'/folder1/folder2/file.asp=>file.asp
ASP防盗链及防下载的方法
while instr(longname,"/")
ASP防盗链及防下载的方法longname 
= right(longname,len(longname)-1)
ASP防盗链及防下载的方法
wend
ASP防盗链及防下载的方法GetFileName 
= longname
ASP防盗链及防下载的方法
End Function
ASP防盗链及防下载的方法
Dim Stream
ASP防盗链及防下载的方法
Dim Contents
ASP防盗链及防下载的方法
Dim FileName
ASP防盗链及防下载的方法
Dim TrueFileName
ASP防盗链及防下载的方法
Dim FileExt
ASP防盗链及防下载的方法
Const adTypeBinary = 1
ASP防盗链及防下载的方法FileName 
= Request.QueryString("FileName")
ASP防盗链及防下载的方法
if FileName = "" Then
ASP防盗链及防下载的方法  Response.Write 
"无效文件名!"
ASP防盗链及防下载的方法  Response.End
ASP防盗链及防下载的方法
End if
ASP防盗链及防下载的方法FileExt 
= Mid(FileName, InStrRev(FileName, "."+ 1)
ASP防盗链及防下载的方法
Select Case UCase(FileExt)
ASP防盗链及防下载的方法  
Case "ASP""ASA""ASPX""ASAX""MDB"
ASP防盗链及防下载的方法    Response.Write 
"非法操作!"
ASP防盗链及防下载的方法    Response.End
ASP防盗链及防下载的方法
End Select
ASP防盗链及防下载的方法Response.Clear
ASP防盗链及防下载的方法
if lcase(right(FileName,3))="gif" or lcase(right(FileName,3))="jpg" or lcase(right(FileName,3))="png" then
ASP防盗链及防下载的方法Response.ContentType 
= "image/*" '对图像文件不出现下载对话框
ASP防盗链及防下载的方法
else
ASP防盗链及防下载的方法Response.ContentType 
= "application/ms-download"
ASP防盗链及防下载的方法
end if
ASP防盗链及防下载的方法Response.AddHeader 
"content-disposition""attachment; filename=" & GetFileName(Request.QueryString("FileName"))
ASP防盗链及防下载的方法
Set Stream = server.CreateObject("ADODB.Stream")
ASP防盗链及防下载的方法Stream.Type 
= adTypeBinary
ASP防盗链及防下载的方法Stream.Open
ASP防盗链及防下载的方法
if lcase(right(FileName,3))="pdf" then '设置pdf类型文件目录
ASP防盗链及防下载的方法
TrueFileName = "/the_pdf_file_s/"&FileName
ASP防盗链及防下载的方法
end if 
ASP防盗链及防下载的方法
if lcase(right(FileName,3))="doc" then '设置DOC类型文件目录
ASP防盗链及防下载的方法
TrueFileName = "/my_D_O_C_file/"&FileName
ASP防盗链及防下载的方法
end if
ASP防盗链及防下载的方法
if lcase(right(FileName,3))="gif" or lcase(right(FileName,3))="jpg" or lcase(right(FileName,3))="png" then
ASP防盗链及防下载的方法TrueFileName 
= "/all_images_/"&FileName '设置图像文件目录
ASP防盗链及防下载的方法
end if
ASP防盗链及防下载的方法Stream.LoadFromFile Server.MapPath(TrueFileName)
ASP防盗链及防下载的方法
While Not Stream.EOS
ASP防盗链及防下载的方法  Response.BinaryWrite Stream.Read(
1024 * 64)
ASP防盗链及防下载的方法
Wend
ASP防盗链及防下载的方法Stream.Close
ASP防盗链及防下载的方法
Set Stream = Nothing
ASP防盗链及防下载的方法Response.Flush
ASP防盗链及防下载的方法Response.End
ASP防盗链及防下载的方法%
>
ASP防盗链及防下载的方法
ASP防盗链及防下载的方法

相关文章:

  • 2022-12-23
  • 2022-02-02
  • 2021-10-04
  • 2022-01-23
  • 2021-07-08
  • 2021-08-20
猜你喜欢
  • 2022-02-22
  • 2021-08-10
  • 2021-06-14
  • 2022-02-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案