可下载目标网页上的所有图片代码

<%
参数设置开始
url =  request("url")
localaddr = server.mappath("pp/") 保存到本地的目录
localdir = "aa/" http 访问的相对路径
allowfileext = "jpg|bmp|png|gif|swf" 支持的文件名格式
参数设置完毕

if createdir(localaddr) = false then
response.write "创建目录失败,请检查目录权限"
response.end
end if
response.write convert2localaddr(url,localaddr,localdir)

function convert2localaddr(url,localaddr,localdir)
  参数说明
  url 页面地址
  localaddr 保存本地的物理地址
  localdir 相对路径
strcontent = gethttppage(url)
set objregexp = new regexp
objregexp.ignorecase = true
objregexp.global = true
objregexp.pattern = "<img.+?>"
set matches =objregexp.execute(strcontent)
for each match in matches
  retstr = retstr & getremoteimages(match.value)
next
imagesarray=split(retstr,"||")
remoteimage=""
localimage=""
for i=1 to ubound(imagesarray)
  if imagesarray(i)<>"" and instr(remoteimage,imagesarray(i))<1 then
   fname=baseurl&cstr(i&mid(imagesarray(i),instrrev(imagesarray(i),".")))
   imagesfilename = imagesarray(i)
   allowfileextarray = split(allowfileext,"|")
   isgetfile = false
   for tmp = 0 to ubound(allowfileextarray)
    if lcase(getfileext(imagesfilename)) = allowfileextarray(tmp) then
     isgetfile=true
    end if
   next
   if isgetfile = true then
    newfilename =  generaterandomfilename(fname)
    call save2local(imagesfilename,localaddr & "/" & newfilename)
    remoteimage=remoteimage&"||"& imagesfilename
    localimage=localimage&"||" & localdir & newfilename
   end if
  end if
next
arrnew=split(localimage,"||")
arrall=split(remoteimage,"||")
for i=1 to ubound(arrnew)
  strcontent=replace(strcontent,arrall(i),arrnew(i))
next
convert2localaddr = strcontent
end function

function getremoteimages(str)
set objregexp1 = new regexp
objregexp1.ignorecase = true
objregexp1.global = true
objregexp1.pattern = "http://.+? "
set mm=objregexp1.execute(str)
for each match1 in mm
  tmpaddr = left(match1.value,len(match1.value)-1)
  getremoteimages=getremoteimages&"||" & replace(replace(tmpaddr,"""",""),"","")
next
end function

function gethttppage(url)
on error resume next
dim http
set http=server.createobject("msxml2.xmlhttp")
http.open "get",url,false
http.send()
if http.readystate<>4 then exit function
gethttppage=bytes2bstr(http.responsebody)
set http=nothing
if err.number<>0 then err.clear 
end function

function bytes2bstr(vin)
dim strreturn
dim i,thischarcode,nextcharcode
strreturn = ""
for i = 1 to lenb(vin)
  thischarcode = ascb(midb(vin,i,1))
  if thischarcode < &h80 then
   strreturn = strreturn & chr(thischarcode)
  else
   nextcharcode = ascb(midb(vin,i+1,1))
   strreturn = strreturn & chr(clng(thischarcode) * &h100 + cint(nextcharcode))
   i = i + 1
  end if
next
bytes2bstr = strreturn
end function

function gethttpimg(url)
on error resume next
dim http
set http=server.createobject("msxml2.xmlhttp")
http.open "get",url,false
http.send()
if http.readystate<>4 then  exit function
gethttpimg=http.responsebody
set http=nothing
if err.number<>0 then err.clear
end function

function save2local(from,tofile)
dim geturl,objstream,imgs
geturl=trim(from)
imgs=gethttpimg(geturl)
set objstream = server.createobject("adodb.stream")
objstream.type =1
objstream.open
objstream.write imgs
objstream.savetofile tofile,2
objstream.close()
set objstream=nothing
end function

function geturlencodel(byval url)中文文件名转换
dim i,code
geturlencodel=""
if trim(url)="" then exit function
for i=1 to len(url)
  code=asc(mid(url,i,1))
  if code<0 then code = code + 65536
  if code>255 then
   geturlencodel=geturlencodel&"%"&left(hex(code),2)&"%"&right(hex(code),2)
  else
   geturlencodel=geturlencodel&mid(url,i,1)
  end if
next
end function

function generaterandomfilename(byval szfilename) 根据原文件名,自动以日期yyyy-mm-dd-random格式生成新文件名
    randomize
    rannum = int(90000 * rnd) + 10000
    if month(now) < 10 then c_month = "0" & month(now) else c_month = month(now)
    if day(now) < 10 then c_day = "0" & day(now) else c_day = day(now)
    if hour(now) < 10 then c_hour = "0" & hour(now) else c_hour = hour(now)
    if minute(now) < 10 then c_minute = "0" & minute(now) else c_minute = minute(now)
    if second(now) < 10 then c_second = "0" & second(now) else c_second = minute(now)
    fileext_a = split(szfilename, ".")
    fileext = lcase(fileext_a(ubound(fileext_a)))
    generaterandomfilename = year(now) & c_month & c_day & c_hour & c_minute & c_second & "_" & rannum & "." & fileext
end function

function createdir(byval localpath) 建立目录的程序,如果有多级目录,则一级一级的创建
    on error resume next
    localpath = replace(localpath, "/", "/")
    set fileobject = server.createobject("scripting.filesystemobject")
    patharr = split(localpath, "/")
    path_level = ubound(patharr)
    for i = 0 to path_level
        if i = 0 then pathtmp = patharr(0) & "/" else pathtmp = pathtmp & patharr(i) & "/"
        cpath = left(pathtmp, len(pathtmp) - 1)
        if not fileobject.folderexists(cpath) then fileobject.createfolder cpath
    next
    set fileobject = nothing
    if err.number <> 0 then
        createdir = false
        err.clear
    else
        createdir = true
    end if
end function

function getfileext(byval filename)
fileext_a=split(filename,".")
getfileext=lcase(fileext_a(ubound(fileext_a)))
end function
%>

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值