欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ReSaveRemoteFile函数之asp实现查找文件保存替换的代码

程序员文章站 2022-05-03 13:08:14
'================================================     '函数名:resaver...
'================================================
    '函数名:resaveremotefile
    '作  用:查找文件保存替换
    '参  数:str   ----原字符串
    '参  数:url   ----当然网站url
    '参  数:dir -----保存目录
    '参  数:insave ------是否保存,true,false
    '返回值:格式化取后的字符串
    '================================================
    public function resaveremotefile(byval str, byval url, byval dir,insave)
        dim s_content
        dim re
        dim contentfile, contentfileurl
        dim strtempurl,strfileurl,dirtemp,pathtemp,filetemp,tempi,tempurlarray,arr_path
        dim sallowextname
        sallowextname="rm|swf"

        s_content = str
        on error resume next
        set re = new regexp
        re.ignorecase = true
        re.global = true
        re.pattern = "((src=|href=)((\s)+[.]{1}(" & sallowextname & ")))"
        set contentfile = re.execute(s_content)
        dim scontenturl(), n, i, brepeat
        n = 0
        for each contentfileurl in contentfile
            strfileurl = replace(replace(replace(replace(contentfileurl.value, "src=", "", 1, -1, 1), "href=", "", 1, -1, 1), "'", ""), chr(34), "")
            if n = 0 then
                n = n + 1
                redim scontenturl(n)
                scontenturl(n) = strfileurl
            else
                brepeat = false
                for i = 1 to ubound(scontenturl)
                    if ucase(strfileurl) = ucase(scontenturl(i)) then
                        brepeat = true
                        exit for
                    end if
                next
                if brepeat = false then
                    n = n + 1
                    redim preserve scontenturl(n)
                    scontenturl(n) = strfileurl
                end if
            end if
        next
        if n = 0 then
            resaveremotefile = s_content
            exit function
        end if
        for i = 1 to n 
            strtempurl = scontenturl(i) : strtempurl = formatremoteurl(strtempurl,url)'得到文件地址
            response.write(strtempurl)
            if insave=true then
                arr_path=split(dir,"/")
                '----------建目录-----------------------
                  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
                 '------------------------------------------------------
                tempurlarray=split(strtempurl,"/")
                '----------检查文件是否存在.如果存在换文件名------------------
                do while true 
                    filetemp=dir &  makerandom(5) & tempurlarray(ubound(tempurlarray))'生成随机文件名
                    if checkfile(filetemp)=false then
                        exit do
                    end if
                loop 
                '-------------------------------------------------------------------
                response.write(filetemp)
                if saveremotefile(filetemp,strtempurl)=true then
                    response.write("保存成功")&"<br>"
                    s_content = replace(s_content,scontenturl(i),filetemp, 1, -1, 1)'替换地址    
                else
                    response.write("保存失败")&"<br>"
                end if
            else
                s_content = replace(s_content,scontenturl(i),strtempurl, 1, -1, 1)'替换地址        
            end if    
        next
        set re = nothing
        pictureexist = true
        resaveremotefile = s_content
        exit function
    end function