Qt用FTP进行上传下载
#include "ftpclient.h"
#include "ui_ftpclient.h"
#include <QString>
#include <QMessageBox>
#include <QtGui>
FtpClient::FtpClient(QWidget *parent) :
QDialog(parent),
ui(new Ui::FtpClient)
{
ui->setupUi(this);
LabelServer=new QLabel(tr("服务器:")); //192.168.108.6
LabelUser=new QLabel(tr("用户名:"));
LabelPassword=new QLabel(tr("密码:")); //ft4w685Y
PushButtonLogin=new QPushButton;
PushButtonPut=new QPushButton;
PushButtonGet=new QPushButton;
PushButtonLogin->setText(tr("登录"));
PushButtonPut->setText(tr("上传"));
PushButtonGet->setText(tr("下载"));
LineEditServer=new QLineEdit;
QHBoxLayout *layout1=new QHBoxLayout;
layout1->addWidget(LabelServer);
layout1->addWidget(LineEditServer);
LineEditUser=new QLineEdit;
QHBoxLayout *layout2=new QHBoxLayout;
layout2->addWidget(LabelUser);
layout2->addWidget(LineEditUser);
LineEditPassword=new QLineEdit;
QHBoxLayout *layout3=new QHBoxLayout;
layout3->addWidget(LabelPassword);
layout3->addWidget(LineEditPassword);
QHBoxLayout *layout4=new QHBoxLayout;
layout4->addWidget(PushButtonPut);
layout4->addWidget(PushButtonGet);
QVBoxLayout *mainlayout=new QVBoxLayout;
mainlayout->addLayout(layout1);
mainlayout->addLayout(layout2);
mainlayout->addLayout(layout3);
mainlayout->addWidget(PushButtonLogin);
mainlayout->addLayout(layout4);
setLayout(mainlayout);
PushButtonPut->setEnabled(false);
PushButtonGet->setEnabled(false);
connect(PushButtonLogin,SIGNAL(clicked()),this,SLOT(slotLogin()));
connect(PushButtonPut,SIGNAL(clicked()),this,SLOT(slotPut()));
connect(PushButtonGet,SIGNAL(clicked()),this,SLOT(slotGet()));
}
FtpClient::~FtpClient()
{
delete ui;
}
void FtpClient::slotLogin()
{
QString serverAddress = LineEditServer->text();
if(serverAddress.isEmpty())
{
QMessageBox::warning(this,tr("error"),tr("Please input server address!"));
return;
}
QString userName=LineEditUser->text();
if(userName.isEmpty())
{
QMessageBox::warning(this,tr("error"),tr("Please input user name!"));
return;
}
QString password=LineEditPassword->text();
ftpClient=new QFtp();
ftpClient->connectToHost(serverAddress);
connect(ftpClient,SIGNAL(stateChanged(int)),this,SLOT(slotStateChanged(int)));
connect(ftpClient,SIGNAL(done(bool)),this,SLOT(slotDone(bool)));
ftpClient->login(userName,password);
curStatus=LOGIN;
}
void FtpClient::slotStateChanged(int state)
{
if(state==QFtp::LoggedIn)
{
PushButtonPut->setEnabled(true);
PushButtonGet->setEnabled(true);
}
}
void FtpClient::slotPut()
{
bool ok;
QString fileName=QInputDialog::getText(this,tr("Put File:"),
tr("Please input file name:"),QLineEdit::Normal,QString(),&ok);
if(ok&&!fileName.isEmpty())
{
QFile *remoteFileName=new QFile(fileName);
ftpClient->put(remoteFileName,fileName);
// QFile *dataFile=new QFile("filename");
// dataFile->open(QIODevice::ReadOnly);
// ftpClient->put(dataFile,"filename");
}
curStatus=PUT;
}
void FtpClient::slotGet()
{
bool ok;
QString fileName=QInputDialog::getText(this,tr("Get File:"),
tr("Please input file name:"),QLineEdit::Normal,QString(),&ok);
if(ok&&!fileName.isEmpty())
{
localFile=new QFile(fileName);
localFile->open(QIODevice::WriteOnly);
ftpClient->get(fileName,localFile);
}
curStatus=GET;
}
void FtpClient::slotDone(bool error)
{
if(error)
{
QMessageBox::warning(this,tr("error"),ftpClient->errorString());
return;
}
if(curStatus==LOGIN)
{
PushButtonLogin->setEnabled(false);
curStatus=None;
}
if(curStatus==PUT)
{
QMessageBox::warning(this,tr("succeed"),tr("Put file succeed!"));
curStatus=None;
}
if(curStatus==GET)
{
localFile->close();
QMessageBox::warning(this,tr("succeed"),tr("Get file succeed!"));
curStatus=None;
}
}最主要的问题是,上传文件的函数slotPut(),无论怎样都上传不了文件。在工程文件夹下随便一个记事本,上传时写这记事本的文件名也上传不了。难道Qt写文件上传不能像php一样一个<input type="file">就有一个控件能点,浏览整个计算机的文件后选择一个文件上传?
还有,下载那块是能下载,但却是直接下载到工程文件夹下的,也不能选择下载到具体电脑的哪个位置。应该是put()和get()函数只能这样吧?能用别的函数吗?
求具体实例代码啊~~
...全文
相关推荐
QT5.0实现FTP客户端
利用qftp模块实现FTP客户端,源码已经得到验证,下载下来可直接使用。
Qt 之 FTP 上传/下载
为了方便网络编程,Qt 提供了 Network 模块。该模块包含了许多类,例如:QFtp - 能够更加轻松使用 FTP 协议进行网络编程。
QT5 实现FTP服务器简单交互
使用QT5 的QNetworkAccessManager 加windows自带FTP服务器,实现了简单的FTP的读写交互。希望能够对您有帮助。
qtftp-master_followlco_qt5ftp_QT5_基于QT5的FTP类库_ftpqt5_
基于Qt5的FTP类库
QT4的FTP文件下载程序
QT4的FTP文件下载程序,cpp文件,因头文件包含在工程文件压缩包里拆解不变,头文件很简单可自己补充,见谅!


