如何借助QNetworkRequest创建目录。我知道它没有类似mkDir()的函数在KFtp中击败,但不知何故它可以通过http请求,或者不同的东西。 提前谢谢。
答案 0 :(得分:1)
你应该使用QtFtp。它从Qt 5开始不再构建,因此您需要从源代码构建它并将其添加到Qt安装中。我用Qt 5.2在Windows和OSX上完成了它。它还不错。
我只在Qt 4.8下找到了它的在线文档。
http://doc.qt.io/qt-4.8/qftp.html#details
这是您正在寻找的功能。
http://doc.qt.io/qt-4.8/qftp.html#mkdir
https://forum.qt.io/topic/45242/solved-how-to-do-an-ftp-client-with-qt5-what-classes-to-use
记录示例:
http://doc.qt.io/qt-4.8/qt-network-qftp-example.html
以下是我使用过的需要QtFtp访问的示例。
使用QtFtp的示例:
https://github.com/magist3r/QtWeb/blob/fffaddce36a594013f987e469dbf22d94a78dfa9/src/webview.cpp
webview.h
//////////////////////////////////////////////////// AC: FTP implementation
QFtp* m_ftp;
QFile* m_ftpFile;
QString m_ftpHtml;
QProgressDialog *m_ftpProgressDialog;
QHash<QString, bool> m_ftpIsDirectory;
QString m_ftpCurrentPath;
private:
void ftpCheckDisconnect();
void ftpDownloadFile(const QUrl &url, QString filename );
private slots:
void ftpCancelDownload();
void ftpCommandFinished(int commandId, bool error);
void ftpAddToList(const QUrlInfo &urlInfo);
void ftpUpdateDataTransferProgress(qint64 readBytes, qint64 totalBytes);
webview.cpp
///////////////////////////////////////////////////////////////
// AC: FTP impl
void WebView::ftpCancelDownload()
{
if (m_ftp)
m_ftp->abort();
}
void WebView::ftpCheckDisconnect()
{
if (m_ftp)
{
m_ftp->abort();
m_ftp->deleteLater();
m_ftp = NULL;
setCursor(Qt::ArrowCursor);
}
}
void WebView::loadFtpUrl(const QUrl &url)
{
m_is_loading = true;
m_ftpHtml.clear();
setCursor(Qt::WaitCursor);
m_ftp = new QFtp(this);
connect(m_ftp, SIGNAL(commandFinished(int, bool)),
this, SLOT(ftpCommandFinished(int, bool)));
connect(m_ftp, SIGNAL(listInfo(const QUrlInfo &)),
this, SLOT(ftpAddToList(const QUrlInfo &)));
connect(m_ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
this, SLOT(ftpUpdateDataTransferProgress(qint64, qint64)));
m_ftpHtml.clear();
m_ftpCurrentPath.clear();
m_ftpIsDirectory.clear();
if (!url.isValid() || url.scheme().toLower() != QLatin1String("ftp")) {
m_ftp->connectToHost(url.toString(), 21);
m_ftp->login();
} else {
m_ftp->connectToHost(url.host(), url.port(21));
if (!url.userName().isEmpty())
m_ftp->login(QUrl::fromPercentEncoding(url.userName().toLatin1()), url.password());
else
m_ftp->login();
if (!url.path().isEmpty())
{
if (!url.hasQuery())
{
m_ftp->cd(url.path());
}
else
{
// Downloading file
QString f = url.path();
int ind = f.lastIndexOf('/');
QString dir = "";
if (ind != -1 )
{
dir = f.left(ind + 1);
}
m_ftp->cd(dir);
setStatusBarText(tr("Downloading file %1...").arg(url.toString()));
return;
}
}
}
setStatusBarText(tr("Connecting to FTP server %1...").arg(url.toString()));
}
void WebView::ftpDownloadFile(const QUrl &url, QString fileName )
{
if (!m_ftp)
return;
if (QFile::exists(fileName)) {
QMessageBox::information(this, tr("FTP"),
tr("There already exists a file called %1 in "
"the current directory.")
.arg(fileName));
return;
}
QString downloadDirectory = dirDownloads(true);
if (!downloadDirectory.isEmpty() && downloadDirectory.right(1) != QDir::separator())
downloadDirectory += QDir::separator();
QString fn = QFileDialog::getSaveFileName(this, tr("Save File"), downloadDirectory + fileName);
if (fn.isEmpty())
{
QMessageBox::information(this, tr("FTP"), tr("Download canceled."));
return;
}
m_ftpFile = new QFile(fn);
if (!m_ftpFile->open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("FTP"),
tr("Unable to save the file %1: %2.")
.arg(fn).arg(m_ftpFile->errorString()));
delete m_ftpFile;
return;
}
m_ftp->get(fileName, m_ftpFile);
m_ftpProgressDialog->setLabelText(QString(" <br>" + tr("Downloading <b>%1</b>")).arg(fn));
m_ftpProgressDialog->setWindowTitle(tr("FTP Download"));
m_ftpProgressDialog->setModal(true);
}
void WebView::ftpCommandFinished(int res, bool error)
{
setCursor(Qt::ArrowCursor);
if (!m_ftp)
return;
if (m_ftp->currentCommand() == QFtp::ConnectToHost) {
if (error) {
QMessageBox::information(this, tr("FTP"),
tr("Unable to connect to the FTP server at %1. Please check that the host name is correct.")
.arg(m_initialUrl.toString()));
ftpCheckDisconnect();
return;
}
setStatusBarText(tr("Logged onto %1.").arg(m_initialUrl.host()));
return;
}
if (m_ftp->currentCommand() == QFtp::Login)
{
setStatusBarText(tr("Listing %1...").arg(m_initialUrl.toString()));
m_ftpHtml.clear();
QString u = m_initialUrl.toString();
int ind = u.lastIndexOf('/');
if (ind != -1 && ind > 7 && ind < u.length() -1 )
{
u = u.left(ind);
}
else
u = "";
if ( !m_initialUrl.hasQuery() )
{
//<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">
m_ftpHtml = "<html><body><PRE><H2>Contents of " + m_initialUrl.toString() + "</H2>" +
(u.length() > 0 ? "<p> Go to <a href=\"" + u + "\">parent directory</a>" : QString(""))+
"<p><table cellpadding=3 cellspacing=3><tr><td></td><td><b>Name</b></td><td width=70 align=right><b>Size</b></td><td width=110 align=center><b>Date/Time</b></td></tr><tr><td> </td><td></td></tr>";
setCursor(Qt::WaitCursor);
m_ftp->list();
}
else
{
QString f = m_initialUrl.toString().left(m_initialUrl.toString().length() - 4);
int ind = f.lastIndexOf('/');
if (ind != -1 )
{
f = f.right(f.length() - ind - 1);
}
ftpDownloadFile(m_initialUrl, f );
}
}
if (m_ftp->currentCommand() == QFtp::Get)
{
if (error)
{
setStatusBarText(tr("Canceled download of %1").arg(m_ftpFile->fileName()));
m_ftpFile->close();
m_ftpFile->remove();
}
else
{
DownloadManager* dm = BrowserApplication::downloadManager();
setStatusBarText(tr("Successfully downloaded file %1").arg(m_ftpFile->fileName()));
m_ftpFile->close();
dm->addItem(m_initialUrl, m_ftpFile->fileName(), true );
}
delete m_ftpFile;
m_ftpProgressDialog->hide();
}
else
if (m_ftp->currentCommand() == QFtp::List)
{
setStatusBarText(tr("Listed %1").arg(m_initialUrl.toString()));
if (m_ftpIsDirectory.isEmpty())
{
m_ftpHtml += "<tr><td>Directory is empty</td></tr>";
}
m_ftpHtml += "</table></PRE></body></html>";
setHtml(m_ftpHtml, m_initialUrl);
//page()->mainFrame()->setHtml(m_ftpHtml, m_initialUrl);
//QString html = page()->mainFrame()->toHtml () ;
//page()->setViewportSize(size());
m_is_loading = false;
}
urlChanged(m_initialUrl);
}
void WebView::ftpAddToList(const QUrlInfo &urlInfo)
{
m_ftpHtml += "<tr><td>" + (urlInfo.isDir() ? QString("DIR") : QString("")) + "</td>";
m_ftpHtml += "<td>" + (urlInfo.isDir() ? QString("<b>") : QString("")) +
(urlInfo.isDir() ? QString("<a href=\"" + m_initialUrl.toString() + (m_initialUrl.toString().right(1) == "/" ? QString(""): QString("/"))
+ urlInfo.name() + "\">") : QString("")) +
urlInfo.name() + (urlInfo.isDir() ? QString("</a>") : QString("")) + "</td>";
m_ftpHtml += "<td align=right>" + (urlInfo.isDir() ? "" : QString::number(urlInfo.size())) + "</td>";
m_ftpHtml += "<td align=center>" + urlInfo.lastModified().toString("MMM dd yyyy") + "</td>";
m_ftpHtml += "<td>" +
(!urlInfo.isDir() ? QString("<a href=\"" + m_initialUrl.toString() + (m_initialUrl.toString().right(1) == "/" ? QString(""): QString("/"))
+ urlInfo.name() + "?get\">") : QString("")) +
(!urlInfo.isDir() ? QString("GET FILE</a>") : QString("")) + "</td></tr>";
m_ftpIsDirectory[urlInfo.name()] = urlInfo.isDir();
}
void WebView::ftpUpdateDataTransferProgress(qint64 readBytes,
qint64 totalBytes)
{
if (m_ftpProgressDialog)
{
m_ftpProgressDialog->setMaximum(totalBytes);
m_ftpProgressDialog->setValue(readBytes);
}
}
希望有所帮助。
答案 1 :(得分:0)
嗯,简而言之,我决定在GitHub QtFtp上找到的问题编译并扔到正确的地方,并且它有效。这里是对存档的圈套(仅在草案qftp中移除子项目'示例'且不会编译)。https://codeload.github.com/qtproject/qtftp/zip/master