本人写的整套创建静态文件类

  1. <%
  2. '-------------------------------------------------------
  3. ' ☆☆ 2006-9-12 ☆☆
  4. ' ☆☆ 全国版中要使用的创建静态页面函数类 ☆☆
  5. ' ☆☆ 宿远 ☆☆
  6. '---------------------------------------------------------
  7. '
  8.     Class clsCreateFile
  9.         Private strUrl
  10.         Private strAddress
  11.         Private strTarget
  12.         Private strAddressUrl
  13.         Private CreateFile
  14.         Private CreateFolder
  15.         Public SourceUrl                '要抓取的来源页面的链接地址实例
  16.         Public TargetAddress            '要将文件创建到的目标地址实例
  17.         Public Directory                '要将文件创建到的目标地址的目录实例
  18.         Private Sub Class_Initialize()    '类初始化
  19.             Set SourceUrl = New clsSourceUrl
  20.             Set TargetAddress = New clsTargetAddress
  21.             Set Directory = New clsDirectory
  22.         End Sub
  23.         Private Sub Class_Terminate()        '类关闭
  24.             Set SourceUrl = Nothing
  25.             Set TargetAddress = Nothing
  26.             Set Directory = Nothing
  27.         End Sub
  28.         Public Property Let Url(Value)
  29.             strUrl = Value
  30.         End Property
  31.         Public Property Get Url()
  32.             Url = strUrl
  33.         End Property
  34.         Public Property Let Address(Value)
  35.             strAddress = Value
  36.         End Property
  37.         Public Property Get Address()
  38.             Address = strAddress
  39.         End Property
  40.         Public Property Let Target(Value)
  41.             strTarget = Value
  42.         End Property
  43.         Public Property Get Target()
  44.             Target = strTarget
  45.         End Property
  46.         Public Property Let AddressURL(Value)
  47.             strAddressURL = Value
  48.         End Property
  49.         Public Property Get AddressURL()
  50.             AddressURL = strAddressURL
  51.         End Property
  52.         Public Sub Open(intCreateFile,intCreateFolder)
  53.             CreateFile = intCreateFile
  54.             CreateFolder = intCreateFolder
  55.             SourceUrl.FileUrl = strUrl
  56.             SourceUrl.FileAddress = strAddress
  57.             SourceUrl.Visit()
  58.             TargetAddress.Address = strTarget
  59.             TargetAddress.AddressURL = strAddressUrl & TargetAddress.Name()
  60.             Directory.Address = Replace(TargetAddress.Address,TargetAddress.Name,"")
  61.         End Sub
  62.         Public Function Err()
  63.             Dim Discover
  64.             IF cBool(SourceUrl.ExistsFiles()) = False Then
  65.                 strErr = "源文件不存在"
  66.                 Err = strErr
  67.                 Exit Function
  68.                 'strErrDepict = strErrDepict & IIF(strErrDepict = "","","
    ") & strErr
  69.             End IF
  70.             IF cBool(SourceUrl.CanVisit) = False Then
  71.                 strErr = "源文件访问出错"
  72.                 Err = strErr
  73.                 Exit Function
  74.                 'strErrDepict = strErrDepict & IIF(strErrDepict = "","","
    ") & strErr
  75.             End IF
  76.             IF cBool(CreateFolder) = False Then
  77.                 IF cBool(Directory.ExistsFolder()) = False Then
  78.                     strErr = "写入目录不存在"
  79.                     Err = strErr
  80.                     Exit Function
  81.                     'strErrDepict = strErrDepict & IIF(strErrDepict = "","","
    ") & strErr
  82.                 End IF
  83.             End IF
  84.             IF cBool(CreateFile) = False Then
  85.                 IF cBool(TargetAddress.ExistsFiles()) = True Then
  86.                     strErr = "目标文件已存在"
  87.                     Err = strErr
  88.                     Exit Function
  89.                     'strErrDepict = strErrDepict & IIF(strErrDepict = "","","
    ") & strErr
  90.                 End IF
  91.             End IF
  92.         End Function
  93.         Public Sub Create()
  94.             IF Err = "" Then
  95.                 IF cBool(CreateFolder)     = True Then
  96.                     Directory.CreateFolder()
  97.                 End IF
  98.                 IF cBool(CreateFile) = True Then
  99.                     TargetAddress.CreateFile SourceUrl.responseBody
  100.                 End IF
  101.             End IF
  102.         End Sub
  103.     End Class
  104.     Class clsSourceUrl
  105.         Private strUrl
  106.         Private strAddress
  107.         Private FileService
  108.         Public CanVisit
  109.         Public ReadyState
  110.         Public Status
  111.         Public responseBody
  112.         Private Sub Class_Initialize()    '类初始化
  113.             CanVisit = False
  114.             Set FileService = New clsFileService
  115.         End Sub
  116.         Private Sub Class_Terminate()        '类关闭
  117.             Set FileService = Nothing
  118.         End Sub
  119.         Public Property Let FileUrl(Value)
  120.             strUrl = Value
  121.         End Property
  122.         Public Property Get FileUrl
  123.             FileUrl = strUrl
  124.         End Property
  125.         Public Property Let FileAddress(Value)
  126.             strAddress = Value
  127.         End Property
  128.         Public Property Get FileAddress
  129.             FileAddress = strAddress
  130.         End Property
  131.         Public Function Name()
  132.             Name = FileService.FileName(strAddress,1)
  133.         End Function
  134.         Public Function ExistsFiles()
  135.             Response.Write(strAddress & "
    "
    )
  136.             ExistsFiles = FileService.ExistsFiles(strAddress)
  137.         End Function
  138.         Public Sub Visit()
  139.             FileService.VisitUrl strUrl,0
  140.             ReadyState         = FileService.ReadyState
  141.             Status             = FileService.Status
  142.             responseBody     = FileService.responseBody
  143.             IF ReadyState = 4 Then
  144.                 IF Status = 200 And Lenb(responseBody)>0 Then CanVisit = True
  145.             End IF
  146.         End Sub
  147.         Public Function FileText()
  148.             FileText = FileService.FileText(strAddress)
  149.         End Function
  150.     End Class
  151.     Class clsTargetAddress
  152.         Private strAddress
  153.         Private strAddressUrl
  154.         Private FileService
  155.         Private Sub Class_Initialize()    '类初始化
  156.             Set FileService = New clsFileService
  157.         End Sub
  158.         Private Sub Class_Terminate()        '类关闭
  159.             Set FileService = Nothing
  160.         End Sub
  161.         Public Property Let Address(Value)
  162.             strAddress = Value
  163.         End Property
  164.         Public Property Get Address()
  165.             Address = strAddress
  166.         End Property
  167.         Public Property Let AddressURL(Value)
  168.             strAddressUrl = Value
  169.         End Property
  170.         Public Property Get AddressURL()
  171.             AddressURL = strAddressUrl
  172.         End Property
  173.         Public Function Name()
  174.             Name = FileService.FileName(strAddress,1)
  175.         End Function
  176.         Public Function ExistsFiles()
  177.             ExistsFiles = FileService.ExistsFiles(strAddress)
  178.         End Function
  179.         Public Sub CreateFile(htmlBody)
  180.             FileService.CreateHtml htmlBody,Address
  181.         End Sub
  182.     End Class
  183.     Class clsDirectory
  184.         Private strAddress
  185.         Private FileService
  186.         Private Sub Class_Initialize()    '类初始化
  187.             Set FileService = New clsFileService
  188.         End Sub
  189.         Private Sub Class_Terminate()        '类关闭
  190.             Set FileService = Nothing
  191.         End Sub
  192.         Public Property Let Address(Value)
  193.             strAddress = Value
  194.         End Property
  195.         Public Property Get Address()
  196.             Address = strAddress
  197.         End Property
  198.         Public Function ExistsFolder()
  199.             ExistsFolder = FileService.ExistsFolder(strAddress)
  200.         End Function
  201.         Public Function LostFolder()
  202.             LostFolder = FileService.LostFolder(strAddress,0)
  203.         End Function
  204.         Public Sub CreateFolder()
  205.             FileService.LostFolder strAddress,1
  206.         End Sub
  207.     End Class
  208.     Class clsFileService
  209.         Private objFso
  210.         Private objXml
  211.         Public Status
  212.         Public responseBody
  213.         Public ReadyState
  214.         Public responseText
  215.         Public Sub Class_Initialize()
  216.             Set objFso = Server.CreateObject(OBJFSOID)
  217.             Set objXml = Server.CreateObject("MSXML2.XMLHTTP.3.0")
  218.         End Sub
  219.         Public Sub Class_Terminate()
  220.             Set objFso = Nothing
  221.             Set objXml = Nothing
  222.         End Sub
  223.         Public Function FileName(strAddress,intPlace)        '通过链接地址或目标地址查询文件名
  224.             IF strAddress <> "" Or Not isNULL(strAddress) Then
  225.                 strAddress = Replace(strAddress,"/","/")
  226.                 IF InStr(strAddress, "/"Then
  227.                     arrAddress = Split(strAddress,"/")
  228.                     FileName = arrAddress(UBound(arrAddress))
  229.                     IF cBool(intPlace) = False Then FileName = Replace(strAddress,FileName,"")
  230.                 Else
  231.                     FileName = strAddress
  232.                     IF cBool(intPlace) = False Then FileName = ""
  233.                 End IF
  234.             Else
  235.                 FileName = ""
  236.             End IF
  237.         End Function
  238.         Public Function ExistsFiles(strPath)
  239.             IF strPath <> "" Then
  240.                 IF objFso.FileExists(strPath) Then
  241.                     ExistsFiles = True
  242.                 Else
  243.                     ExistsFiles = False
  244.                 End IF
  245.             Else
  246.                  ExistsFiles = False
  247.             End IF
  248.         End Function
  249.         Public Function ExistsFolder(strPath)
  250.             IF strPath <> "" Then
  251.                 IF objFso.FolderExists(strPath) Then
  252.                     ExistsFolder = True
  253.                 Else
  254.                     ExistsFolder = False
  255.                 End IF
  256.             Else
  257.                  ExistsFolder = False
  258.             End IF
  259.         End Function
  260.         Public Function LostFolder(strAddress,intCreateFolder)
  261.             Dim arrAddress,strTmp
  262.             IF strAddress <> "" Then
  263.                 strTmp = Replace(Replace(strAddress,SITEMAP,""),"/","/")
  264.                 arrAddress = Split(strTmp,"/")
  265.                 For i = 0 To UBound(arrAddress) - 1
  266.                     strTmpPath = strTmpPath & arrAddress(i) & "/"
  267.                     strPathTmp    = SITEMAP & strTmpPath
  268.                     IF Not objFso.FolderExists(strPathTmp) Then
  269.                         LostFolder = arrAddress(i)
  270.                         IF cBool(intCreateFolder) = False Then
  271.                             Exit Function
  272.                         Else
  273.                             objFso.CreateFolder(strPathTmp) ' 创建
  274.                         End IF
  275.                     End If
  276.                 Next
  277.             Else
  278.                 LostFolder = ""
  279.             End IF
  280.         End Function
  281.         Public Function bytes2BSTR(vIn)
  282.             Dim strReturn
  283.             Dim i,ThisCharCode,NextCharCode
  284.             strReturn = ""
  285.             For i = 1 To LenB(vIn)
  286.                 ThisCharCode = AscB(MidB(vIn,i,1))
  287.                 IF ThisCharCode < &H80 Then
  288.                     strReturn = strReturn & Chr(ThisCharCode)
  289.                 Else
  290.                     NextCharCode = AscB(MidB(vIn,i + 1,1))
  291.                     strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
  292.                     i = i + 1
  293.                 End IF
  294.             Next
  295.             bytes2BSTR = strReturn
  296.         End Function
  297.         Public Sub VisitUrl(strUrl,intResType)
  298.             Dim oUrl,tmpPara
  299.             tmpPara = "&m="
  300.             IF InStr(strUrl,"?") = 0 Then tmpPara = "?m="
  301.             Randomize()
  302.             oUrl = strUrl & tmpPara & Rnd()
  303.             objXml.Open "Get", oUrl ,False
  304.             objXml.Send
  305.             ReadyState = objXml.ReadyState
  306.             Status = objXml.Status
  307.             Select Case intResType
  308.                 case 0
  309.                     responseBody = objXml.responseBody
  310.                 case 1
  311.                     responseBody = objXml.responseText
  312.                 case 2
  313.                     responseBody = objXml.responseXML
  314.                 case 3
  315.                     responseBody = objXml.responseStream
  316.             End Select
  317.         End Sub
  318.         Public Sub CreateHtml(xmlBody,FilePath)
  319.             Dim objStream
  320.             Set objStream = Server.CreateObject("Adodb.Stream")
  321.             objStream.Type = 1
  322.             objStream.Mode = 3
  323.             objStream.Open()
  324.             objStream.Write xmlBody
  325.             objStream.Position = 0
  326.             objStream.Type = 2
  327.             objStream.Charset = "GB2312"
  328.             objStream.SaveToFile FilePath,2
  329.             objStream.Close()
  330.             Set objStream = Nothing
  331.         End Sub
  332.         Public Function FileText(FilePath)
  333.             IF ExistsFiles(FilePath) Then
  334.                 Set objFileOpen = objFso.OpenTextFile(FilePath,1)
  335.                 FileText = objFileOpen.ReadAll
  336.                 Set objFileOpen = Nothing
  337.             Else
  338.                 FileText = ""
  339.             End IF
  340.         End Function
  341.     End Class
  342.     Dim objCreateFile
  343.     Set objCreateFile = New clsCreateFile
  344.         objCreateFile.Url         = "http://sh.studyget.com/Template/Column_List/GRE_Datum_1.asp?ThirdID=1281&TopNum=2"
  345.         objCreateFile.Address     = "//192.168.1.254/www.studyget.com/Garrison/201226/Template/Column_List/GRE_Datum_1.asp"
  346.         objCreateFile.Target     = "//192.168.1.254/www.studyget.com/00/01/00.html"
  347.         objCreateFile.AddressUrl = "http://www.studyget.com/00/01/"
  348.         objCreateFile.Open 1,1
  349.             Response.Write("

    <------------------------------------------------------------>
    "
    )
  350.             Response.Write("
    SourceUrl.FileUrl: "
     & objCreateFile.SourceUrl.FileUrl)
  351.             Response.Write("
    SourceUrl.FileAddress: "
     & objCreateFile.SourceUrl.FileAddress)
  352.             Response.Write("
    SourceUrl.Name(): "
     & objCreateFile.SourceUrl.Name())
  353.             Response.Write("
    SourceUrl.ExistsFiles(): "
     & objCreateFile.SourceUrl.ExistsFiles())
  354.             Response.Write("

    <------------------------------------------------------------>
    "
    )
  355.             Response.Write("
    TargetAddress.Address:  "
     & objCreateFile.TargetAddress.Address)
  356.             Response.Write("
    TargetAddress.AddressUrl:  "
     & objCreateFile.TargetAddress.AddressUrl)
  357.             Response.Write("
    TargetAddress.Name():  "
     & objCreateFile.TargetAddress.Name())
  358.             Response.Write("
    TargetAddress.ExistsFiles():  "
     & objCreateFile.TargetAddress.ExistsFiles())
  359.             Response.Write("

    <------------------------------------------------------------>
    "
    )
  360.             Response.Write("
    Directory.Address: "
     & objCreateFile.Directory.Address)
  361.             Response.Write("
    Directory.ExistsFolder: "
     & objCreateFile.Directory.ExistsFolder)
  362.             Response.Write("
    Directory.LostFolder: "
     & objCreateFile.Directory.LostFolder)
  363.         IF objCreateFile.Err <> "" Then
  364.             Response.Write(objCreateFile.Err)
  365.         Else
  366.             objCreateFile.Create()
  367.         End IF
  368.     Set objCreateFile = Nothing
  369. %>
 

你可能感兴趣的:(Asp,url)