seo中的编码问题

来源:互联网 发布:淘宝红包密令哪里领取 编辑:程序博客网 时间:2024/06/03 11:18

CodePage = 65001 utf-8

ASP中创建和读取UTF-8编码格式文件
2008/07/08 00:05

ASP中IO操作读写文件一般有2种方法:一是FSO对象,二是ADODB.STREAM对象。

用FSO读取和创建文件比较常用,但是要生成UTF-8编码的文件,没测试成功。通过object.CreateTextFile(filename[, overwrite[, unicode]])方法生成的文件编码可以为unicode,虽然文件可以正常浏览查看,但搜索引擎抓取到的却是乱码。下面我们来看看UTF-8与UNICODE的区别。

各种编码的定义:
1.字符编码、内码、汉字编码
      ASCII、GB2312、GBK、GB18030向下兼容,即同一个字符在这些方案中总是有相同的编码,后面的标准支持更多的字符。

2.Unicode、UCS、UTF
      Unicode只与ASCII兼容,与GB码不兼容。Unicode也是一种字符编码方式,不过它是由国际组织设计,可以容纳全世界所有语言文字的编码方案。其中UCS规定怎么用多个字节表示各种文字,UTF则规定怎么传输这些编码。现在一般使用UCS-2,即用两个字节进行编码。而所说的UFT-8就是以8位为单元对UCS进行编码。

3.cp936是什么?
      Unicode统一了编码,但是如何兼容各国的文字编码就需要用到codepage,codepage就是各国的文字编码和Unicode之间的映射表,比如简体中文和Unicode的映射表就是CP936。
      从936中随意取一行,例如:
                          0x9993 0x6ABD #CJK UNIFIED IDEOGRAPH
      前面的编码是GBK的编码,后面的是Unicode,通过查这张表,就能简单的实现GBK和Unicode之间的转换。


这时要创建UTF-8编码的文件,可使用ADODB.STREAM对象。下面提供2个函数,分别用于读和写:

  1. function LoadContent(path,usefso)
  2. if usefso then
  3. Set fs = CreateObject("Scripting.FileSystemObject")
  4. set f=fs.OpenTextFile(path)
  5. strContent=f.ReadAll
  6. f.close:set f=nothing
  7. set fs=nothing 
  8. else
  9. on error resume next
  10. set stream=Server.CreateObject("ADODB.Stream")
  11. If Err.Number=-2147221005 Then 
  12.    Response.Write "<div align='center'>不支持ADODB.Stream</div>"
  13.    Err.Clear
  14.    Response.End
  15.    End If
  16. with stream
  17.    .type=2
  18.    .mode=3
  19.    .Open
  20.    .LoadFromFile path
  21.    .Charset = "GB2312"
  22.          .Position = 2 
  23.    strContent=.ReadText
  24. end with
  25. stream.close:stream=nothing
  26. end if
  27. 'LoadContent=strContent
  28. 'response.write strContent
  29. LoadContent=strContent
  30. end function
  31. function WriteContent(path,content,overrite,usefso)
  32. if usefso=true then
  33. Set fs = CreateObject("Scripting.FileSystemObject")
  34. if overrite=true then
  35. set f=fs.CreateTextFile(path,overrite)
  36. else
  37. set f=fs.OpenTextFile(path,8)
  38. end if
  39. f.writeLine(content)
  40. f.close:set f=nothing
  41. set fs=nothing
  42. else 
  43. On Error Resume Next
  44.     Set objStream = Server.CreateObject("ADODB.Stream")
  45. If Err.Number=-2147221005 Then 
  46. Response.Write "<div align='center'>不支持ADODB.Stream</div>"
  47. Err.Clear
  48. Response.End
  49. End If
  50. With objStream
  51.         .Type = 2
  52.         .Open
  53.         .Charset = "utf-8"   '这里指定编码格式
  54.         .Position = objStream.Size
  55.         .WriteText = content
  56. 'if override=true then
  57.    .SaveToFile path,2
  58. 'else
  59. ' .SaveToFile path,1
  60. 'end if
  61.         .Close
  62.     End With
  63.     Set objStream = Nothing
  64. end if
  65. end function
    1. <%   
    2.   dim   mytxt,fname,fpath   
    3.   fname="1.txt"   
    4.     '.表示当前目录   
    5.   fpath=Server.MapPath(".")   
    6.   set   fso=server.createobject("scripting.filesystemobject")   
    7.   '参数说明:第一个true表示覆盖已有同名文件,否则为不覆盖;   
    8.   '第个false表示创建格式为ASCII格式,如果true则为Unicode   
    9.   set   mytxt=fso.createtextfile(fPath   &   "/"   &   fname,true,false)   
    10.   mytxt.write   "此处为写入1.txt中的内容"&chr(13)   
    11.   mytxt.close   
    12.   response.write   "当前目录下建立新文件1.txt并写入成功<br>"   
    13.   set   mytxt=nothing   
    14.   set   fso=nothing   
    15.   %>   
    16.     
    17.   <%   
    18.   fname="1.txt"   
    19.   fpath=Server.MapPath(".")   
    20.   set   fso=server.createobject("scripting.filesystemobject")   
    21.   set   mytxt=fso.opentextfile(fPath   &   "/"   &   fname,8,true,-2)   
    22.   '参数说明:8读取+追加(1读取,2写入),true文件不存在时创建新文件,-2系统默认格式ASCII(0为ASCII   
    23.   '-1为Unicode格式   
    24.   response.write   "打开指定文件1.txt成功<br>"   
    25.   mytxt.write(request.servervariables("remote_host"))   
    26.   mytxt.close   
    27.   set   mytxt=nothing   
    28.   set   fso=nothing   
    29.   response.write   "写入客户端ip到指定文件1.txt成功<br>"   
    30.   %>

原创粉丝点击