当前位置: 移动技术网 > IT编程>开发语言>Asp > ASP 程序实现自动升级功能

ASP 程序实现自动升级功能

2017年12月12日  | 移动技术网IT编程  | 我要评论
现在流行虚拟主机建站,我也有个网站,也算是个站长咯。当了近一年的站长,感到网站程序每次升级的时候颇为麻烦:先去官方看公告,然后下载升级包到本地,解压,ftp上传到虚拟主机。这些都是累人的体力活,加之本人又懒得很,所以异想天开的觉得要是程序能够自动升级就好了。所以就想了想,写了本文,希望对web程序开发者有帮助。这里只针对asp,因为我只会asp :-(
先看看传统的win32程序的升级过程(比如杀毒软件),它是依靠软件的升级程序通过网络连接到服务器分析并下载升级文件到本地。
web程序有点不一样,因为它是运行于web服务器。它最终是要把升级服务器上的文件覆盖到web服务器,站长的电脑只是中转。如果直接把升级服务器上的文件copy到web服务器(而不通过站长中转)那就实现了自动升级。
好在系统自带了一个 microsoft.xmlhttp 组件用于访问web,在asp中可以调用它来实现连接升级服务器下载升级文件。
以下代码是利用 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服务器当前目录,至于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
写到这里差不多该结束了,还有些诸如版本判断之类的细节就略过略过咯。
希望早日用到自动升级的各类web程序,也好让我等懒人乐得悠闲,哈哈。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网