网络传输问题
- C/C++ code
void Copy::startUploading(){ connect(&httpCopyUpload , SIGNAL(responseReady()) , this , SLOT(getUploadResponse())); connect(&httpCopyUpload , SIGNAL(responseError()) , this , SLOT(errorUploadResponse())); QString strFilePathName = "/root/file.tgz"; QByteArray filebyte = ""; QFile fileTar(strFilePathName); if (!fileTar.open(QIODevice::ReadOnly)) { fileTar.close(); return; } else { filebyte = fileTar.readAll(); fileTar.close(); } QString encodedstring = filebyte.toBase64(); QString strIp = getElementText("ServerIP"); QString strPort = getElementText("ServerPort"); httpCopyUpload.setAction("http://tempuri.org/UploadFileByByte"); httpCopyUpload.setHost(strIp,strPort.toInt()); QtSoapMessage request; request.setMethod("UploadFileByByte" , "http://tempuri.org/"); request.addMethodArgument("fileName" , "" ,"file.tgz"); request.addMethodArgument("fileId" , "" , ""); request.addMethodArgument("fileByte" , "" ,encodedstring); request.addMethodArgument("isFirst" , "" , "true"); request.addMethodArgument("isLast" , "" , "true"); request.addMethodArgument("fileMD5" , "" , ""); httpCopyUpload.submitRequest(request , "/Services/FileOperationWebService.asmx");}这是我写的利用gsoap传送文件的代码。发送单个文件是没问题的。
现在,我想把一个文件分成一个个的小块去传。
谁能给点思路啊。
我不想操作字符串, 太耗费资源了。
[解决办法]
我以前做过基于QTcpSocket的文件传输。总结起来传输一个文件主要分这么几步:
1. 客户端在本地打开文件,注意,一定要以独占方式打开,否则文件在传输时被其他进程修改了就麻烦了。
2. 客户端和服务器建立一个连接
3. 客户端告诉服务器要上传的文件名、路径、文件大小、每块大小等信息。
4. 服务器收到信息后,在本地创建文件,当然,也必须是独占方式。
5. 服务器向客户端发送准备就绪的应答。
7. 客户端收到应答后开始发送第一块数据
8. 服务器收到数据后保存到文件,同时向客户端发送确认应答。
9. 客户端收到确认应答后,开始发送第二块数据。直至数据发送完毕。
整个过程需要注意三点:
1. 文件必须以独占方式打开。
2. 客户端和服务器应保持一问一答的状态。
3. 块的大小有讲究,过大或过小都会影响传输速度。我当时默认设置为1024 * 48字节
不过,说了这么多,楼主为什么不用现成的QFtp呢?