posts - 1,  comments - 0,  trackbacks - 0
在QT上写的一个FTP客户端,包含几个基本的功能,用户登录、密码保护、文件上传、文件下载。
#include "ftp.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QTextCodec>
#include <QDialog>
#include <QMessageBox>
#include <QStringList>
#include <QInputDialog>
#include <QFileDialog>


FtpClient::FtpClient(QWidget *parent)
    : QWidget(parent)
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
    //textEdit = new QTextEdit;
    widget = new QTreeWidget;
    widget->setEnabled(false);
    widget->setRootIsDecorated(false);
    widget->setHeaderLabels(QStringList() << tr("Name")
                            << tr("Size")<< tr("Owner")
                            << tr("Group")<< tr("Time"));
    ftpLabel = new QLabel(tr("请做出如下输入,在信息完全正确是,可执行。"));
    ftpserverLabel = new QLabel(tr("FTP客户端:"));
    portLabel = new QLabel(tr("端口:"));
    userLabel = new QLabel(tr("用户名:"));
    passwordLabel = new QLabel(tr("密码:"));

    ftpserverEdit = new QLineEdit;
    ftpserverLabel->setBuddy(ftpserverEdit);

    portEdit = new QLineEdit("21");
    userEdit = new QLineEdit;
    passwordEdit = new QLineEdit;
    passwordEdit->setEchoMode(QLineEdit::Password);

    ftpPuB = new QPushButton(tr("登录"));
    ftpPuB->setDefault(true);
    portPuB = new QPushButton(tr("上传目录"));
    //portPuB->setDefault(false);
    portPuB->setDisabled(true);
    userPuB = new QPushButton(tr("下载"));
    userPuB->setDisabled(true);
    //userPuB->setDefault(false);
    passPuB = new QPushButton(tr("退出"));

    setWindowTitle(tr("Ftp 客户端"));
    resize(300,200);

    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->addWidget(ftpPuB);
    hLayout->addWidget(portPuB);
    hLayout->addWidget(userPuB);
    hLayout->addWidget(passPuB);

    QGridLayout *gridLayout = new QGridLayout;
    gridLayout->addWidget(ftpserverLabel,0,0,1,1);
    gridLayout->addWidget(ftpserverEdit,0,1,1,1);
    gridLayout->addWidget(portLabel,0,2,1,1);
    gridLayout->addWidget(portEdit,0,3,1,1);
    gridLayout->addWidget(userLabel,1,0,1,1);
    gridLayout->addWidget(userEdit,1,1,1,1);
    gridLayout->addWidget(passwordLabel,1,2,1,1);
    gridLayout->addWidget(passwordEdit,1,3,1,1);

    QVBoxLayout *vLayout = new QVBoxLayout(this);
    vLayout->addWidget(widget);
    vLayout->addWidget(ftpLabel);
    vLayout->addLayout(gridLayout);
    vLayout->addLayout(hLayout);

    connect(widget,SIGNAL(itemActivated(QTreeWidgetItem*,int)),
            this,SLOT(processItem(QTreeWidgetItem*,int)));
    connect(widget,SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
            this,SLOT(enableDownloadButton()));
    connect(ftpPuB,SIGNAL(clicked()),this,SLOT(connect1234()));
    //connect(passPuB,SIGNAL(clicked()),this,SLOT(close()));
    connect(ftpPuB,SIGNAL(clicked()),this,SLOT(accept()));
    connect(portPuB,SIGNAL(clicked()),this,SLOT(slotPut()));
    connect(userPuB,SIGNAL(clicked()),this,SLOT(slotGet()));
    connect(passPuB,SIGNAL(clicked()),this,SLOT(slotDone(bool)));

    //connect(ftpPuB,SIGNAL(pressed()),this,SLOT(login()));
    
//connect(ftpPuB,SIGNAL(clicked()),ftp,SLOT(connect1234()));

    /*if(ftp == NULL){

    ftp = new QFtp(this);
    connect(progressDialog,SIGNAL(canceled()),
            this,SLOT(cancelDowload()));
    connect(&ftp,SIGNAL(commandFinished(int bool),this,SLOT(ftpCommandFinished(int,bool))));
*/
    //connect(&ftp,SIGNAL(done(bool)),this,SLOT(ftpDone(bool)));
   
// connect(&ftp,SIGNAL(commandStarted(int)),this,SLOT(ftpcommandStarted(int)));
    
//connect(&ftp,SIGNAL(commandFinished(int,bool)),this,SLOT(ftpcommandFinished(int,bool)));



}

FtpClient::~FtpClient()
{

}

/*bool FtpClient::logIn(const QUrl &url)
{
    if(!url.isValid()){
        qDebug()<<tr("错误:无效的URL")<<endl;
        return false;
    }
    if(url.scheme() !="ftp"){
        qDebug()<<tr("错误:URL 必须以'ftp:'开始")<<endl;
        return false;

    }


connectId = ftp.connectToHost(url.host(),url.port(21));
//connectId = ftp.connectToHost(url.host(),url.port(21));
loginId = ftp.login();
closeId = ftp.close();
return true;

}



void FtpClient::ftpDone(bool error)
{
    if(error){
        qDebug() << tr("错误:") <<qPrintable(ftp.errorString()) <<endl;
    }
    emit done();
}

void FtpClient::ftpcommandStarted(int id)
{
    if(id == connectId){
        qDebug() <<QObject::tr("连接中.") <<endl;
    }else if(id = loginId){
        qDebug() <<QObject::tr("登录中.") <<endl;

    }else if(id = closeId){
        qDebug() <<QObject::tr("关闭中.") <<endl;
    }
}

void FtpClient::ftpcommandFinished(int id, bool error)
{
    if(error){
        //textEdit->textChanged();
        //qDebug() << QObject::tr("操作错误,程序退出!");
        return;
    }
    if(id == connectId){
       qDebug() << QObject::tr("已连接")<<endl;
   }else if(id == loginId){
       qDebug() << QObject::tr("已登录")<<endl;

   }else if(id == closeId){
       qDebug() << QObject::tr("已关闭")<<endl;

   }
}
*/

void FtpClient::accept()
{
    if(userEdit->text() == tr("xiaoxinhe")
        && passwordEdit->text() == tr("123456"))
        {
        //QDialog::accept();
        ftpClient = new QFtp;
        ftpClient->connectToHost(ftpserverEdit->text());

        //ftpClient->connectNotify(ftpserverEdit->text());

        
//ftpClient->login(userEdit->text(),passwordEdit->text());
        ftpClient->login();
        connect(ftpClient,SIGNAL(stateChanged(int)),this,
                SLOT(stateChanged(int)));

        //if()
    }
    else
    {

        QMessageBox::warning(this,tr("警告!"),tr("用户名或密码错误"),QMessageBox::Yes);

        userEdit->setFocus();
    }
}


void FtpClient::processItem(QTreeWidgetItem *item, int column)
{

}

void FtpClient::enableDownloadButton()
{
    QTreeWidgetItem *current = widget->currentItem();
    if(current){
        QString currentFile = current->text(0);
        userPuB->setEnabled(!isDirectory.value(currentFile));
    }else{
        userPuB->setEnabled(false);
    }
}

void FtpClient::enableConnectButton()
{

}

void FtpClient::login()
{
    QDialog *dialog=new QDialog();
    dialog->children();
    //QPushButton *okpushButton = new QPushButton(this);
    dialog->exec();

    //dialog->exec();


}

void FtpClient::stateChanged(int state)
{
    if(state == QFtp::LoggedIn)
    {
       userPuB->setEnabled(true);
       portPuB->setEnabled(true);
    }
}


void FtpClient::slotPut()
{
    bool ok;
    //QString fileName = QInputDialog::getText(this,tr("Put File;"),
                                            
//tr("Please input file name;"),
                                            
//QLineEdit::Normal,QString(),&ok);
    QString fileName = QFileDialog::getOpenFileName(this,tr("打开文件"),"/,*.*");
    if(ok && !fileName.isEmpty())
    {
        QFile *remoteFileName = new QFile(fileName);
        ftpClient->put(remoteFileName,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)
    {
        ftpPuB->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;
    }
}
{
}
void FtpClient::enableDownloadButton()
{
    QTreeWidgetItem *current = widget->currentItem();
    if(current){
        QString currentFile = current->text(0);
        userPuB->setEnabled(!isDirectory.value(currentFile));
    }else{
        userPuB->setEnabled(false);
    }
}
void FtpClient::enableConnectButton()
{
}
void FtpClient::login()
{
    QDialog *dialog=new QDialog();
    dialog->children();
    //QPushButton *okpushButton = new QPushButton(this);
    dialog->exec();
    //dialog->exec();
}
void FtpClient::stateChanged(int state)
{
    if(state == QFtp::LoggedIn)
    {
       userPuB->setEnabled(true);
       portPuB->setEnabled(true);
    }
}
void FtpClient::slotPut()
{
    bool ok;
    //QString fileName = QInputDialog::getText(this,tr("Put File;"),
                                            //tr("Please input file name;"),
                                            //QLineEdit::Normal,QString(),&ok);
    QString fileName = QFileDialog::getOpenFileName(this,tr("打开文件"),"/,*.*");
    if(ok && !fileName.isEmpty())
    {
        QFile *remoteFileName = new QFile(fileName);
        ftpClient->put(remoteFileName,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)
    {
        ftpPuB->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;
    }
}
posted on 2012-06-29 09:20 和总 阅读(879) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理


<2026年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿

随笔档案

文章档案

相册

个人空间

搜索

  •  

最新评论