1. ftp://ftp.denx.de/pub/u-boot/lowboot-1.0.0.patch.gz 下载文件
FtpGet.h
#ifndef FTPGET_H
#define FTPGET_H
#include <QUrl>
#include <QFtp>
#include <QFile>
class Ftpget : public QObject
{
Q_OBJECT
public:
Ftpget(QObject *parent=0);
bool getFile(const QUrl &url);
signals:
void done();
private slots:
void ftpDone(bool error);
private:
QFtp ftp;
QFile file;
};
#endif // FTPGET_H
FtpGet.cpp
#include "FtpGet.h"
#include <iostream>
#include <QFileInfo>
Ftpget::Ftpget(QObject *parent)
:QObject(parent)
{
connect(&ftp, SIGNAL(done(bool)), this, SLOT(ftpDone(bool)));
}
bool Ftpget::getFile(const QUrl &url)
{
if( !url.isValid() )
{
std::cerr << "Error: Invaild URL" << std::endl;
return false;
}
if( url.scheme() != "ftp" )
{
std::cerr << "Error: URL must start with 'ftp'" << std::endl;
return false;
}
if( url.path().isEmpty() )
{
std::cerr << "Error: URL has no path " << std::endl;
return false;
}
QString localFileName = QFileInfo(url.path()).fileName();
if( localFileName.isEmpty() )
localFileName = "ftpget.out";
file.setFileName(localFileName);
if( !file.open(QIODevice::WriteOnly) )
{
std::cerr << "Error: cannot write file"
<< qPrintable(file.fileName()) << ":"
<< qPrintable(file.errorString()) << std::endl;
return false;
}
ftp.connectToHost(url.host(), url.port(21));
ftp.login();
ftp.get(url.path(), &file);
ftp.close();
return true;
}
void Ftpget::ftpDone(bool error)
{
if( error )
std::cerr << "Error: " << qPrintable(ftp.errorString()) << std::endl;
else
std::cerr << "File downloaded as " << qPrintable(file.fileName()) << std::endl;
file.close();
emit done();
}
main.cpp
#include <QtGui/QApplication>
#include <QCoreApplication>
#include <QStringList>
#include <iostream>
#include "FtpGet.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QStringList args = QCoreApplication::arguments();
if( args.count() != 2 )
{
std::cerr << "Ex: ftpget ftp://xxxxx" << std::endl;
return 1;
}
Ftpget getter;
if( !getter.getFile(QUrl(args[1])) )
return 1;
QObject::connect(&getter, SIGNAL(done()), &a, SLOT(quit()));
return a.exec();
}