当前位置:   article > 正文

Qt解决QUrl含有中文或特殊字符无法下载的问题_qurl gbk

qurl gbk
  1. // url = http://www.xxx.com/xxx – 3 xxx.txt
  2. void getRemoteFile(const QString url)
  3. {
  4. // 解决文件名中含有特殊字符无法下载的问题
  5. QString fileName = QUrl(url).fileName();
  6. QString headPath = QString(url).remove(fileName);
  7. QTextCodec *codec = QTextCodec::codecForName("gbk");
  8. QByteArray byte = codec->fromUnicode(fileName); // 只对文件名进行编码,否则前面路径会改变 http%3A%2F%2Fwww.xxx
  9. byte = byte.toPercentEncoding(); // 转成带百分号的格式
  10. byte = QByteArray(headPath.toStdString().c_str()) + byte;
  11. QUrl encodeUrl(byte);
  12. encodeUrl = encodeUrl.fromEncoded(byte); // encodeUrl = http://www.xxx.com/xxx %A8C 3 xxx.txt
  13. m_reply = m_manager.get(QNetworkRequest(encodeUrl));
  14. }

把转码后的字符转回

  1. QString path = reply->url().toString().remove(m_remoteServerURL); // path = xxx %A8C 3 xxx.txt
  2. QTextCodec *codec = QTextCodec::codecForName("gbk");
  3. QByteArray ba = codec->fromUnicode(path);
  4. ba = QByteArray::fromPercentEncoding(ba);
  5. path =codec->toUnicode(ba); // path = xxx – 3 xxx.txt



                
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/299692
推荐阅读
  

闽ICP备14008679号