主页 > 编程资料 > Delphi >
发布时间:2015-09-22 作者:网络 阅读:179次

如果没有文件的头信息,那么需要直接通过字符串进行判断,代码通过IsUTF8String这个函数进行判断,最终得出结果。

所有的代码


procedure TForm1.Button1Click(Sender: TObject);

var

  mstream: TMemoryStream;

  ustream: TStringStream;

  astream: TStringStream;

  stream: TStringStream;

  idHttpObj: TIdHTTP;

  ssss: TStringList;

  url: sTring;

  ss: string;

  rs: RawByteString;

  aByte: Byte;

  isFoundCode: Boolean;

begin

  url := 'http://www.dfwlt.com'; // UTF8

  mstream := TMemoryStream.Create;

  try

     idHttpObj := TIdHTTP.Create(nil);

      try

        Memo1.Lines.Clear;

        Memo1.Lines.add(url);

        Memo1.Lines.add('正在读取请稍后...');

        Memo1.Lines.add('');

        idHttpObj.Get(url, mstream);

        mstream.Position := 0;

        Memo1.Lines.Clear;

        mstream.Read(aByte, 1);


        isFoundCode := False;

        if aByte = $FF then

        begin

          mstream.Read(aByte, 1);

          if aByte = $EF then

          begin

            isFoundCode := True;

          end;

        end

        else if aByte = $EF then

        begin

          mstream.Read(aByte, 1);

          if aByte = $BB then

          begin

            mstream.Read(aByte, 1);

            if aByte = $BF then

            begin

              isFoundCode := True;

            end;

          end

          else if aByte = $FF then

          begin

            isFoundCode := True;

          end;

        end;

        mstream.Position := 0;


        if isFoundCode then

        begin

          //自动识别 BOM

          Memo1.Lines.LoadFromStream(mstream);

        end

        else

        begin

          //没有 BOM 猜吧。

          ustream := TStringStream.Create('', TEncoding.Unicode);

          try

            ustream.CopyFrom(mstream, 0);

            astream := TStringStream.Create('', TEncoding.ANSI);

            try

              mstream.Position := 0;

              astream.CopyFrom(mstream, 0);

              if ustream.DataString <> astream.DataString then

              begin

                SetLength(rs, Length(astream.Bytes) + 1);

                FillChar(rs[Low(string)], Length(rs), 0);

                Move((@astream.Bytes[0])^, rs[Low(string)], Length(astream.Bytes));

                if IsUTF8String(rs) then

                begin

                  stream := TStringStream.Create('', TEncoding.UTF8);

                  try

                    mstream.Position := 0;

                    stream.CopyFrom(mstream, 0);

                    ss := stream.DataString;

                  finally

                    FreeAndNil(stream);

                  end;

                end

                else

                begin

                  ss := astream.DataString;

                end;

              end

              else

              begin

                ss := ustream.DataString;

              end;

              Memo1.Lines.Text := ss;

            finally

              FreeAndNil(astream);

            end;

          finally

            FreeAndNil(ustream);

          end;

        end;

      finally

        FreeAndNil(idHttpObj);

      end;

  finally

    FreeAndNil(mstream);

  end;

  

end;


关键字词: