1、下载QSSH库文件
https://download.csdn.net/download/m0_65386043/89363147
2、在QT中使用QSSH库文件
将下载的库文件放到程序的同级目录然后再pro文件中添加下列命令即可使用
include($$PWD/../QSsh/QSsh.pri)
3、SSH登录
QSsh::SshConnectionParameters params;
params.port = 端口;
params.userName = 用户名;
params.password = 密码;
params.host = IP;
params.timeout = 60;//超时时间,如果网路不好的话尽量设置稍微大一些
params.authenticationType = QSsh::SshConnectionParameters::AuthenticationTypePassword;
QSsh::SshConnection *m_connection;
m_connection = new QSsh::SshConnection(params, this);
connect(m_connection, SIGNAL(connected()), SLOT(onConnected()));
m_connection->connectToHost();
4、登录成功后会自动触发onConnected函数,建立sftp下载通道
m_sftp = m_connection->createSftpChannel();
if (m_sftp) {
connect(m_sftp.data(), SIGNAL(initialized()),
SLOT(onDownLoadChannelInitialized()));
connect(m_sftp.data(), SIGNAL(channelError(QString)),
SLOT(onDownLoadChannelError(QString)));
connect(m_sftp.data(), SIGNAL(finished(QSsh::SftpJobId, QString)),
SLOT(onDownLoadfinished(QSsh::SftpJobId, QString)));
m_sftp->initialize();
} else {
qDebug() << "SecureDownloader: Error null channel";
}
5、通道建立后,开始下载或上传文件
QSsh::SftpJobId job = m_sftp>downloadFile("远程路径","本地路径", QSsh::SftpOverwriteExisting);//下载
if (job != QSsh::SftpInvalidJob) {
qDebug() << "SecureDownloader: Starting job #" << job;
} else {
qDebug() << "SecureDownloader: Invalid Job";
}
//上传
QSsh::SftpJobId job = m_sftp>uploadFile("本地路径","远程路径", QSsh::SftpOverwriteExisting);
if (job != QSsh::SftpInvalidJob) {
qDebug() << "SecureDownloader: Starting job #" << job;
} else {
qDebug() << "SecureDownloader: Invalid Job";
}
6、问题
使用时遇到了下载到16M后就卡死不下载的情况,有知道的可以指点一下