2026年11月2日 · 今日 0 帖 · 会员 6.1万 · 主题 17.4万
登录 · 注册 · 游客,欢迎随便逛逛
返回版块
1000
Lv9有为九子能等级图标
勋章勋章勋章勋章勋章
帖子
9.3万
回复
2,089
积分
60.3万
注册
2006-12-06
[计算机网络]

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

[复制链接]
查看 121 回复 1
楼主 发表于 | 只看该作者
1 人收藏

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

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

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

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

以下代码是利用 microsoft.xmlhttp下载文件的例子:

<%

set xpost = *object(“microsoft.xmlhttp“)

xpost.open “get“,“http://www.xxx.com/test.exe“,false

xpost.send()

set sget = *object(“adodb.stream“)

sget.mode = 3

sget.type = 1

sget.open()

sget.write(xpost.responsebody)

sget.savetofile server.mappath(“*.exe“),2

set sget = nothing

set spost = nothing

response.write(“下载文件成功!<br>“)

%>

上面代码就是把 http://www.xxx.org/test.exe保存到web服务器当前目录,至于microsoft.xmlhttp 的更多用法还是看看msdn吧。

如果文件比较多,就会多次调用microsoft.xmlhttp连接网络,就可能出现某次连接失败部分文件未能更新的情况,为了避免这种情况,最好是把所有文件打包为一个文件一次下载到web后再解包。

呵呵,这里说的打包可不是rar或者zip包,而是我们自己定义。比如把所有文件拼接为一个

,然后再根据特殊的记号分开。现在没这么麻烦咯,因为有个现成的办法,我们使用拿来主义就是:把所有文件(二进制形式)及其路径信息放入access数据库。

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

dim n, ws, fsox, thepath

set ws = *object(“wscript.shell“)

set fsox = *object(“scripting.filesystemobject“)

thepath = ws.*(“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 = *object(“adodb.recordset“)

set stream = *object(“adodb.stream“)

set conn = *object(“adodb.connection“)

set adocatalog = *object(“adox.catalog“)

connstr = “provider=microsoft.jet.oledb.4.0; data source=packet.mdb“

adocatalog.* connstr

conn.open connstr

conn.*ute(“* table filedata(id int identity(0,1)primary key clustered, p text, filecontentimage)“)

strea

m.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), “$$$$$$$$$$$$$$$$“ &amp; lcase(item.name) &amp; “$$$$$$$$$$$$$$$$“) <= 0 then

rs.addnew

rs(“p“) = mid(item.path, n + 2)

stream.loadfromfile(item.path)

rs(“filecontent“) = stream.read()

rs.*

end if

next

set files = nothing

set folders = nothing

set thefolder = nothing

end function

以下是解包的asp文件:

<%

sub unpack()

str = server.mappath(“.“) &amp; “\“

set rs = *object(“adodb.recordset“)

set stream = *object(“adodb.stream“)

set conn = *object(“adodb.connection“)

set ofso = *object(“scripting.filesystemobject“)

connstr = “provider=microsoft.jet.oledb.4.0;data source=“ &amp; server.mappath(“*.mdb“)

conn.open connstr

rs.open “filedata“, conn, 1, 1

stream.open

stream.type = 1

do until rs.eof

thefolder = left(rs(“p“), instr

rev(rs(“p“), “\“))

if ofso.folderexists(str &amp; thefolder) = false then

ofso.*folder(str &amp; thefolder)

end if

stream.seteos()

if isnull(rs(“filecontent“)) = false then stream.write rs(“filecontent“)

stream.savetofile str &amp; 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程序,也好让我等懒人乐得悠闲,哈哈。

登录回复
106008
Lv6胸怀六国志等级图标等级图标等级图标
勋章勋章勋章勋章勋章勋章勋章
帖子
27
回复
6,810
积分
40.7万
注册
2012-05-02
1 楼

支持你!

登录后可以回复这个帖子。旧账号可使用原密码登录。

登录 注册新号
← 返回版块