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

80酷酷网    80kuku.com

  ado|stream|web|xml|缓存|下载研究了几天Adodb.stream和XMLHTTP的应用,
找了不少很有趣的教程,
下面的代码是将一个远程的页面,图片地址保存到本地的实例。
将代码保存为一个.ASP的文件,
将它放到IIS的一个目录下,在它的上级见一个cache目录,
下载后的文件将保存到cache目录下

fso.Asp?path=http://www.im286.com/images/im286/logo.gif
你就可以将http://www.im286.com/images/im286/logo.gif文件保存下来。
学小偷的朋友也可以看看。。

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

sPath=Path
ifleft(lcase(path),7)<>"http://"then
'-------------如果前面没有http就是本地文件,交给LocalFile处理------------
LocalFile(path)
else
'--------------------否则为远程文件,交给RemoteFile处理------------------
RemoteFile(Path)
endif
'Response.Writeerr.Description
'--------------处理函数-----------

subLocalFile(Path)
'-------------------如果为本地文件则简单的跳转到该页面-------------------
'Response.RedirectPath
Response.write"发生错误!"
EndSub

SubRemoteFile(sPath)
'-------------------------处理远程文件函数------------------------------
FileName=GetFileName(sPath)
'-------------GetFileName为把地址转换为合格的文件名过程-------------
FileName=Server.MapPath("/Cache/"&FileName)
SetobjFso=Server.CreateObject("Scripting.FileSystemObject")
'Response.WritefileName
ifobjFso.FileExists(FileName)Then
'--------------检查文件是否是已经访问过,如是,则简单跳转------------
Response.Redirect"/cache/"&GetFileName(path)
Else
'----------------否则的话就先用GetBody函数读取----------------------
'Response.WritePath
t=GetBody(Path)
'-----------------用二进制方法写到浏览器上--------------------------
Response.BinaryWritet
Response.Flush
'-----------------输出缓冲------------------------------------------
SaveFilet,GetFileName(path)
'------------------将文件内容缓存到本地路径,以待下次访问-----------
Endif
SetobjFso=Nothing
EndSub

FunctionGetBody(url)
'-----------------------本函数为远程获取内容的函数---------------------
'onerrorresumenext
'Response.Writeurl
SetRetrieval=CreateObject("Microsoft.XMLHTTP")
'----------------------建立XMLHTTP对象-----------------------------
WithRetrieval
.Open"Get",url,False,"",""
'------------------用Get,异步的方法发送-----------------------
.Send
'GetBody=.ResponseText
GetBody=.ResponseBody
'------------------函数返回获取的内容--------------------------
EndWith
SetRetrieval=Nothing
'response.Writeerr.Description
EndFunction

FunctionGetFileName(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
EndFunction

subSaveFile(str,fName)
'-------------------------本函数为将流内容存盘的函数-------------------
'onerrorresumenext
SetobjStream=Server.CreateObject("ADODB.Stream")
'--------------建立ADODB.Stream对象,必须要ADO2.5以上版本---------
'objStream.Type=adTypeBinary
objStream.Type=1
'-------------以二进制模式打开-------------------------------------
objStream.Open
objstream.writestr
'--------------------将字符串内容写入缓冲--------------------------
'response.Writefname
'路径注意
objstream.SaveToFile"d:/web/cache/"&fName,2
'objstream.SaveToFile"d:cache"&fName,adSaveCreateOverWrite
'--------------------将缓冲的内容写入文件--------------------------
'response.BinaryWriteobjstream.Read
objstream.Close()
setobjstream=nothing
'-----------------------关闭对象,释放资源-------------------------
'response.Writeerr.Description
Endsub

functionsaveimage(from,tofile)
dimgeturl,objStream,imgs
geturl=trim(from)
imgs=gethttppage(geturl)'取得图片的具休内容的过程
SetobjStream=Server.CreateObject("ADODB.Stream")'建立ADODB.Stream对象,必须要ADO2.5以上版本
objStream.Type=1'以二进制模式打开
objStream.Open
objstream.writeimgs'将字符串内容写入缓冲
objstream.SaveToFileserver.mappath(tofile),2'-将缓冲的内容写入文件
objstream.Close()'关闭对象
setobjstream=nothing
endfunction
%>

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: