系统相关
首页 > 系统相关> > Windows下的Qt程序使用wget命令下载文件

Windows下的Qt程序使用wget命令下载文件

作者:互联网

wget 非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性.如果是由于网络的原因下载失败,wget会不断的尝试,直到整个文件下载完毕。如果是服务器打断下载过程,它会再次联到服务器上从停止的地方继续下载。这对从那些限定了链接时间的服务器上下载大文件非常有用。

1.命令格式:

wget  [参数]  [URL地址]

2.示例代码:

QString command  = QString("wget -O \"%1/%2\" --no-check-certificate \"%3\"").arg(desktop_dir).arg(file_name).arg(download_url);
int ret = download_command(command.toLatin1().data());
if (ret) {
   QFile file("download_file.log");
   file.open(QIODevice::ReadOnly);
   QTextStream text_stream(&file);
   QString file_info = text_stream.readAll();
   m_text_log->setText(file_info);
   file.remove();
   file.close();
}
int DownloadFileDialog::download_command(const char *command)
{
    SECURITY_ATTRIBUTES sa;
    sa.nLength = sizeof(sa);
    sa.lpSecurityDescriptor = NULL;
    sa.bInheritHandle = TRUE;

    HANDLE h = CreateFile(L"download_file.log",
        FILE_WRITE_DATA,
        FILE_SHARE_WRITE | FILE_SHARE_READ,
        &sa,
        OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL);

    PROCESS_INFORMATION pi;
    STARTUPINFO si;
    BOOL ret = FALSE;
    DWORD flags = CREATE_NO_WINDOW;

    ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
    ZeroMemory(&si, sizeof(STARTUPINFO));
    si.cb = sizeof(STARTUPINFO);
    si.dwFlags |= STARTF_USESTDHANDLES;
    si.hStdInput = NULL;
    si.hStdError = h;
    si.hStdOutput = h;

    wchar_t cmd[1024] = {0};
    mbstowcs(cmd, command, strlen(command) + 1);
    ret = CreateProcess(NULL, cmd, NULL, NULL, TRUE, flags, NULL, NULL, &si, &pi);
    if (ret)
    {
        WaitForSingleObject(pi.hProcess, INFINITE);

        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
        CloseHandle(h);

        return 1;
    }
    CloseHandle(h);

    return -1;
}

 下载源码请点击这里

 

标签:pi,Qt,Windows,si,command,file,wget,sa,NULL
来源: https://www.cnblogs.com/QingYiShouJiuRen/p/16546449.html