5

我正在尝试在 CentOS 6.4 上使用 Qt 5.0 中的 QNetworkAccessManager 将文件上传到服务器。

我曾尝试在网上遵循一些示例,但它们都不起作用。QFTP 工作得很好,但速度很慢,现在已弃用。我的上传代码是:

void ftp::start(QString fileLocation)
{

    QUrl url2("ftp://example.com");
    url2.setUserName(ftpusername);
    url2.setPassword(ftppassword);

    data = new QFile(fileLocation, this);
    if (data->open(QIODevice::ReadOnly)) {
        nam = new QNetworkAccessManager();
        reply = nam->put(QNetworkRequest(url2), data);

        connect(nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(requestFinished(QNetworkReply*)));
        connect(reply, SIGNAL(uploadProgress(qint64, qint64)), SLOT(uploadProgress2(qint64, qint64)));
        connect(reply, SIGNAL(finished()), SLOT(uploadDone()));
    }
    else
    {
        qDebug() << "Could not open file to FTP";
    }
}

void ftp::uploadProgress2(qint64 done, qint64 total) {
    double percent;
    if(done > 0 && total > 0)
    {
        percent = (done*100)/total;
    }
    myParent->addLog("Completed: " + QString::number(done) + "/" + QString::number(total) + " " + QString::number(percent) + "%");
}

void ftp::uploadDone() {
    qDebug() << "Error Code: " << reply->error();
    data->deleteLater();
    reply->deleteLater();
}

void ftp::requestFinished(QNetworkReply* r)
{
    qDebug() << "Finished ";
    qDebug()<< r->errorString();
}

这是我的程序的输出:

Completed: 0/0 0% 
Finished   
"Cannot open ftp://username:password@example.com/: is a directory"  
Error code: 202

查看文档, 202 意味着:

QNetworkReply::ContentOperationNotPermittedError The operation requested on the remote content is not permitted

有什么建议么?

4

1 回答 1

5

改变:

QUrl url2("ftp://example.com");

QUrl url2("ftp://example.com/somefile");

有必要指出文件的链接。

于 2013-07-03T15:22:56.473 回答