设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间01 现在时间01 缓存数据 当你走完一段之后回头看,你会发现,那些真正能被记得的事真的是没有多少,真正无法忘记的人屈指可数,真正有趣的日子不过是那么一些,而真正需要害怕的也是寥寥无几。

当你走完一段之后回头看,你会发现,那些真正能被记得的事真的是没有多少,真正无法忘记的人屈指可数,真正有趣的日子不过是那么一些,而真正需要害怕的也是寥寥无几。

查看: 1145|回复: 3

直接保存URL图像或网页到服务器本地的类

[复制链接]

  离线 

TA的专栏

  • 打卡等级:热心大叔
  • 打卡总天数:234
  • 打卡月天数:0
  • 打卡总奖励:3524
  • 最近打卡:2025-11-26 11:23:27
等级头衔

等級:晓枫资讯-上等兵

在线时间
0 小时

积分成就
威望
0
贡献
443
主题
396
精华
0
金钱
4818
积分
917
注册时间
2023-1-22
最后登录
2025-11-26

发表于 2023-2-14 16:30:21 | 显示全部楼层 |阅读模式

<% @ LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
Option Explicit

Class BoxInfoImg
    '传输类的使用方法
    '图象上传和上传信息获取CLASS

    '用法:
    'dim imgUp
    'set imgUp=new BoxInfoImg

    '属性: 
    'imgUp.width    '宽
    'imgUp.height    '高
    'imgUp.imgSize    '大小
    'imgUp.imgType    '类型
    'imgUp.imgName    '文件名
    'imgUp.imgName '图像文件名:"&
    'imgUp.filename '文件名"&
    'imgUp.extName '扩展名"
    'imgUp.DiskPath '保存位置"
    'imgUp.XuPath '虚拟路径"
    'imgUp.NewUrl '保存后url"
    'imgUp.SaveMode '保存后url"

    '方法:
    'imgUp.saveImg(fullpath)    '保存图像文件

    dim ADOS
    dim width,height,imgSize,imgType,imgName,fileName
    dim preName,extName
    dim SavePath,SaveName,SaveMode
    dim DiskPath,XuPath,NewUrl
    dim textStr
    dim i

    Private Sub Class_Initialize
        set ADOS=Server.CreateObject("Adodb.Stream")
            ADOS.Type=1 
            ADOS.Mode=3 
            ADOS.Open 
            getImageSize
    End Sub

    Private Sub Class_Terminate
        ADOS.close
        set ADOS=nothing
    End Sub

    Public Function getImageSize() 

            dim ret(3),bFlag,fdata,fsize

            fdata=GetWebData(GetStrUrl) '取得XmlHttp数据
            fsize=clng(lenb(fdata))        '取得数据尺寸

            
            if fsize=0 then 
                exit function 
                R_write "无有效数据保存",0
            end if

            ADOS.Write fdata    
            ADOS.Position=0

            SaveName=iSaveName
            SavePath=iSavePath
            SaveMode=iSaveMode

            '写文本对象读取图像长宽和类型

            ADOS.Position=0 '重置数据开始位置 
            bFlag=ADOS.read(3)

            if isNull(bFlag) then 
                width=0
                height=0
                imgSize=0
                imgType="unknow"
                ret(0)=imgType:ret(1)=width:ret(2)=height:ret(3)=""
                getimagesize=ret
                exit function
            end if

            '取文件类型和长宽
            select case hex(binVal(bFlag))
            case "4E5089":
                ADOS.read(15)
                ret(0)="png"
                ret(1)=BinVal2(ADOS.read(2))
                ADOS.read(2)
                ret(2)=BinVal2(ADOS.read(2))
            case "464947":
                ADOS.read(3)
                ret(0)="gif"
                ret(1)=BinVal(ADOS.read(2))
                ret(2)=BinVal(ADOS.read(2))
            case "FFD8FF":
                dim p1
                do 
                do: p1=binVal(ADOS.Read(1)): loop while p1=255 and not ADOS.EOS
                if p1>191 and p1<196 then exit do else ADOS.read(binval2(ADOS.Read(2))-2)
                do:p1=binVal(ADOS.Read(1)):loop while p1<255 and not ADOS.EOS
            loop while true
                ADOS.Read(3)
                ret(0)="jpg"
                ret(2)=binval2(ADOS.Read(2))
                ret(1)=binval2(ADOS.Read(2))
            case else:
                if left(Bin2Str(bFlag),2)="BM" then
                    ADOS.Read(15)
                    ret(0)="bmp"
                    ret(1)=binval(ADOS.Read(4))
                    ret(2)=binval(ADOS.Read(4))
                else
                    ret(0)=""
                end if
            end select
            '
            dim tempStr
            dim nameStr
            dim defaultName
            dim ln
            tempStr=split(GetStrUrl,"/")
            nameStr=tempStr(ubound(tempStr))
            if nameStr="" then
                r_write "错误的URL,请输入可访问的URL",0
                exit function
            end if
            fileName=split(nameStr,"?")(0)
            ln=inStrRev(fileName,".")
            if ln>0 then 
                preName=left(fileName,inStrRev(fileName,".")-1)
            else
                preName=fileName
            end if
            'R_write fileName,1
            'R_write inStrRev(fileName,"."),1
            'R_write fileName,0
            extName=right(fileName,len(fileName)-inStrRev(fileName,"."))

            Select case ret(0)
            case "png","jpg","bmp","gif","swf"
                width=ret(1)
                height=ret(2)
                imgSize=fsize
                imgType=ret(0)
                imgName=preName&"."&ret(0)
            case else
                width=0
                height=0
                imgSize=fsize
                imgName="unknow"
                imgType=".unknow"
            end select

            if SaveMode="1" then
                defaultName=imgName
                if SaveName="" then 
                    SaveName=defaultName
                else
                    if lcase(right(SaveName,4))<>"."&imgType then
                        SaveName=SaveName&"."&imgType
                    end if
                end if
            else
                defaultName=filename
            end if
            if SaveName="" then SaveName=defaultName
            SavePath=replace(SavePath,"//","/")
            if right(SavePath,1)<>"/" then SavePath=SavePath&"/"
            if SavePath="" then SavePath="./"
                DiskPath=server.mappath(SavePath&SaveName)
                XuPath=replace(replace(DiskPath,server.mappath("/"),""),"\","/")
            NewUrl="http://"&Request.ServerVariables("SERVER_NAME")&XuPath

            getimagesize=ret
    End Function

    Public function SaveImg(FullPath)
        SaveImg=false
        if SaveMode="1" then
            if trim(fullpath)="" or _
                width=0 or _ 
                height=0 or _
                imgSize=0 or _
                imgType=".unknow" then exit function end if
        end if
        ADOS.Position=0
        if SaveMode="2" then
            ADOS.Type=2
            ADOS.Charset ="gb2312"
            ADOS.SaveToFile FullPath,2
            textStr=ADOS.readtext()
        else
            ADOS.SaveToFile FullPath,2
        end if
        SaveImg=true
    End function

    Private Function Bin2Str(Bin)
        Dim I,Str,clow
        For I=1 to LenB(Bin)
            clow=MidB(Bin,I,1)
        if ASCB(clow)<128 then
            Str = Str & Chr(ASCB(clow))
        else
            I=I+1
            if I <= LenB(Bin) then Str = Str & Chr(ASCW(MidB(Bin,I,1)&clow))
        end if
        Next 
            Bin2Str = Str
    End Function

    Private Function Num2Str(num,base,lens)
        dim ret:ret = ""
        while(num>=base)
            ret=(num mod base) & ret
            num=(num - num mod base)/base
        wend
            Num2Str = right(string(lens,"0") & num & ret,lens)
    End Function

    Private Function Str2Num(str,base)
        dim ret:ret = 0
        for i=1 to len(str)
            ret = ret *base + cint(mid(str,i,1))
        next
            Str2Num=ret
    End Function

    Private Function BinVal(bin)
        dim ret:ret = 0
        for i = lenb(bin) to 1 step -1
            ret = ret *256 + ascb(midb(bin,i,1))
        next
            BinVal=ret
    End Function

    Private Function BinVal2(bin)
        dim ret:ret = 0
        for i = 1 to lenb(bin)
            ret = ret *256 + ascb(midb(bin,i,1))
        next
            BinVal2=ret
    End Function

    Private    Function GetWebData(byval StrUrl)
        if StrUrl="" then 
            r_write "无效",1
            exit function
        end if
        dim tempStr
        tempStr=split(GetStrUrl,"/")
        if tempStr(ubound(tempStr))="" or inStr(StrUrl,"/")=0 then
            R_Write "未指定有效的URL",0
            exit function
        end if
        dim Retrieval
        Set Retrieval = Server.CreateObject("Microsoft.XMLHTTP")
        With Retrieval
        .Open "Get", StrUrl, False, "", ""
        .Send
        GetWebData =.ResponseBody
        End With
        Set Retrieval = Nothing
    End Function            

End Class
%>
<%
SUB saveUpload(GetUrl,SavePath,SaveName,mode)
    dim chkInfo

    if GetUrl="" then 
        call tform()
        R_Write "
传输文件栏没有填写!",0
    end if

    set imgUp=new BoxInfoImg

    if mode="1" and imgUp.imgName="unknow" then
        call tform()
        set imgUp=nothing
        R_Write "
传输文件栏没有填写有效的图像URL!",0
    end if

    chkInfo=""
    dim i,testStr,showStr
    '限定格式
    select case imgUp.imgType
    case "png","jpg","bmp","gif"
        if imgUp.width=0 or imgUp.height=0 or imgUp.imgSize=0 then 
            chkInfo="
  • "+"传输图像数据不存在,请确定你的URL是否正确"
            end if
        case else 
            chkInfo="
  • 无效的传输格式,允许图像数据格式为 ""png"",""jpg"",""bmp"",""gif""
  • "
        end select

        'R_Write SavePath,1
        'R_Write mode,1
        'R_Write imgUp.imgName,1
        'R_Write imgUp.filename,1
        'R_Write "SaveName="&SaveName,1

        if mode="1" and chkInfo<>"" then '检查上传图像数据合格后,则保存之
                call tform()
                R_Write chkInfo,0
        else
            Server.ScriptTimeOut=5000
            imgUp.saveImg imgUp.DiskPath     
        end if
    '-------------
                R_write "===处理结果部分资料===
    ",1
                R_write "  宽:"&imgUp.width&" pix",1
                R_write "  高:"&imgUp.height&" pix",1
                R_write " 大小:"&formatnumber(imgUp.imgSize/1024,2,-1)&" KB",1
                R_write " 格式:"&imgUp.imgType,1
                R_write "图像文件名:"&imgUp.imgName,1
                R_write "文件名:"&imgUp.filename,1
                R_write "扩展名:"&imgUp.extName,1
                R_write "保存位置:"&imgUp.DiskPath,1
                R_write "虚拟路径:"&imgUp.XuPath,1
                R_write "保存后url:"&imgUp.NewUrl,1
            call tform()
            set imgUp=nothing 
                R_write "------------------------
    传输完毕",0
    End SUB

    SUB tform()
    %>

     获取 URL:

     保存路径:

    保存文件名:

     保存类型:
    > Web图像 
    > 文本文件
    > 二进制数据
       


    <%
    if GetStrUrl<>"" then
        if iSaveMode="2" then
            R_write "Run this code",1
            R_write ""&server.htmlencode(imgUp.textStr)&"",1
        else
             R_write "",1
        end if
    end if
    %>



    如果保存为图像,不要加扩展名,自动识别加上,如果加的扩展名不合也回自动加上

    保存文件路径为空则保存在当前路径

    保存文件名为空则使用自动识别取得的文件名

    保存为其他任意方式,对asp html 等为取得发送结果的Html
    <%End SUB

    Sub R_write(str,num)
        dim istr:istr=str
        dim inum:inum=num
        response.write str&"
    "
        if inum=0 then response.end
    end sub

    '=================调用过程 Execute========================
    %>



     New Document 







    <%
    dim imgUp        '传输对象
    dim GetStrUrl    '要获取的图像或网页URL
    dim iSaveName    '要保存的名字
    dim iSavePath    '要保存的虚拟路径
    dim iSaveMode    '保存的模式 1 为图像 0 为任意文件
        iSavePath=trim(request.form("SavePath"))
        iSaveName=trim(request.form("SaveName"))
        GetStrUrl=trim(request.form("GetStrUrl"))
        iSaveMode=trim(request.form("SaveMode"))
    if GetStrUrl<>"" then
        CALL saveUpload(GetStrUrl,iSavePath,iSaveName,iSaveMode)
        call tform()
    else
        call tform()
    end if
    %>



    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
    晓枫资讯-科技资讯社区-免责声明
    免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
          1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
          2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
          3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
          4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
          5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
    http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

      离线 

    TA的专栏

    等级头衔

    等級:晓枫资讯-列兵

    在线时间
    0 小时

    积分成就
    威望
    0
    贡献
    0
    主题
    0
    精华
    0
    金钱
    14
    积分
    8
    注册时间
    2022-12-27
    最后登录
    2022-12-27

    发表于 2025-1-9 11:32:54 | 显示全部楼层
    感谢楼主分享。
    http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

      离线 

    TA的专栏

    等级头衔

    等級:晓枫资讯-列兵

    在线时间
    0 小时

    积分成就
    威望
    0
    贡献
    0
    主题
    0
    精华
    0
    金钱
    20
    积分
    20
    注册时间
    2022-12-26
    最后登录
    2022-12-26

    发表于 2025-10-27 22:16:25 | 显示全部楼层
    路过,支持一下
    http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

      离线 

    TA的专栏

    等级头衔

    等級:晓枫资讯-列兵

    在线时间
    0 小时

    积分成就
    威望
    0
    贡献
    0
    主题
    0
    精华
    0
    金钱
    20
    积分
    20
    注册时间
    2022-12-25
    最后登录
    2022-12-25

    发表于 4 天前 | 显示全部楼层
    感谢楼主,顶。
    http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
    严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    1楼
    2楼
    3楼
    4楼

    手机版|晓枫资讯--科技资讯社区 本站已运行

    CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

    晓枫资讯--科技资讯社区

    本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

    如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

    Powered by Discuz! X3.5

    快速回复 返回顶部 返回列表