现在通过C++ GUI Programming with Qt4书中QFtp的例子, 来回顾一下以前学习的知识。
先回顾下书中的分析,添加一些自己不熟悉的知识点,最后了列出完整程序代码。
1. The QUrl class provides a high-level interface for extracting the different parts of a URL, such as the file name, path, protocol, and port.
QUrl类提供了一个高级接口,用来提取URL的不同部分,如文件名称、路径、协议和端口。
1.1 bool QUrl::isValid () const // Returns true if the URL is valid; otherwise returns false.
1.2 QString QUrl::path () const // Returns the path of the URL.
path().isEmpty() // QString.isEmpty(); 检查字符串是否为空
1.3 QString QUrl::scheme () const // Returns the scheme of the URL. If an empty string is returned, this means the scheme is undefined and the URL is then relative.
See also setScheme() and isRelative().
2. QFileInfo class //想起刚开始学习网络编程的时候对文件处理还不熟悉,饶弯子了
The QFileInfo class provides system-independent file information.
QFileInfo provides information about a file's name and position (path) in the file system, its access rights and whether it is a directory or symbolic link, QFileInfo can also be used to obtain information about a Qt resource.
2.1 5种构造函数,此处为QFileInfo::QFileInfo ( const QDir & dir, const QString & file )。
2.2 QString QFileInfo::fileName () const
Returns the name of the file, excluding the path.
Example:
QFileInfo fi("/tmp/archive.tar.gz");
QString name = fi.fileName(); // name = "archive.tar.gz"
3 QFtp class
The QFtp class provides an implementation of the client side of FTP protocol.
This class provides a direct interface to FTP that allows you to have more control over the requests. However, for new applications, it is recommended to use QNetworkAccessManager and QNetworkReply, as those classes possess a simpler, yet more powerful API.
这里有个奇怪的问题,文档中怎么说对于新的应用程序建议使用QNetworkAccessManager and QNetworkReply。
QFtp provides several FTP commands, including connectToHost(), login(), close(), list(), cd(), get(), put(), remove(), mkdir(), rmdir(), and rename(). All of these functions schedule an FTP command and return an ID number that identifies the command. It is also possible to control the transfer mode (the default is passive) and the transfer type (the default is binary).
未完
ftpget.pro 文件
TEMPLATE = app QT = core network CONFIG += console CONFIG -= app_bundle HEADERS = ftpget.h SOURCES = ftpget.cpp \ main.cpp
ftpget.h 头文件
#ifndef FTPGET_H #define FTPGET_H #include <QFile> #include <QFtp> class QUrl; 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.cpp 文件
#include <QtCore> #include <QtNetwork> #include <iostream> #include "ftpget.h" 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: Invalid 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 <QtCore> #include <iostream> #include "ftpget.h" int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QStringList args = QCoreApplication::arguments(); if (args.count() != 2) { std::cerr << "Usage: ftpget url" << std::endl << "Example:" << std::endl << " ftpget ftp://ftp.trolltech.com/mirrors" << std::endl; return 1; } FtpGet getter; if (!getter.getFile(QUrl(args[1]))) return 1; QObject::connect(&getter, SIGNAL(done()), &app, SLOT(quit())); return app.exec(); }