ASP 程序?qū)崿F(xiàn)自動(dòng)升級(jí)功能

2010-08-28 10:53:19來源:西部e網(wǎng)作者:

現(xiàn)在流行虛擬主機(jī)建站,我也有個(gè)網(wǎng)站,也算是個(gè)站長咯。當(dāng)了近一年的站長,感到網(wǎng)站程序每次升級(jí)的時(shí)候頗為麻煩:先去官方看公告,然后下載升級(jí)包到本地,解壓,F(xiàn)TP上傳到虛擬主機(jī)。這些都是累人的體力活,加之本人又懶得很,所以異想天開的覺得要是程序能夠自動(dòng)升級(jí)就好了。所以就想了想,寫了本文,希望對WEB程序開發(fā)者有幫助。這里只針對ASP,因?yàn)槲抑粫?huì)ASP :-(

先看看傳統(tǒng)的win32程序的升級(jí)過程(比如殺毒軟件),它是依靠軟件的升級(jí)程序通過網(wǎng)絡(luò)連接到服務(wù)器分析并下載升級(jí)文件到本地。

WEB程序有點(diǎn)不一樣,因?yàn)樗沁\(yùn)行于WEB服務(wù)器。它最終是要把升級(jí)服務(wù)器上的文件覆蓋到WEB服務(wù)器,站長的電腦只是中轉(zhuǎn)。如果直接把升級(jí)服務(wù)器上的文件Copy到WEB服務(wù)器(而不通過站長中轉(zhuǎn))那就實(shí)現(xiàn)了自動(dòng)升級(jí)。

好在系統(tǒng)自帶了一個(gè) Microsoft.XMLHTTP 組件用于訪問WEB,在ASP中可以調(diào)用它來實(shí)現(xiàn)連接升級(jí)服務(wù)器下載升級(jí)文件。

以下代碼是利用 Microsoft.XMLHTTP下載文件的例子:
 
<%
Set xPost = CreateObject("Microsoft.XMLHTTP")
         xPost.Open "GET","http://www.0x54.org/test.exe",False
         xPost.Send()
         Set sGet = CreateObject("ADODB.Stream")
         sGet.Mode = 3
         sGet.Type = 1
         sGet.Open()
         sGet.Write(xPost.responseBody)
         sGet.SaveToFile Server.MapPath("update.exe"),2
         set sGet = nothing
         set sPOST = nothing
         response.Write("下載文件成功!<br>")
%>

 
上面代碼就是把 http://www.0x54.org/test.exe保存到WEB服務(wù)器當(dāng)前目錄,至于Microsoft.XMLHTTP 的更多用法還是看看MSDN吧。

如果文件比較多,就會(huì)多次調(diào)用Microsoft.XMLHTTP連接網(wǎng)絡(luò),就可能出現(xiàn)某次連接失敗部分文件未能更新的情況,為了避免這種情況,最好是把所有文件打包為一個(gè)文件一次下載到WEB后再解包。

呵呵,這里說的打包可不是RAR或者ZIP包,而是我們自己定義。比如把所有文件拼接為一個(gè),然后再根據(jù)特殊的記號(hào)分開,F(xiàn)在沒這么麻煩咯,因?yàn)橛袀(gè)現(xiàn)成的辦法,我們使用拿來主義就是:把所有文件(二進(jìn)制形式)及其路徑信息放入Access數(shù)據(jù)庫。

下面這個(gè)vbs文件(來自海洋頂端2006Plus)就是打包當(dāng)前目錄的所有文件的:
 
       Dim n, ws, fsoX, thePath
         Set ws = CreateObject("WScript.Shell")
         Set fsoX = CreateObject("Scripting.FileSystemObject")
         thePath = ws.Exec("cmd /c cd").StdOut.ReadAll() & "\"
         i = InStr(thePath, Chr(13))
         thePath = Left(thePath, i - 1)
         n = len(thePath)
On Error Resume Next
         addToMdb(thePath)
         Wscript.Echo "當(dāng)前目錄已經(jīng)打包完畢,根目錄為當(dāng)前目錄"
         Sub addToMdb(thePath)
                   Dim rs, conn, stream, connStr
                   Set rs = CreateObject("ADODB.RecordSet")
                   Set stream = CreateObject("ADODB.Stream")
                   Set conn = CreateObject("ADODB.Connection")
                   Set adoCatalog = CreateObject("ADOX.Catalog")
                   connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Packet.mdb"
                   adoCatalog.Create connStr
                   conn.Open connStr
                   conn.Execute("Create Table FileData(Id int IDENTITY(0,1) PRIMARY KEY CLUSTERED, P Text, fileContent Image)")
                   stream.Open
                   stream.Type = 1
                  rs.Open "FileData", conn, 3, 3
                   fsoTreeForMdb thePath, rs, stream
                   rs.Close
                   Conn.Close
                   stream.Close
                   Set rs = Nothing
                   Set conn = Nothing
                   Set stream = Nothing
                   Set adoCatalog = Nothing
         End Sub
         Function fsoTreeForMdb(thePath, rs, stream)
                   Dim i, item, theFolder, folders, files
                   sysFileList = "$" & WScript.ScriptName & "$Packet.mdb$Packet.ldb$"
                   Set theFolder = fsoX.GetFolder(thePath)
                   Set files = theFolder.Files
                   Set folders = theFolder.SubFolders
                   For Each item In folders
                            fsoTreeForMdb item.Path, rs, stream
                   Next
                   For Each item In files
                            If InStr(LCase(sysFileList), "$" & LCase(item.Name) & "$") <= 0 Then
                                     rs.AddNew
                                     rs("P") = Mid(item.Path, n + 2)
                                     stream.LoadFromFile(item.Path)
                                     rs("fileContent") = stream.Read()
                                     rs.Update
                            End If
                   Next
                   Set files = Nothing
                   Set folders = Nothing
                   Set theFolder = Nothing
         End Function

 
以下是解包的ASP文件:
 
<%
         Sub UnPack()
                   str = Server.MapPath(".") & "\"
                   Set rs = CreateObject("ADODB.RecordSet")
                   Set stream = CreateObject("ADODB.Stream")
                   Set conn = CreateObject("ADODB.Connection")
                   Set oFso = CreateObject("Scripting.FileSystemObject")
                   connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("update.mdb")
                   conn.Open connStr
                   rs.Open "FileData", conn, 1, 1
                   stream.Open
                   stream.Type = 1
                   Do Until rs.Eof
                            theFolder = Left(rs("P"), InStrRev(rs("P"), "\"))
                            If oFso.FolderExists(str & theFolder) = False Then
                                     oFso.CreateFolder(str & theFolder)
                            End If
                            stream.SetEOS()
                            If IsNull(rs("fileContent")) = False Then stream.Write rs("fileContent")
                            stream.SaveToFile str & rs("P"), 2
                            rs.MoveNext
                   Loop
                   rs.Close
                   conn.Close
                   stream.Close
                   Set ws = Nothing
                   Set rs = Nothing
                   Set stream = Nothing
                   Set conn = Nothing
                   Set oFso = Nothing
         End Sub
%>

 
嗯,有了以上代碼就不難開發(fā)出自己的ASP升級(jí)程序了,流程無外乎這樣:判斷是否需要升級(jí)(Y) -> 下載升級(jí)包 -> 解開升級(jí)包覆蓋舊文件 -> 刪除升級(jí)包 -> 更新版本信息 -> OK
寫到這里差不多該結(jié)束了,還有些諸如版本判斷之類的細(xì)節(jié)就略過略過咯。

希望早日用到自動(dòng)升級(jí)的各類WEB程序,也好讓我等懶人樂得悠閑,哈哈。

 

關(guān)鍵詞:ASP

贊助商鏈接: