资源简介
qt关于获取网络图片并保存在本地,运用QNetworkAccessManager,QNetworkReply,QNetworkRequest
代码片段和文件信息
#include “http_network.h“
#include
#include ject>
#include
#include
#include
#include
http_network ::http_network(Qobject *parent)
:Qobject(parent)m_reply(0)
{
m_network = new QNetworkAccessManager(this);
}
http_network::~http_network()
{
if(m_reply)
{
// m_reply->disconnect(this);
m_reply->deleteLater();
m_reply = 0;
}
}
void http_network::download(const QString &url)
{
QUrl qurl(url);
QNetworkRequest request;
// QSslConfiguration config;
QSslConfiguration conf = request.sslConfiguration();
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
conf.setProtocol(QSsl::TlsV1SslV3);
request.setSslConfiguration(conf);
request.setUrl(qurl);
if(m_reply)
{
m_reply->disconnect(this);
m_reply->deleteLater();
m_reply = 0;
}
m_reply = m_network->get(request);
connect(m_replySIGNAL(readyRead())thisSLOT(onReadyRead()));
connect(m_replySIGNAL(error(QNetworkReply::NetworkError))
thisSLOT(onerror(QNetworkReply::NetworkError)));
connect(m_replySIGNAL(finished())thisSLOT(onfinished()));
connect(m_replySIGNAL(sslErrors(QList))
thisSLOT(onSslError(QList)));
}
void http_network::onReadyRead()
{
data.append( m_reply->readAll());//图片过大是每次读一点,一定要记得拼接
qDebug()<<“readyread“< }
void http_network::onerror(QNetworkReply::NetworkError error)
{
m_reply->disconnect(this);
m_reply->deleteLater();
m_reply = 0;
qDebug()<<“http_error“< }
void http_network::onfinished()
{
QString string = data;
// QPixmap pixmap;
// pixmap.loadFromData(data);
QFile file(QDir::currentPath()+“/network/picname/1.jpg“);//文件的名字
if(file.exists())
{
qDebug()<<“file has exinsts“< qDebug()<
}else
{
QDir dir(QDir::currentPath()+“/network/picname“);
dir.mkpath(QDir::currentPath()+“/network/picname“);//创建多级目录
if(file.open(QFile::WriteOnly))
{
file.write(data);
file.close();
}
}
m_reply->disconnect(this);
m_reply->deleteLater();
m_reply = 0;
qDebug()<<“download finished“< }
void http_network::onSslError(QList)
{
qDebug()<<“sslerror“< }
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2542 2018-03-02 18:49 net_workAccessManager\http_network.cpp
文件 620 2018-03-02 15:40 net_workAccessManager\http_network.h
文件 257 2018-03-02 18:36 net_workAccessManager\main.cpp
文件 231 2018-03-02 16:09 net_workAccessManager\net_workAccessManager.pro
文件 23998 2018-03-02 16:36 net_workAccessManager\net_workAccessManager.pro.user
目录 0 2018-03-02 18:49 net_workAccessManager
----------- --------- ---------- ----- ----
27648 6
评论
共有 条评论