深圳宝安外贸网站建设公司,唯品会一个专做特卖的网站广告,上海广告传媒有限公司,2024年重大新闻摘抄本文主要讲解网盘的文件操作中进入文件夹的部分#xff0c;具体实现步骤如下#xff1a;
1、具体步骤如下#xff1a;
A、客户端发送进入文件夹的请求#xff08;该请求包含目录信息以及要进入的文件夹名字#xff09;
B、服务器收到该请求之后#xff0c;服务器首先判…本文主要讲解网盘的文件操作中进入文件夹的部分具体实现步骤如下
1、具体步骤如下
A、客户端发送进入文件夹的请求该请求包含目录信息以及要进入的文件夹名字
B、服务器收到该请求之后服务器首先判断路径是否正确。
正确则进入到该路径中将新文件夹中的文件信息发送给客户端
不正确回复失败给客户端
C、客户端接收来自服务器的回复信息并显示
2、代码实现
2.1、添加协议进入文件夹的消息类型 ENUM_MSG_TYPE_ENTER_DIR_REQUEST, //进入文件夹请求
ENUM_MSG_TYPE_ENTER_DIR_RESPOND, //进入文件夹回复
2.2、添加进入文件夹的槽函数 //进入文件夹
void enterDir(const QModelIndex index);
2.3、关联connect信号doubleClicked双击 //关联进入文件夹信号槽 doubleClicked双击connect(m_pBookListW,SIGNAL(doubleClicked(QModelIndex)),this, SLOT(enterDir(QModelIndex)));
2.4、添加槽函数定义
void Book::enterDir(const QModelIndex index)
{//通过index获得双击的选项上面的内容QString strDirName index.data().toString();//测试打印 //qDebug()strDirName;QString strCurPath TcpClient::getinstance().curPath();PDU *pdu mkPDU(strCurPath.size()1);pdu-uiMsgTypeENUM_MSG_TYPE_ENTER_DIR_REQUEST;strncpy(pdu-caData,strDirName.toStdString().c_str(),strDirName.size());memcpy(pdu-caMsg,strCurPath.toStdString().c_str(),strCurPath.size());//发送给服务器TcpClient::getinstance().getTcpSocket().write((char*)pdu,pdu-uiPDULen);free(pdu);pduNULL;
}
2.5、在服务器端写处理
2.6、添加进入文件夹的case
case ENUM_MSG_TYPE_ENTER_DIR_REQUEST:{char caEnterName[32] {\0};strncpy(caEnterName,pdu-caData,32);//产生一块空间将传过来的路径拷贝出来char *pPath new char[pdu-uiMsgLen];memcpy(pPath,pdu-caMsg,pdu-uiMsgLen);QString strPath QString(%1/%2).arg(pPath).arg(caEnterName);qDebug()strPath;//判断是路径还是唱常规文件QFileInfo fileInfo(strPath);PDU *respdu NULL;//判断类型if(fileInfo.isDir()){QDir dir(strPath);QFileInfoList fileInfoList dir.entryInfoList();//产生pduint iFileCount fileInfoList.size();PDU *respdu mkPDU(sizeof (FileInfo)*iFileCount);respdu-uiMsgType ENUM_MSG_TYPE_FLUSH_DIR_RESPOND;FileInfo *pFileInfo NULL;QString strFileName;for(int i0;iiFileCount;i){//拷贝进去,跳到下一个结构体pFileInfo (FileInfo*)(respdu-caMsg)i;strFileName fileInfoList[i].fileName();memcpy(pFileInfo-caFileName,strFileName.toStdString().c_str(),strFileName.size());//判断类型if(fileInfoList[i].isDir()){pFileInfo-iFileType0;//表示是个文件夹}else if(fileInfoList[i].isFile()) {pFileInfo-iFileType 1;//常规文件}}//发送write((char*)respdu, respdu-uiPDULen);free(respdu);respdu NULL;}else if(fileInfo.isFile()) {respdumkPDU(0);respdu-uiMsgTypeENUM_MSG_TYPE_ENTER_DIR_RESPOND;strcpy(respdu-caData,ENTERE_DIR_FAILED);write((char*)respdu, respdu-uiPDULen);free(respdu);respdu NULL;}break;}
2.7、客户端接收 //---------------------进入文件夹------------------------case ENUM_MSG_TYPE_ENTER_DIR_RESPOND:{QMessageBox::information(this,进入文件夹,pdu-caData);break;}
现在还是有点问题的当我们进入文件夹的时候当前的文件夹strCurPath应该更新的
2.8、添加清楚进入文件夹的函数
void Book::clearEnterDir()
{m_strEnterDir.clear();
} //---------------------进入文件夹------------------------case ENUM_MSG_TYPE_ENTER_DIR_RESPOND:{OpeWidget::getInstance().getBook()-clearEnterDir();QMessageBox::information(this,进入文件夹,pdu-caData);break;}
3、测试
3.1、点击常规文件 3.2、点击文件夹直接进入