Qt:TXT文本读写的字符编码

Response返回我是这样写的,一开始写的toLocal8Bit(),在浏览器地址里面输入127.0.0.1:8080,能正常

显示中文,但web处理的时候却乱码了。然后我将toLocal8Bit()修改为toUtf8(),web就能正常处理了

复制代码
void HttpController::service(HttpRequest &request, HttpResponse &response) {

    QFile aFile("xxxxx.txt");

    if(!aFile.exists())
    {
        response.write("File does not exist",false);
    }

    if(!aFile.open(QIODevice::ReadOnly |QIODevice::Text))
    {
        response.write("Pattern exception",false);
    }

    QTextStream aStream(&aFile);
    QString text;
    text = aStream.readAll();
    //QByteArray byte= text.toLocal8Bit();
    QByteArray byte = text.toUtf8();
    response.write(byte,true);
    aFile.close();

}

toUtf8是输出UTF-8编码的字符集

toLatin1是相当与ASCii码不包含中文的遇到中文默认转换为ascii码0x3f也就是字符'?'

Local8bit是本地操作系统设置的字符集编码,一般为GB2312.

相关推荐
tiancaijiben3 分钟前
阿里云云备份(Cloud Backup)全量对接与使用指南
数据库·oracle
Lang-12103 分钟前
CentOS Linux服务器完整迁移方案
linux·服务器·centos
TCW11216 分钟前
Linux操作系统系列.动态加载
linux·服务器
lisanmengmeng13 分钟前
gitlab 免密配置
linux·服务器·gitlab
sulikey14 分钟前
数据库中等值连接与自然连接的区别。为什么不建议使用自然连接?
数据库·sql·mysql·等值连接·自然连接
普马萨特15 分钟前
Wi-Fi (802.11) 协议演进
运维·服务器·网络
sycmancia19 分钟前
Qt——自定义模型类
开发语言·qt
袁小皮皮不皮23 分钟前
2.HCIP OSPF路由基础(优化版)
运维·服务器·网络·网络协议·智能路由器
IT策士25 分钟前
Redis 从入门到精通:分布式锁 —— 从 SETNX 到 Redlock
数据库·redis·分布式
云计算磊哥@27 分钟前
运维开发宝典027-MySQL03数据库的增删改查
运维·数据库·运维开发