欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

利用xmlhttp和adodb.stream加缓存技术下载远程Web文件

程序员文章站 2022-08-10 18:41:43
<%'----------远程获取内容,并将内容存在本地电脑上,包括任何文件!----------'---------------利用xmlhttp和adodb.st...

<%
'----------远程获取内容,并将内容存在本地电脑上,包括任何文件!----------
'---------------利用xmlhttp和adodb.stream-----------------
'on error resume next
'-------------------------------定义输出格式-----------------------------
path=request("path")
if path ="" then
path="http://pcqc.86516.com/index.asp"
'这里定义的网址是百度,,注意一定要有文件后缀
end if

spath = path
if left(lcase(path),7) <> "http://" then
'-------------如果前面没有http就是本地文件,交给localfile处理------------
localfile(path)
else
'--------------------否则为远程文件,交给remotefile处理------------------
remotefile(path)
end if
'response.write err.description
'--------------处理函数-----------

sub localfile(path)
'-------------------如果为本地文件则简单的跳转到该页面-------------------
'response.redirect path
response.write "发生错误!"
end sub
sub remotefile(spath)
'-------------------------处理远程文件函数------------------------------
filename = getfilename(spath)
'-------------getfilename为把地址转换为合格的文件名过程-------------
filename = server.mappath("cache/" & filename)
set objfso = server.createobject("scripting.filesystemobject")
'response.write filename
if objfso.fileexists(filename) then
'--------------检查文件是否是已经访问过,如是,则简单跳转------------
response.redirect "cache/" & getfilename(path)
else
'----------------否则的话就先用getbody函数读取----------------------
'response.write path
t = getbody(path)
'-----------------用二进制方法写到浏览器上--------------------------
response.binarywrite t
response.flush
'-----------------输出缓冲------------------------------------------
savefile t,getfilename(path)
'------------------将文件内容缓存到本地路径,以待下次访问-----------
end if
set objfso = nothing
end sub

function getbody(url)
'-----------------------本函数为远程获取内容的函数---------------------
'on error resume next
'response.write url
set retrieval = createobject("microsoft.xmlhttp")
'----------------------建立xmlhttp对象-----------------------------
with retrieval
.open "get", url, false, "", ""
'------------------用get,异步的方法发送-----------------------
.send
'getbody = .responsetext
getbody = .responsebody
'------------------函数返回获取的内容--------------------------
end with
set retrieval = nothing
'response.write err.description
end function

function getfilename(str)
'-------------------------本函数为合格化的文件名函数-------------------
str = replace(lcase(str),"http://","")
str = replace(lcase(str),"//","/")
str = replace(str,"?","")
str = replace(str,"&","")
str = replace(str,"/","")
str = replace(str,vbcrlf,"")
getfilename = str
end function

sub savefile(str,fname)
'-------------------------本函数为将流内容存盘的函数-------------------
'on error resume next
set objstream = server.createobject("adodb.stream")
'--------------建立adodb.stream对象,必须要ado 2.5以上版本---------
'objstream.type = adtypebinary
objstream.type = 1
'-------------以二进制模式打开-------------------------------------
objstream.open
objstream.write str
'--------------------将字符串内容写入缓冲--------------------------
'response.write fname
'路径注意
objstream.savetofile "e:\webroot\pcqc\vip\uploadfile\cache\"&fname,2
'objstream.savetofile "d:\cache\" & fname,adsavecreateoverwrite
'--------------------将缓冲的内容写入文件--------------------------
'response.binarywrite objstream.read
objstream.close()
set objstream = nothing
'-----------------------关闭对象,释放资源-------------------------
'response.write err.description
end sub

function saveimage(from,tofile)
dim geturl,objstream,imgs
geturl=trim(from)
imgs=gethttppage(geturl)'取得图片的具休内容的过程
set objstream = server.createobject("adodb.stream")'建立adodb.stream对象,必须要ado 2.5以上版本
objstream.type =1'以二进制模式打开
objstream.open
objstream.write imgs'将字符串内容写入缓冲
objstream.savetofile server.mappath(tofile),2'-将缓冲的内容写入文件
objstream.close()'关闭对象
set objstream=nothing
end function
%>