asp防盜鏈原理及代碼

來源:互聯網
上載者:User

防盜鏈的原理:
不直接給出伺服器中真實的url,將要訪問的檔案放到伺服器上不可訪問的目錄中,所以要訪問的檔案就必須通過asp讀取檔案以二進位流的類型來傳送檔案,如果加上來源地址和使用者登入狀態的判斷,可以一定意義上的防止盜鏈。
缺點是:不過開銷也會很大。

下面是兩個樣本的函數,沒有加上許可權判斷。
Sub downloadFile(strFile) 
On error resume next 
Server.ScriptTimeOut=999999 
Dim S,fso,f,intFilelength,strFilename,DownFileName 

strFilename = Server.MapPath(strFile) 
Response.Clear 
Set s = Server.CreateObject("ADODB.Stream") 
s.Open 
s.Type = 1 
Set fso = Server.CreateObject("Scripting.FileSystemObject") 
If Not fso.FileExists(strFilename) Then 
'Response.Write("<h1>錯誤: </h1>
系統找不到指定檔案") 
showimg "images/logos.gif" 
Exit Sub 
End If 
Set f = fso.GetFile(strFilename) 
intFilelength = f.size 
s.LoadFromFile(strFilename) 
If err Then 
'Response.Write("<h1>錯誤: </h1>" & err.Description & "<p>") 
showimg "images/logos.gif" 
Response.End 
End If 
Set fso=Nothing 
Dim Data 
Data=s.Read 
s.Close 
Set s=Nothing 
If Response.IsClientConnected Then 
Response.AddHeader "Content-Disposition", "attachment; filename=" & strFile 
Response.AddHeader "Content-Length", intFilelength 
Response.CharSet = "UTF-8" 
Response.ContentType = "application/octet-stream" 
Response.BinaryWrite Data 
Response.Flush 
End If 
End Sub 



ASP防盜鏈輸出圖片函數 showimg (需要Persits.Jpeg組件支援) 

Sub showimg(FileName) 
Response.Clear 
Dim Jpeg,temp_pic 
On Error Resume Next 
Set Jpeg = Server.CreateObject("Persits.Jpeg") 
If -2147221005=Err then 
'Response.write "沒有這個組件,請安裝!" '檢查是否安裝AspJpeg組件 
downloadFile FileName 
Exit Sub 
End If 
Jpeg.Open (Server.MapPath(FileName)) '開啟圖片 
If err.number then 
'Response.write "開啟圖片失敗,請檢查路徑!" 
Jpeg.Open (Server.MapPath("images/logos.gif")) 
response.end 
End if 
temp_pic=Jpeg.Binary 
Response.ContentType = "image/*" 
Response.AddHeader "Content-Disposition","filename=" & arr_FileName(FileName) 
Response.BinaryWrite temp_pic 
Response.End 
End Sub 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.