当前位置: 移动技术网 > IT编程>开发语言>Asp > ASP替换、保存远程图片实现代码

ASP替换、保存远程图片实现代码

2017年12月08日  | 移动技术网IT编程  | 我要评论

asp通过函数来实现替换、保存远程图片,完成自动采集图片、提取图片的功能,函数中自动判断重复图片,智能分析链接路径,并转成成相对的图片地址保存在你指定的网站目录中,我们可将此函数用在后台的编辑器中,当你复制了含有图片的内容后,本代码会自动帮你上传图片。同时本代码也是采集程序中的重要处理函数,函数代码如下:

function replacesaveremotefile(constr,strinstalldir,strchanneldir,savetf,tisturl)
if constr="$false$" or constr="" or strinstalldir="" or strchanneldir="" then
replacesaveremotefile=constr
exit function
end if
dim tempstr,tempstr2,tempstr3,re,matches,match,tempi,temparray,temparray2
set re = new regexp
re.ignorecase = true
re.global = true
re.pattern ="]>"
set matches =re.execute(constr)
for each match in matches
if tempstr<>"" then
tempstr=tempstr & "$array$" & match.value
else
tempstr=match.value
end if
next
if tempstr<>"" then
temparray=split(tempstr,"$array$")
tempstr=""
for tempi=0 to ubound(temparray)
re.pattern ="src\s*=\s*.+?\.(gif|jpg|bmp|jpeg|psd|png|svg|dxf|wmf|tiff)"
set matches =re.execute(temparray(tempi))
for each match in matches
if tempstr<>"" then
tempstr=tempstr & "$array$" & match.value
else
tempstr=match.value
end if
next
next
end if
if tempstr<>"" then
re.pattern ="src\s*=\s*"
tempstr=re.replace(tempstr,"")
end if
set matches=nothing
set re=nothing
if tempstr="" or isnull(tempstr)=true then
replacesaveremotefile=constr
exit function
end if
tempstr=replace(tempstr,"""","")
tempstr=replace(tempstr,"'","")
tempstr=replace(tempstr," ","")
dim remotefileurl,savepath,pathtemp,dtnow,strfilename,strfiletype,arrsavefilename,rannum,arr_path
dtnow=now()
if savetf=true then
savepath= strchanneldir & "/" & year(dtnow) & right("0" & month(dtnow),2) & "/"
  response.write "链接路径:" & savepath & ""
arr_path=split(savepath,"/")
pathtemp=""
for tempi=0 to ubound(arr_path)
if tempi=0 then
pathtemp=arr_path(0) & "/"
elseif tempi=ubound(arr_path) then
exit for
else
pathtemp=pathtemp & arr_path(tempi) & "/"
end if
if checkdir(pathtemp)=false then
if makenewsdir(pathtemp)=false then
savetf=false
exit for
end if
end if
next
end if
'去掉重复图片
temparray=split(tempstr,"$array$")
tempstr=""
for tempi=0 to ubound(temparray)
if instr(lcase(tempstr),lcase(temparray(tempi)))<1 then
tempstr=tempstr & "$array$" & temparray(tempi)
end if
next
tempstr=right(tempstr,len(tempstr)-7)
temparray=split(tempstr,"$array$")
'转换相对图片地址
tempstr=""
for tempi=0 to ubound(temparray)
tempstr=tempstr & "$array$" & definiteurl(temparray(tempi),tisturl)
next
tempstr=right(tempstr,len(tempstr)-7)
tempstr=replace(tempstr,chr(0),"")
temparray2=split(tempstr,"$array$")
tempstr=""
'图片替换/保存
set re = new regexp
re.ignorecase = true
re.global = true
for tempi=0 to ubound(temparray2)
remotefileurl=temparray2(tempi)
if remotefileurl<>"$false$" and savetf=true then'保存图片
arrsavefilename = split(remotefileurl,".")
  strfiletype=lcase(arrsavefilename(ubound(arrsavefilename)))'文件类型
if strfiletype="asp" or strfiletype="asa" or strfiletype="aspx" or strfiletype="cer" or strfiletype="cdx" or strfiletype="exe" or strfiletype="rar" or strfiletype="zip" then
uploadfiles=""
replacesaveremotefile=constr
exit function
end if

randomize
rannum=int(900*rnd)+100
  strfilename = year(dtnow) & right("0" & month(dtnow),2) & right("0" & day(dtnow),2) & right("0" & hour(dtnow),2) & right("0" & minute(dtnow),2) & right("0" & second(dtnow),2) & rannum & "." & strfiletype
re.pattern =temparray(tempi)
  if saveremotefile(savepath & strfilename,remotefileurl)=true then
'********************************
pathtemp=savepath & strfilename
constr=re.replace(constr,pathtemp)
re.pattern=strinstalldir & strchanneldir & "/"
uploadfiles=uploadfiles & "|" & re.replace(savepath &strfilename,"")
else
pathtemp=remotefileurl
constr=re.replace(constr,pathtemp)
'uploadfiles=uploadfiles & "|" & remotefileurl
end if
elseif remotefileurl<>"$false$" and savetf=false then'不保存图片
re.pattern =temparray(tempi)
constr=re.replace(constr,remotefileurl)
uploadfiles=uploadfiles & "|" & remotefileurl
end if
next
set re=nothing
if uploadfiles<>"" then
uploadfiles=right(uploadfiles,len(uploadfiles)-1)
end if
replacesaveremotefile=constr
end function

函数参数说明:
constr:要替换的字符串
参 数:savetf:是否保存文件,false不保存,true保存
参 数: tisturl:当前网页地址

以上就是asp替换、保存远程图片函数代码,希望对大家的学习有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网