ASP网站程序自动升级实现的方法

网络整理 - 09-06

现在流行虚拟主机建站,我也有个网站,也算是个站长咯。当了近一年的站长,感到网站程序每次升级的时候颇为麻烦:先去官方看公告,然后下载升级包到本地,解压,FTP上传到虚拟主机。这些都是累人的体力活,加之本人又懒得很,所以异想天开的觉得要是程序能够自动升级就好了。所以就想了想,写了本文,希望对WEB程序开发者有帮助。这里只针对ASP,因为我只会ASP :-(

先看看传统的win32程序的升级过程(比如杀毒软件),它是依靠软件的升级程序通过网络连接到服务器分析并下载升级文件到本地。

WEB程序有点不一样,因为它是运行于WEB服务器。它最终是要把升级服务器上的文件覆盖到WEB服务器,站长的电脑只是中转。如果直接把升级服务器上的文件Copy到WEB服务器(而不通过站长中转)那就实现了自动升级。

好在系统自带了一个 Microsoft.XMLHTTP 组件用于访问WEB,在ASP中可以调用它来实现连接升级服务器下载升级文件。

以下代码是利用 Microsoft.XMLHTTP下载文件的例子:

以下为引用的内容:

<%
Set xPost = CreateObject("Microsoft.XMLHTTP")
         xPost.Open "GET","",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>")
%>

上面代码就是把 保存到WEB服务器当前目录,至于Microsoft.XMLHTTP 的更多用法还是看看MSDN吧。

如果文件比较多,就会多次调用Microsoft.XMLHTTP连接网络,就可能出现某次连接失败部分文件未能更新的情况,为了避免这种情况,最好是把所有文件打包为一个文件一次下载到WEB后再解包。
呵呵,这里说的打包可不是RAR或者ZIP包,而是我们自己定义。比如把所有文件拼接为一个,然后再根据特殊的记号分开。现在没这么麻烦咯,因为有个现成的办法,我们使用拿来主义就是:把所有文件(二进制形式)及其路径信息放入Access数据库。

下面这个vbs文件(来自海洋顶端2006Plus)就是打包当前目录的所有文件的:

以下为引用的内容:

       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 "当前目录已经打包完毕,根目录为当前目录"
         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
%>


有了以上代码就不难开发出自己的ASP升级程序了,流程无外乎这样:判断是否需要升级(Y) -> 下载升级包 -> 解开升级包覆盖旧文件 -> 删除升级包 -> 更新版本信息 -> OK