主要代码:
- C/C++ code
#include "widget.h"#include "ui_widget.h"Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this); ftp = new QFtp(this); ftp->connectToHost("ftp.qt.nokia.com"); //连接到服务器 ftp->login(); //登录 ftp->cd("qt"); //跳转到“qt”目录下 QFile *file = new QFile("INSTALL"); if (!file->open(QIODevice::WriteOnly)) { delete file; return; } ftp->get("INSTALL", file); connect(ftp,SIGNAL(commandStarted(int)),this,SLOT(ftpCommandStarted(int))); connect(ftp,SIGNAL(commandFinished(int,bool)),this,SLOT(ftpCommandFinished(int,bool))); ftp->close(); //关闭连接}Widget::~Widget(){ delete ui;}void Widget::changeEvent(QEvent *e){ QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}void Widget::ftpCommandStarted(int){ if(ftp->currentCommand() == QFtp::ConnectToHost) { ui->label->setText(tr("正在连接到服务器...")); } if(ftp->currentCommand() == QFtp::Login) { ui->label->setText(tr("正在登录...")); } if(ftp->currentCommand() == QFtp::Get) { ui->label->setText(tr("正在下载...")); } else if(ftp->currentCommand() == QFtp::Close) { ui->label->setText(tr("正在关闭连接...")); }}void Widget::ftpCommandFinished(int,bool error){ if(ftp->currentCommand() == QFtp::ConnectToHost) { if(error) ui->label->setText(tr("连接服务器出现错误:%1").arg(ftp->errorString())); else ui->label->setText(tr("连接到服务器成功")); return; } if(ftp->currentCommand() == QFtp::Login) { if(error) ui->label->setText(tr("登录出现错误:%1").arg(ftp->errorString())); else { ui->label->setText(tr("登录成功")); } } if(ftp->currentCommand() == QFtp::Get) { if(error) ui->label->setText(tr("下载出现错误:%1").arg(ftp->errorString())); else { ui->label->setText(tr("已经完成下载")); ui->textBrowser->setText(ftp->readAll()); } } else if (ftp->currentCommand() == QFtp::Close) { ui->label->setText(tr("已经关闭连接")); }}
现在问题是下小的文件就下不来,大小为0,下大的文件不完整,总之就有丢失,不知怎么解决,求高人指点...谢谢,另外不是网络问题,我用QT自带的FTP例子下文件完全没问题。
------解决方案--------------------
try to use ftp binary mode
------解决方案--------------------
参考 Qt的例子
examples ->network ->qftp
自己对照看看。。。。