• Libevent::evhttp服务器下载


    void http_handler_Get_Download(struct evhttp_request *req, void *arg)
    {
        if (req == NULL)
        {
            return;
        }
        const char *uri = evhttp_request_get_uri(req);
        string strUrl(uri);
    
        string strFilePath = DPC::get_Url_path(strUrl, "path=");
        printf("FilePath = %s 
    ", strFilePath.c_str());
    
        string strFileName = DPC::get_FileName_path(strUrl, "/");
        strFileName = "attachment; filename=" + strFileName;
    
        //查看文件是否存在
        if (access(strFilePath.c_str(), 0) == -1)
        {
            evhttp_send_error(req, HTTP_BADREQUEST, 0);
            return;
        }
        //=========
        std::ifstream t;
        t.open(strFilePath.c_str(), ios::in | ios::binary);
        t.seekg(0, std::ios::end);
        long length = t.tellg();
        t.seekg(0, std::ios::beg);
    
        char *buffer = new char[length];
        t.read(buffer, length);
        t.close();
    
        char FileSize[10] = { '' };
        snprintf(FileSize, sizeof(FileSize), "%d", length);
    
        time_t timep;
        time(&timep);
        char s[50];
        sprintf(s, ctime(&timep));
        std::string strDate = std::string(s, s + (strlen(s) - 1));
        evhttp_add_header(evhttp_request_get_output_headers(req), "Server", "Unix");
        evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "application/octet-stream; charset=utf-8");
        evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Disposition", strFileName.c_str());
        evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Length", FileSize);
        evhttp_add_header(evhttp_request_get_output_headers(req), "Date", strDate.c_str());
        evhttp_add_header(evhttp_request_get_output_headers(req), "Connection", "close");
        //=========
    
        //回响应
        struct evbuffer *retbuff = NULL;
        retbuff = evbuffer_new();
        if (retbuff == NULL)
        {
            return;
        }
        evbuffer_add(retbuff, buffer, length);
        //evbuffer_add_printf(retbuff, "123");
        evhttp_send_reply(req, HTTP_OK, "Client", retbuff);
        evbuffer_free(retbuff);
    }
  • 相关阅读:
    运算符优先级问题
    文件操作工具,需者自取
    Text文档编码识别方法
    删除重复文件的程序
    修道士和野人问题
    猜数字游戏
    存储器层级图
    IL指令汇总
    输入1~8,每个数字不重复
    厦门大学线下编程比赛第一题:求和
  • 原文地址:https://www.cnblogs.com/osbreak/p/10247991.html
Copyright © 2020-2023  润新知