LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

ASP保存远程图片到本地,并生成缩略图

admin
2010年7月14日 1:7 本文热度 5532
[p]asp通过xmlhttp获取远程图片流数据,并保存到本地,把第一张采集到的图片生成缩略图。[/p]
[p]具体代码如下:[/p]
[p]<% [br]'================================================== [br]'函数名:checkdir2 [br]'作 用:检查文件夹是否存在 [br]'参 数:folderpath ------文件夹地址 [br]'================================================== [br]function checkdir2(byval folderpath) [br]dim fso [br]folderpath=server.mappath(".")&"\"&folderpath [br]set fso = server.createobject("scripting.filesystemobject") [br]if fso.folderexists(folderpath) then [br]'存在 [br]checkdir2 = true [br]else [br]'不存在 [br]checkdir2 = false [br]end if [br]set fso = nothing [br]end function [br]'================================================== [br]'函数名:makenewsdir2 [br]'作 用:创建新的文件夹 [br]'参 数:foldername ------文件夹名称 [br]'================================================== [br]function makenewsdir2(byval foldername) [br]dim fso [br]set fso = server.createobject("scripting.filesystemobject") [br]fso.createfolder(server.mappath(".") &"\" &foldername) [br]if fso.folderexists(server.mappath(".") &"\" &foldername) then [br]makenewsdir2 = true [br]else [br]makenewsdir2 = false [br]end if [br]set fso = nothing [br]end function [br]'================================================== [br]'函数名:definiteurl [br]'作 用:将相对地址转换为绝对地址 [br]'参 数:primitiveurl ------要转换的相对地址 [br]'参 数:consulturl ------当前网页地址 [br]'================================================== [br]function definiteurl(byval primitiveurl,byval consulturl) [br]dim contemp,pritemp,pi,ci,priarray,conarray [br]if primitiveurl="" or consulturl="" or primitiveurl="$false$" then [br]definiteurl="$false$" [br]exit function [br]end if [br]if left(consulturl,7)<>"http://" and left(consulturl,7)<>"http://" then [br]consulturl= "http://" & consulturl [br]end if [br]consulturl=replace(consulturl,"://",":\\") [br]if right(consulturl,1)<>"/" then [br]if instr(consulturl,"/")>0 then [br]if instr(right(consulturl,len(consulturl)-instrrev(consulturl,"/")),".")>0 then [br]else [br]consulturl=consulturl & "/" [br]end if [br]else [br]consulturl=consulturl & "/" [br]end if [br]end if [br]conarray=split(consulturl,"/") [br]if left(primitiveurl,7) = "http://" then [br]definiteurl=replace(primitiveurl,"://",":\\") [br]elseif left(primitiveurl,1) = "/" then [br]definiteurl=conarray(0) & primitiveurl [br]elseif left(primitiveurl,2)="./" then [br]definiteurl=conarray(0) & right(primitiveurl,len(primitiveurl)-1) [br]elseif left(primitiveurl,3)="../" then [br]do while left(primitiveurl,3)="../" [br]primitiveurl=right(primitiveurl,len(primitiveurl)-3) [br]pi=pi+1 [br]loop [br]for ci=0 to (ubound(conarray)-1-pi) [br]if definiteurl<>"" then [br]definiteurl=definiteurl & "/" & conarray(ci) [br]else [br]definiteurl=conarray(ci) [br]end if [br]next [br]definiteurl=definiteurl & "/" & primitiveurl [br]else [br]if instr(primitiveurl,"/")>0 then [br]priarray=split(primitiveurl,"/") [br]if instr(priarray(0),".")>0 then [br]if right(primitiveurl,1)="/" then [br]definiteurl="http:\\" & primitiveurl [br]else [br]if instr(priarray(ubound(priarray)-1),".")>0 then [br]definiteurl="http:\\" & primitiveurl [br]else [br]definiteurl="http:\\" & primitiveurl & "/" [br]end if [br]end if [br]else [br]if right(consulturl,1)="/" then [br]definiteurl=consulturl & primitiveurl [br]else [br]definiteurl=left(consulturl,instrrev(consulturl,"/")) & primitiveurl [br]end if [br]end if [br]else [br]if instr(primitiveurl,".")>0 then [br]if right(consulturl,1)="/" then [br]if right(primitiveurl,3)=".cn" or right(primitiveurl,3)="com" or right(primitiveurl,3)="net" or right(primitiveurl,3)="org" then [br]definiteurl="http:\\" & primitiveurl & "/" [br]else [br]definiteurl=consulturl & primitiveurl [br]end if [br]else [br]if right(primitiveurl,3)=".cn" or right(primitiveurl,3)="com" or right(primitiveurl,3)="net" or right(primitiveurl,3)="org" then [br]definiteurl="http:\\" & primitiveurl & "/" [br]else [br]definiteurl=left(consulturl,instrrev(consulturl,"/")) & "/" & primitiveurl [br]end if [br]end if [br]else [br]if right(consulturl,1)="/" then [br]definiteurl=consulturl & primitiveurl & "/" [br]else [br]definiteurl=left(consulturl,instrrev(consulturl,"/")) & "/" & primitiveurl & "/" [br]end if [br]end if [br]end if [br]end if [br]if left(definiteurl,1)="/" then [br]definiteurl=right(definiteurl,len(definiteurl)-1) [br]end if [br]if definiteurl<>"" then [br]definiteurl=replace(definiteurl,"//","/") [br]definiteurl=replace(definiteurl,":\\","://") [br]else [br]definiteurl="$false$" [br]end if [br]end function [br]'================================================== [br]'函数名:replacesaveremotefile [br]'作 用:替换、保存远程文件 [br]'参 数:constr ------ 要替换的字符串 [br]'参 数:starstr ----- 前导 [br]'参 数:overstr ----- [br]'参 数:inclul ------ [br]'参 数:inclur ------ [br]'参 数:savetf ------ 是否保存文件,false不保存,true保存 [br]'参 数:savefilepath- 保存文件夹 [br]'参 数: tisturl------ 当前网页地址 [br]'================================================== [br]function replacesaveremotefile(constr,startstr,overstr,inclul,inclur,savetf,savefilepath,tisturl) [br]if constr="$false$" or constr="" then [br]replacesaveremotefile="$false$" [br]exit function [br]end if [br]dim tempstr,tempstr2,ref,matches,match,tempi,temparray,temparray2,overtypearray [br][br]set ref = new regexp [br]ref.ignorecase = true [br]ref.global = true [br]ref.pattern = "("&startstr&").+?("&overstr&")" [br]set matches =ref.execute(constr) [br]for each match in matches [br]if instr(tempstr,match.value)=0 then [br]if tempstr<>"" then [br]tempstr=tempstr & "$array$" & match.value [br]else [br]tempstr=match.value [br]end if [br]end if [br]next [br]set matches=nothing [br]set ref=nothing [br]if tempstr="" or isnull(tempstr)=true then [br]replacesaveremotefile=constr [br]exit function [br]end if [br]if inclul=false then [br]tempstr=replace(tempstr,startstr,"") [br]end if [br]if inclur=false then [br]if instr(overstr,"|")>0 then [br]overtypearray=split(overstr,"|") [br]for tempi=0 to ubound(overtypearray) [br]tempstr=replace(tempstr,overtypearray(tempi),"") [br]next [br]else [br]tempstr=replace(tempstr,overstr,"") [br]end if [br]end if [br]tempstr=replace(tempstr,"""","") [br]tempstr=replace(tempstr,"'","") [br][br]dim remotefile,remotefileurl,savefilename,savefiletype,arrsavefilename,rannum [br]if right(savefilepath,1)="/" then [br]savefilepath=left(savefilepath,len(savefilepath)-1) [br]end if [br]if savetf=true then [br]if checkdir2(savefilepath)=false then [br]if makenewsdir2(savefilepath)=false then [br]savetf=false [br]end if [br]end if [br]end if [br]savefilepath=savefilepath & "/" [br][br]'图片转换/保存 [br]temparray=split(tempstr,"$array$") [br]for tempi=0 to ubound(temparray) [br]remotefileurl=definiteurl(temparray(tempi),tisturl) [br]if remotefileurl<>"$false$" and savetf=true then'保存图片 [br]arrsavefilename = split(remotefileurl,".") [br]savefiletype=arrsavefilename(ubound(arrsavefilename))'文件类型 [br]rannum=int(900*rnd)+100 [br]savefilename = savefilepath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&rannum&"."&savefiletype [br]call saveremotefile(savefilename,remotefileurl) [br]constr=replace(constr,temparray(tempi),savefilename) [br]elseif remotefileurl<>"$false$" and savetf=false then'不保存图片 [br]savefilename=remotefileurl [br]constr=replace(constr,temparray(tempi),savefilename) [br]end if [br]if remotefileurl<>"$false$" then [br]if uploadfiles="" then [br]uploadfiles=savefilename [br]else [br]uploadfiles=uploadfiles & "|" & savefilename [br]end if [br]end if [br]next [br]replacesaveremotefile=constr [br]end function [br]'================================================== [br]'过程名:saveremotefile [br]'作 用:保存远程的文件到本地 [br]'参 数:localfilename ------ 本地文件名 [br]'参 数:remotefileurl ------ 远程文件url [br]'================================================== [br]sub saveremotefile(localfilename,remotefileurl) [br]dim ads,retrieval,getremotedata [br]set retrieval = server.createobject("microsoft.xmlhttp") [br]with retrieval [br].open "get", remotefileurl, false, "", "" [br].send [br]getremotedata = .responsebody [br]end with [br]set retrieval = nothing [br]set ads = server.createobject("adodb.stream") [br]with ads [br].type = 1 [br].open [br].write getremotedata [br].savetofile server.mappath(localfilename),2 [br].cancel() [br].close() [br]end with [br]set ads=nothing [br]end sub [br][br]'================================================== [br]'过程名:getimg [br]'作 用:取得文章中第一张图片 [br]'参 数:str ------ 文章内容 [br]'参 数:strpath ------ 保存图片的路径 [br]'================================================== [br]function getimg(str,strpath) [br]set objregex = new regexp [br]objregex.ignorecase = true [br]objregex.global = true [br]zzstr=""&strpath&"(.+?)\.(jpg|gif|png|bmp)" [br]objregex.pattern = zzstr [br]set matches = objregex.execute(str) [br]for each match in matches [br]retstr = retstr &"|"& match.value [br]next [br]if retstr<>"" then [br]imglist=split(retstr,"|") [br]imgone=replace(imglist(1),strpath,"") [br]getimg=imgone [br]else [br]getimg="" [br]end if [br]end function [br]%> [br][br]例: [br][br]程序代码 [br]
[br] [br] [br]
[br]<% [br]if request.querystring("action")="test" then [br]'图片开始的字符串 [br]filesstartstr="src=" [br]'图片结束的字符串 [br]filesoverstr="gif|jpg|bmp" [br]'保存图片的文件夹 [br]filespath="qq" [br]'取得保存图片的网站url 自动判断是绝对 还是相对路径 [br]newsurl="http://news.163.com" [br]'取得文章内容 [br]content =request.form("body") [br]'开始保存图片 [br]content=replacesaveremotefile(content,filesstartstr,filesoverstr,false,true,true,filespath,newsurl) [br]'对新闻中的第一张图片创建缩略图 [br]if getimg(content,filespath)<>"" then [br]imgsrc=getimg(content,filespath) [br]imgsrc=replace(imgsrc,filespath,"") [br]set jpeg = server.createobject("persits.jpeg") [br]path = server.mappath(""&filespath&"") & "\"&imgsrc&"" [br]jpeg.open path [br]'如果图片宽小于等于120 高小于等于90 则不创建缩略图 [br]if jpeg.originalwidth<=120 and jpeg.height<=90 then [br]jpeg.width = jpeg.originalwidth [br]jpeg.height = jpeg.originalheight [br]smallimg=filespath&""&getimg(content,filespath) [br]else [br]'图片宽度高度/2 [br]jpeg.width = jpeg.originalwidth / 2 [br]jpeg.height = jpeg.originalheight / 2 [br]jpeg.save server.mappath(""&filespath&"") & "\small_"&imgsrc&"" [br]smallimg=""&filespath&"/small_"&imgsrc&"" [br]end if [br]end if [br]'显示结果 [br]response.write("新闻中的第一张图片是:") [br]response.write("") [br]response.write("
新闻中的第一张图片的缩略图是:") [br]response.write("") [br]response.write("
新的新闻内容(图片为本地):
") [br]response.write(content) [br]response.end() [br]end if [br]%>[/p]

该文章在 2010/7/14 1:07:47 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved