• 5481阅读
  • 7回复

[讨论]qhttp 下载完的文件保存在哪里了? [复制链接]

上一主题 下一主题
离线zzxap
 

只看楼主 倒序阅读 楼主  发表于: 2011-03-14
一个网站内容下载的类和实现
高手请直接看最后几行
httpget.h---------------------------------

#ifndef HTTPGET_H

#define HTTPGET_H

#include <QObject>

class QUrl;

class QHttp;

class QFile;

class HttpGet : public QObject {

Q_OBJECT

public:

HttpGet(QObject *parent = 0);

void downloadFile(const QUrl &url);

signals:

void finished();

private slots:

void done(bool error);

private:

QHttp *http;

QFile *file;

};

#endif

httpget.cpp------------------------------------

#include <QtNetwork>

#include <QFile>

#include <iostream>

#include "HttpGet.h"

using namespace std;

HttpGet::HttpGet(QObject *parent) : QObject(parent) {

http = new QHttp(this);

connect(http, SIGNAL(done(bool)), this, SLOT(done(bool)));

}

void HttpGet::downloadFile(const QUrl &url) {

QFileInfo fileInfo(url.path());

QString fileName = fileInfo.fileName();

if (fileName.isEmpty()) {

fileName = "index.html";

}

file = new QFile(fileName);

if (!file->open(QIODevice::WriteOnly)) {

cerr << "Unable to save the file" << endl;

delete file;

file = 0;

return;

}

http->setHost(url.host(), url.port(80));

http->get(url.path(), file);

http->close();

}

void HttpGet::done(bool error) {

if (error) {

cerr << "Error: " << qPrintable(http->errorString()) << endl;

} else {

cerr << "File downloaded as " << qPrintable(file->fileName())

<< endl;

}

file->close();

delete file;

file = 0;

emit finished();

}

main.cpp------------------------------

#include <QtGui/QApplication>

#include "mainwindow.h"

#include <QTextCodec>

#include <QCoreApplication>

#include <QUrl>

#include "HttpGet.h"

#include <iostream>

int main(int argc, char *argv[])

{

QApplication a(argc, argv);

QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));

HttpGet getter;

getter.downloadFile(QUrl("http://lvpengcheng.com.cn/index.htm"));
//qhttp 下载完的文件保存在哪里了?
return a.exec();

}
离线wxj120bw

只看该作者 1楼 发表于: 2011-03-14
你查看了你程序下debug或者release目录下的文件了吗?
离线zzxap

只看该作者 2楼 发表于: 2011-03-15
引用第1楼wxj120bw于2011-03-14 23:07发表的  :
你查看了你程序下debug或者release目录下的文件了吗?


项目中根本就没这两个文件夹
离线uidab

只看该作者 3楼 发表于: 2011-03-15
回 楼主(zzxap) 的帖子
你总要设置一下保存路径吧。
有时候为了工作直接获得答案,而我却失去了思考的乐趣!


飘啊飘,何时能安居!
离线zzxap

只看该作者 4楼 发表于: 2011-03-15
Re:回 楼主(zzxap) 的帖子
引用第3楼uidab于2011-03-15 11:15发表的 回 楼主(zzxap) 的帖子 :
你总要设置一下保存路径吧。


在哪里设?
离线wxj120bw

只看该作者 5楼 发表于: 2011-03-15
你用的是什么开发工具 还有downloadFile函数 建立楼主 把代码理清 再发问
离线83888788
只看该作者 6楼 发表于: 2011-03-15
楼主在你的
file = new QFile(fileName);

代码后面加一句
qDebug()<<file.fileName();
这样便可以查看路径了

还有楼主的fileName最好还是绝对路径
下面的代码你可以参考一下
  1.     QUrl url=.......
  2.         QString savePath=QFileDialog::getExistingDirectory(this,tr("Choose the path"),"."
  3.                                       ,QFileDialog::ShowDirsOnly|QFileDialog::DontResolveSymlinks);
  4.         QString localFileName =QFileInfo(url.path()).fileName();
  5.         if(localFileName.isEmpty())
  6.             localFileName="rename.out";
  7.         savePath+="/";
  8.         localFileName=savePath+localFileName;
  9.         localFileName=QDir::toNativeSeparators(localFileName);
  10.     file=new QFile(localFileName);


离线zzxap

只看该作者 7楼 发表于: 2011-03-17
file = new QFile(你想要保存的目录+fileName);就可以了
快速回复
限100 字节
 
上一个 下一个