Qt用FTP进行上传下载

水军总督 水军总督 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#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()函数只能这样吧?能用别的函数吗?

求具体实例代码啊~~
...全文
评论 5
收藏 已收藏
分享
返回顶部
水军总督
Qt 21,503 成员 26,693 内容