域名|空间 淘宝 拍拍网店传奇世界私服 林海科技频道 欢迎加盟1G双线赠数据库八折广告位56296827
返回列表 回复 发帖

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

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


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


fso.Asp?path=http://www.im286.com/images/im286/logo.gif

你就可以将文件保存下来。
学小偷的朋友也可以看看。。



[申请加精华]


CODE:
[url=###][Copy to clipboard][/url]
<%
&#39;----------远程获取内容,并将内容存在本地电脑上,包括任何文件!----------
&#39;---------------利用xmlhttp和adodb.stream-----------------
&#39;On Error Resume Next
&#39;-------------------------------定义输出格式-----------------------------
path=request("path")
if path ="" then
      path="http://www.baidu.com/index.html"
&#39;这里定义的网址是百度,,注意一定要有文件后缀
end if

sPath = Path
if left(lcase(path),7) <> "http://" then
&#39;-------------如果前面没有http就是本地文件,交给LocalFile处理------------
LocalFile(path)
else
&#39;--------------------否则为远程文件,交给RemoteFile处理------------------
RemoteFile(Path)
end if
&#39;Response.Write err.Description
&#39;--------------处理函数-----------

sub LocalFile(Path)
&#39;-------------------如果为本地文件则简单的跳转到该页面-------------------
&#39;Response.Redirect Path
Response.write "发生错误!"
End Sub

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

Function GetBody(url)
&#39;-----------------------本函数为远程获取内容的函数---------------------
&#39;on error resume next
&#39;Response.Write url
Set Retrieval = CreateObject("Microsoft.XMLHTTP")
&#39;----------------------建立XMLHTTP对象-----------------------------
With Retrieval
.Open "Get", url, False, "", ""
&#39;------------------用Get,异步的方法发送-----------------------
.Send
&#39;GetBody = .ResponseText
GetBody = .ResponseBody
&#39;------------------函数返回获取的内容--------------------------
End With
Set Retrieval = Nothing
&#39;response.Write err.Description
End Function

Function GetFileName(str)
&#39;-------------------------本函数为合格化的文件名函数-------------------
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)
&#39;-------------------------本函数为将流内容存盘的函数-------------------
&#39;on error resume next
Set objStream = Server.CreateObject("ADODB.Stream")
&#39;--------------建立ADODB.Stream对象,必须要ADO 2.5以上版本---------
&#39;objStream.Type = adTypeBinary
objStream.Type = 1
&#39;-------------以二进制模式打开-------------------------------------
objStream.Open
objstream.write str
&#39;--------------------将字符串内容写入缓冲--------------------------
&#39;response.Write fname
&#39;路径注意
objstream.SaveToFile "d:/web/cache/" & fName,2
&#39;objstream.SaveToFile "d:\cache\" & fName,adSaveCreateOverWrite
&#39;--------------------将缓冲的内容写入文件--------------------------
&#39;response.BinaryWrite objstream.Read
objstream.Close()
set objstream = nothing
&#39;-----------------------关闭对象,释放资源-------------------------
&#39;response.Write err.Description
End sub

function saveimage(from,tofile)
dim geturl,objStream,imgs
geturl=trim(from)
imgs=gethttppage(geturl)&#39;取得图片的具休内容的过程
Set objStream = Server.CreateObject("ADODB.Stream")&#39;建立ADODB.Stream对象,必须要ADO 2.5以上版本
objStream.Type =1&#39;以二进制模式打开
objStream.Open
objstream.write imgs&#39;将字符串内容写入缓冲
objstream.SaveToFile server.mappath(tofile),2&#39;-将缓冲的内容写入文件
objstream.Close()&#39;关闭对象
set objstream=nothing
end function
%>






第7宇宙16星区银河系11旋臂太阳系地球亚欧大陆亚洲
返回列表