tcp客户端向tcp服务器发送json文件,服务器保存文件
·
客户端:
void socket::send_msg(QString file_name)
{
//读取json文件
QFile file(file_name); // fileName文件的路径
if (file.open(QIODevice::ReadOnly)) {
QByteArray data = file.readAll();
file.close();
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文件
更多推荐


所有评论(0)