简单实例一
这里可用到xmlhttp 和文件对象 fso,
代码如下 | 复制代码 |
<% Function SaveRemoteFile(sSavePath,sRemoteFileUrl) On Error Resume Next SaveRemoteFile = False Dim oXML : Set oXML = Server.CreateObject("Microsoft.XMLHTTP") With oXML .Open "Get",sRemoteFileUrl,False,"","" .Send If .Status<>200 Then Exit Function RemoteDate = .ResponseBody End With Set oXML = Nothing Dim oStream : Set oStream = Server.CreateObject("Adodb.Stream") With oStream .Type = 1 .Open .Write RemoteDate .SaveToFile sSavePath,2 If Err.Number=0 Then SaveRemoteFile = True .Close() End With Set oStream = Nothing End Function'调用方法如下 SaveAddr=Server.MapPath("demo.gif") |
实例1把图片保存到本地然后生成缩略图
ASP通过XMLHTTP获取远程图片流数据,并保存到本地,把第一张采集到的图片生成缩略图。
具体代码如下:
代码如下 | 复制代码 |
<% '================================================== '函数名:CheckDir2 '作 用:检查文件夹是否存在 '参 数:FolderPath ------文件夹地址 '================================================== Function CheckDir2(byval FolderPath) dim fso folderpath=Server.MapPath(".")&""&folderpath Set fso = Server.CreateObject("Scripting.FileSystemObject") If fso.FolderExists(FolderPath) then '存在 CheckDir2 = True Else '不存在 CheckDir2 = False End if Set fso = nothing End Function '================================================== '函数名:MakeNewsDir2 '作 用:创建新的文件夹 '参 数:foldername ------文件夹名称 '================================================== Function MakeNewsDir2(byval foldername) dim fso Set fso = Server.CreateObject("Scripting.FileSystemObject") fso.CreateFolder(Server.MapPath(".") &"" &foldername) If fso.FolderExists(Server.MapPath(".") &"" &foldername) Then MakeNewsDir2 = True Else MakeNewsDir2 = False End If Set fso = nothing End Function '================================================== '函数名:DefiniteUrl '作 用:将相对地址转换为绝对地址 '参 数:PrimitiveUrl ------要转换的相对地址 '参 数:ConsultUrl ------当前网页地址 '================================================== Function DefiniteUrl(Byval PrimitiveUrl,Byval ConsultUrl) Dim ConTemp,PriTemp,Pi,Ci,PriArray,ConArray If PrimitiveUrl="" or ConsultUrl="" or PrimitiveUrl="$False$" Then DefiniteUrl="$False$" Exit Function End If If Left(ConsultUrl,7)<>"HTTP://" And Left(ConsultUrl,7)<>"http://" Then ConsultUrl= "http://" & ConsultUrl End If ConsultUrl=Replace(ConsultUrl,"://",":\") If Right(ConsultUrl,1)<>"/" Then If Instr(ConsultUrl,"/")>0 Then If Instr(Right(ConsultUrl,Len(ConsultUrl)-InstrRev(ConsultUrl,"/")),".")>0 then Else ConsultUrl=ConsultUrl & "/" End If Else ConsultUrl=ConsultUrl & "/" End If End If ConArray=Split(ConsultUrl,"/") If Left(PrimitiveUrl,7) = "http://" then DefiniteUrl=Replace(PrimitiveUrl,"://",":\") ElseIf Left(PrimitiveUrl,1) = "/" Then DefiniteUrl=ConArray(0) & PrimitiveUrl ElseIf Left(PrimitiveUrl,2)="./" Then DefiniteUrl=ConArray(0) & Right(PrimitiveUrl,Len(PrimitiveUrl)-1) ElseIf Left(PrimitiveUrl,3)="../" then Do While Left(PrimitiveUrl,3)="../" PrimitiveUrl=Right(PrimitiveUrl,Len(PrimitiveUrl)-3) Pi=Pi+1 Loop For Ci=0 to (Ubound(ConArray)-1-Pi) If DefiniteUrl<>"" Then DefiniteUrl=DefiniteUrl & "/" & ConArray(Ci) Else DefiniteUrl=ConArray(Ci) End If Next DefiniteUrl=DefiniteUrl & "/" & PrimitiveUrl Else If Instr(PrimitiveUrl,"/")>0 Then PriArray=Split(PrimitiveUrl,"/") If Instr(PriArray(0),".")>0 Then If Right(PrimitiveUrl,1)="/" Then DefiniteUrl="http:\" & PrimitiveUrl Else If Instr(PriArray(Ubound(PriArray)-1),".")>0 Then DefiniteUrl="http:\" & PrimitiveUrl Else DefiniteUrl="http:\" & PrimitiveUrl & "/" End If End If Else If Right(ConsultUrl,1)="/" Then DefiniteUrl=ConsultUrl & PrimitiveUrl Else DefiniteUrl=Left(ConsultUrl,InstrRev(ConsultUrl,"/")) & PrimitiveUrl End If End If Else If Instr(PrimitiveUrl,".")>0 Then If Right(ConsultUrl,1)="/" Then If right(PrimitiveUrl,3)=".cn" or right(PrimitiveUrl,3)="com" or right(PrimitiveUrl,3)="net" or right(PrimitiveUrl,3)="org" Then DefiniteUrl="http:\" & PrimitiveUrl & "/" Else DefiniteUrl=ConsultUrl & PrimitiveUrl End If Else If right(PrimitiveUrl,3)=".cn" or right(PrimitiveUrl,3)="com" or right(PrimitiveUrl,3)="net" or right(PrimitiveUrl,3)="org" Then DefiniteUrl="http:\" & PrimitiveUrl & "/" Else DefiniteUrl=Left(ConsultUrl,InstrRev(ConsultUrl,"/")) & "/" & PrimitiveUrl End If End If Else If Right(ConsultUrl,1)="/" Then DefiniteUrl=ConsultUrl & PrimitiveUrl & "/" Else DefiniteUrl=Left(ConsultUrl,InstrRev(ConsultUrl,"/")) & "/" & PrimitiveUrl & "/" End If End If End If End If If Left(DefiniteUrl,1)="/" then DefiniteUrl=Right(DefiniteUrl,Len(DefiniteUrl)-1) End if If DefiniteUrl<>"" Then DefiniteUrl=Replace(DefiniteUrl,"//","/") DefiniteUrl=Replace(DefiniteUrl,":\","://") Else DefiniteUrl="$False$" End If End Function '================================================== '函数名:ReplaceSaveRemoteFile '作 用:替换、保存远程文件 '参 数:ConStr ------ 要替换的字符串 '参 数:StarStr ----- 前导 '参 数:OverStr ----- '参 数:IncluL ------ '参 数:IncluR ------ '参 数:SaveTf ------ 是否保存文件,False不保存,True保存 '参 数:SaveFilePath- 保存文件夹 '参 数: TistUrl------ 当前网页地址 '================================================== Function ReplaceSaveRemoteFile(ConStr,StartStr,OverStr,IncluL,IncluR,SaveTf,SaveFilePath,TistUrl) If ConStr="$False$" or ConStr="" Then ReplaceSaveRemoteFile="$False$" Exit Function End If Dim TempStr,TempStr2,ReF,Matches,Match,Tempi,TempArray,TempArray2,OverTypeArray Set ReF = New Regexp Dim RemoteFile,RemoteFileurl,SaveFileName,SaveFileType,ArrSaveFileName,RanNum '图片转换/保存 '================================================== 例: 程序代码 |
注:如果有些网防盗连这时图片不能保存了,我们需要模仿ie浏览器发布信息以用户正常浏览模式去下载,代码如
代码如下 | 复制代码 |
<% '盗链判断 Dim server_v1,server_v2 server_v1=Cstr(Request.ServerVariables("HTTP_REFERER")) server_v2=Cstr(Request.ServerVariables("SERVER_NAME")) If Mid(server_v1,8,len(server_v2))<>server_v2 Then Response.Write "非法的盗链" Response.End End If Dim url, body, myCache url = Request.QueryString("url") Set myCache = new cache If Err.Number = 0 Then '取得数据 'cache类 class Cache |
这些代码可以有效的破解图片防盗链系统.如网易相册.直接把下面的代码保存成pic.asp,然后用pic.asp?url=图片路径的方式调用即可.增加了缓存技术