ich bin grade dabei einen download von einem Ftp-Server im Lokal Netzwerk zu programmieren.
Jedoch klappt der download Lokal nicht, jedoch von einen Server aus dem Internet ich hoffe ihr könnt mir helfen.
Per FileZilla oder Windows klappt der Zugriff auf den Ftp-Server ohne Probleme.
Auch der Upload funktioniert in meinem Programm, ich kann lediglich nichts herunterladen bzw. ich bekomme kein finished oder downloadProgress SIGNAL, wenn ich versuch von meinem Lokal FTP-Server herunter zu laden.
hier mein code:
Code: Alles auswählen
#ifndef FTPMANAGER_H
#define FTPMANAGER_H
#include <QCoreApplication>
#include <QFile>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QTimer>
#include <QUrl>
#include <QDebug>
#include <QFileInfo>
#include <QObject>
class FTPManager : public QObject
{
Q_OBJECT
public:
explicit FTPManager(QObject *parent = 0);
~FTPManager();
QNetworkAccessManager manager;
QFile *data;
QNetworkReply *reply;
public:
void Upload();
void doUpload(const QString &file);
public slots:
void uploadFinished(QNetworkReply *reply);
void uploadProgress(qint64 bytesSent, qint64 bytesTotal);
public:
void doDownload(const QUrl &url);
QString saveFileName(const QUrl &url);
bool saveToDisk(const QString &filename, QIODevice *data);
void Download();
QList<QNetworkReply *> currentDownloads;
public slots:
void downloadFinished(QNetworkReply *reply);
void ObjectKaputt(QObject* pObjekt);
void authentication();
};
#endif // FTPMANAGER_H
Code: Alles auswählen
FTPManager::FTPManager(QObject *parent) : QObject(parent)
{
//connect(&manager, SIGNAL(finished(QNetworkReply*)), SLOT(uploadFinished(QNetworkReply*)));
connect(&manager, SIGNAL(finished(QNetworkReply*)), SLOT(downloadFinished(QNetworkReply*)));
connect(&manager, SIGNAL(destroyed(QObject*)),SLOT(ObjectKaputt(QObject*)));
connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),SLOT(authentication()));
}
FTPManager::~FTPManager()
{
qDebug() << "zerstörung";
}
void FTPManager::doDownload(const QUrl &url)
{
QNetworkRequest request(url);
QNetworkReply *reply = manager.get(request);
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), SLOT(uploadProgress(qint64, qint64)));
currentDownloads.append(reply);
}
QString FTPManager::saveFileName(const QUrl &url)
{
QString path = url.path();
QString basename = QFileInfo(path).fileName();
if (basename.isEmpty())
basename = "download";
if (QFile::exists(basename)) {
// already exists, don't overwrite
int i = 0;
basename += '.';
while (QFile::exists(basename + QString::number(i)))
++i;
basename += QString::number(i);
}
return basename;
}
bool FTPManager::saveToDisk(const QString &filename, QIODevice *data)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly))
{
qDebug() << "Could not open " << qPrintable(filename) << " for writing: " << qPrintable(file.errorString());
return false;
}
file.write(data->readAll());
file.close();
return true;
}
void FTPManager::Download()
{
QStringList args;
//QUrl url("ftp://172.18.16.137/StorageCard/data/bla.txt");
//QUrl url("ftp://172.18.16.121/StorageCard/DATA/bla.txt");
QUrl url("ftp://ftp.ed.ac.uk/pub/Unix/Ethernet.txt");
url.setPort(21);
url.setUserName("anonymous");
args.append(url.toString());
if (args.isEmpty()) {
qDebug() << "Laedt alle URls in der argsliste herrunter";
QCoreApplication::instance()->quit();
return;
}
doDownload(url);
/*
foreach (QString arg, args) {
QUrl url = QUrl::fromEncoded(arg.toLocal8Bit());
doDownload(url);
}
*/
}
void FTPManager::downloadFinished(QNetworkReply *reply)
{
QUrl url = reply->url();
if (reply->error())
{
qDebug() << "Download of " << url.toEncoded().constData() << " failed: " << qPrintable(reply->errorString());
} else
{
QString filename = saveFileName(url);
if (saveToDisk(filename, reply))
{
qDebug() << "Download of " << url.toEncoded().constData() << "succeeded saved to " << qPrintable(filename);
}
}
currentDownloads.removeAll(reply);
reply->deleteLater();
if (currentDownloads.isEmpty())
// all downloads finished
QCoreApplication::instance()->quit();
}
void FTPManager::ObjectKaputt(QObject* pObjekt)
{
qDebug() << "Object kaputt ";
}
void FTPManager::authentication()
{
qDebug() << "authentication ";
}
