客户端:
- void socket::send_msg(QString file_name)
- {
- qDebug() <<"socket::send_msg(QString file_name):" << QThread::currentThread();
- //读取json文件
- QFile file(file_name); // fileName文件的路径
- if (file.open(QIODevice::ReadOnly)) {
- QByteArray data = file.readAll();
- file.close();
- qDebug() << "[out]: "<
- tcpClient->write(data);
- }
- }
就是根据文件路径--->QFile--->QByteArray--->传递给服务器
服务器:
- void widget::onSocketReadyRead()
- {//读取缓冲区行文本
- qDebug() << "read:" << tcpSocket;
- QByteArray ba=tcpSocket->readAll();
- write_to_json_file(ba);
- }
- void widget::write_to_json_file(QByteArray& ba)
- {
- QJsonDocument document = QJsonDocument::fromJson(ba);
- QString str = QApplication::applicationDirPath() + "/2.json";
- qDebug() << "filepath:" << str;
- QFile file(str);
- if (file.open(QIODevice::WriteOnly)) {
- file.write(document.toJson());
- file.close();
- }
- }
从客户端接收数据--->QByteArray--->QJsonDocument--->QFile--->xxx.json文件