#pragma once class CDownloadDlg; class CInternetDownload { public: typedef struct { DWORD nStart;//文件片段的起始位置 DWORD nEnd;//文件片段的長度 size_t nIndex;//當前線程的編號 size_t nTotal; CInternetDownload* pThis; } ThreadParam; CInternetDownload(CDownloadDlg* pDlg); virtual ~CInternetDownload(void); virtual int Download(const CString& strUrl, size_t nThreads); protected: static CString s_strUrl; static CDownloadDlg* s_pDlg; virtual DWORD GetFileLength(const CString& strUrl) = 0; virtual CFile* GetInternetFile(LPVOID) = 0; static UINT ServerWorkerThread(LPVOID lpParam); CInternetSession internetSession; private: CFile* m_internetFile; };
InternetDownload.cpp
#include "StdAfx.h" #include "./internetdownload.h" #include "DownloadDlg.h" CDownloadDlg* CInternetDownload::s_pDlg = NULL; CString CInternetDownload::s_strUrl; CInternetDownload::CInternetDownload(CDownloadDlg* pDlg) : m_internetFile(NULL) { s_pDlg = pDlg; } CInternetDownload::~CInternetDownload(void) { } UINT CInternetDownload::ServerWorkerThread(LPVOID lpParam) { ThreadParam *pParam = static_cast<ThreadParam *>(lpParam); CFile *pFile = pParam->pThis->GetInternetFile(lpParam); if (!pFile) return -1; CFile localFile; CString strFileName; strFileName.Format("c://temp.part%d", pParam->nIndex); if (!localFile.Open(strFileName, CFile::modeCreate | CFile::modeWrite)) return -1; char * const pBuffer = new char[1024]; DWORD nLeft = pParam->nEnd - pParam->nStart + 1; while (TRUE) { int nRead = pFile->Read(pBuffer, nLeft > 1024 ? 1024 : nLeft); localFile.Write(pBuffer, nRead); if (nRead < 1024) break; nLeft -= nRead; } localFile.Close(); delete []pBuffer; pFile->Close(); delete pFile; s_pDlg->PostMessage(WM_DOWNLOADED_MESSAGE, pParam->nTotal, pParam->nIndex); delete pParam; pParam = NULL; return 0; } int CInternetDownload::Download(const CString& strUrl, size_t nThreads) { s_strUrl = strUrl; DWORD nFileSize = GetFileLength(strUrl); UINT nSegmentLength = (nFileSize + nThreads - 1) / nThreads; for (size_t i = 0; i < nThreads; i++) { ThreadParam *lpParam = new ThreadParam; //memset(lpParam, '/0', sizeof(ThreadParam)); lpParam->nTotal = nThreads; lpParam->nIndex = i; lpParam->nStart = nSegmentLength * i; lpParam->nEnd = lpParam->nStart + nSegmentLength - 1; lpParam->pThis = this; if (lpParam->nEnd >= nFileSize) lpParam->nEnd = nFileSize - 1; AfxBeginThread(ServerWorkerThread, static_cast<LPVOID>(lpParam), THREAD_PRIORITY_NORMAL, 0, 0, NULL); //CreateThread(NULL, 0, ServerWorkerThread, (LPVOID)i, 0, 0); } return 0; } #pragma once #include "InternetDownload.h" class CFtpDownload : public CInternetDownload { public: CFtpDownload(CDownloadDlg* pDlg); virtual ~CFtpDownload(void); // int Download(const CString& strUrl, size_t nThreads); private: DWORD GetFileLength(const CString& strUrl); CFile* GetInternetFile(LPVOID lpParam); };
#include "StdAfx.h" #include "./ftpdownload.h" #include "DownloadDlg.h" #include <winsock2.h> #include "shlwapi.h" #pragma comment(lib, "Wininet.lib") #pragma comment(lib, "shlwapi.lib") CFtpDownload::CFtpDownload(CDownloadDlg* pDlg) : CInternetDownload(pDlg) { // s_pDlg = pDlg; } CFtpDownload::~CFtpDownload(void) { } CFile* CFtpDownload::GetInternetFile(LPVOID lpParam) { ThreadParam *pParam = static_cast<ThreadParam *>(lpParam); CFtpConnection *pFtpConn = NULL; CString strServerName, strObject, strUserName, strPassword; DWORD dwServiceType; INTERNET_PORT nPort; if (!AfxParseURLEx(s_strUrl, dwServiceType, strServerName, strObject, nPort, strUserName, strPassword) || dwServiceType != INTERNET_SERVICE_FTP) { TRACE(_T("Not a ftp Quest! /n")); } pFtpConn = internetSession.GetFtpConnection(strServerName, strUserName, strPassword); CString strFtpCommand; strFtpCommand.Format("REST %d", pParam->nStart); if (!FtpCommand(*pFtpConn, FALSE, FTP_TRANSFER_TYPE_ASCII, strFtpCommand, 0, 0)) { int i = WSAGetLastError(); return NULL; } CFile *pFile = pFtpConn->OpenFile(strObject); return pFile; } /* int CFtpDownload::Download(const CString& strUrl, size_t nThreads) { DWORD nFileSize = GetFileLength(strUrl); s_strUrl = strUrl; UINT nSegmentLength = (nFileSize + nThreads - 1) / nThreads; for (size_t i = 0; i < nThreads; i++) { ThreadParam *lpParam = new ThreadParam; //memset(lpParam, '/0', sizeof(ThreadParam)); lpParam->nTotal = nThreads; lpParam->nIndex = i; lpParam->nStart = nSegmentLength * i; lpParam->nEnd = lpParam->nStart + nSegmentLength - 1; lpParam->pThis = this; if (lpParam->nEnd >= nFileSize) lpParam->nEnd = nFileSize - 1; AfxBeginThread(ServerWorkerThread, static_cast<LPVOID>(lpParam), THREAD_PRIORITY_NORMAL, 0, 0, NULL); //CreateThread(NULL, 0, ServerWorkerThread, (LPVOID)i, 0, 0); } return 0; } */ DWORD CFtpDownload::GetFileLength(const CString& strUrl) { CInternetSession internetSession; CFtpConnection *pFtpConn = NULL; CString strServerName, strObject, strUserName, strPassword; DWORD dwServiceType; INTERNET_PORT nPort; if (!AfxParseURLEx(strUrl, dwServiceType, strServerName, strObject, nPort, strUserName, strPassword) || dwServiceType != INTERNET_SERVICE_FTP) { TRACE(_T("Not a ftp Quest! /n")); return -1; } pFtpConn = internetSession.GetFtpConnection(strServerName, strUserName, strPassword); //FtpCommand("REST 1024"); /* if (!FtpCommand(*pFtpConn, FALSE, FTP_TRANSFER_TYPE_ASCII, "REST 1024", 0, 0)) return -1;*/ CInternetFile *pFile = pFtpConn->OpenFile(strObject); DWORD nFileSize = FtpGetFileSize(*pFile, 0); pFile->Close(); delete pFile; return nFileSize; }
#pragma once #include "InternetDownload.h" class CHttpDownload : public CInternetDownload { public: CHttpDownload(CDownloadDlg* pDlg); virtual ~CHttpDownload(void); private: // static UINT ServerWorkerThread(LPVOID lpParam); DWORD GetFileLength(const CString& strUrl); CFile* GetInternetFile(LPVOID lpParam); public: // int Download(const CString& strUrl, int nThreads); int FormatRequestHeader(void); }; #include "StdAfx.h" #include "./httpdownload.h" #include "DownloadDlg.h" static const char szHeaders[] = "Accept: */*/r/nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)/r/n"; CFile* CHttpDownload::GetInternetFile(LPVOID lpParam) { ThreadParam *pParam = static_cast<ThreadParam *>(lpParam); DWORD dwFlag = INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD; CString strHeader;//請求頭 strHeader.Format("%sRange: bytes=%d-%d/r/n", szHeaders, pParam->nStart, pParam->nEnd);//pParam->nIndex * 8192);//, 1024 * 1024); return internetSession.OpenURL(s_strUrl, 1, dwFlag, strHeader, -1); } /* UINT CHttpDownload::ServerWorkerThread(LPVOID lpParam) { //size_t nIndex = reinterpret_cast<size_t>(lpParam);//第幾個文件片段 ThreadParam *pParam = static_cast<ThreadParam *>(lpParam); char * const pBuffer = new char[1024]; //for (int i = 0; ; i++) while (TRUE) { int nRead = pHttpFile->Read(pBuffer, 1024); localFile.Write(pBuffer, nRead); if (nRead < 1024) break; } localFile.Close(); delete []pBuffer; pHttpFile->Close(); delete pHttpFile; s_pDlg->PostMessage(WM_DOWNLOADED_MESSAGE, pParam->nTotal, pParam->nIndex); delete pParam; return 0; } */ CHttpDownload::CHttpDownload(CDownloadDlg* pDlg) : CInternetDownload(pDlg) //: m_strHeader("Accept: */*/r/nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows XP)/r/n") { } CHttpDownload::~CHttpDownload(void) { } /* int CHttpDownload::Download(const CString& strUrl, int nThreads) { s_strUrl = strUrl; DWORD nFileSize = GetFileLength(strUrl); UINT nSegmentLength = (nFileSize + nThreads - 1) / nThreads; for (size_t i = 0; i < nThreads; i++) { ThreadParam *lpParam = new ThreadParam; //memset(lpParam, '/0', sizeof(ThreadParam)); lpParam->nTotal = nThreads; lpParam->nIndex = i; lpParam->nStart = nSegmentLength * i; lpParam->nEnd = lpParam->nStart + nSegmentLength - 1; if (lpParam->nEnd >= nFileSize) lpParam->nEnd = nFileSize - 1; AfxBeginThread(ServerWorkerThread, static_cast<LPVOID>(lpParam), THREAD_PRIORITY_NORMAL, 0, 0, NULL); //CreateThread(NULL, 0, ServerWorkerThread, (LPVOID)i, 0, 0); } return 0; } */ int CHttpDownload::FormatRequestHeader(void) { // char szHeader[1024]; return 0; } DWORD CHttpDownload::GetFileLength(const CString& strUrl) { CInternetSession sess; DWORD dwFlag = INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD; s_strUrl = strUrl; CHttpFile* pHttpFile = (CHttpFile* )sess.OpenURL(s_strUrl, 1, dwFlag); if (!pHttpFile) return -1; CString strLength; pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, strLength); pHttpFile->Close(); delete pHttpFile; return _ttoi(strLength); } // DownloadDlg.cpp : 實現文件 // #include "stdafx.h" #include "Download.h" #include "DownloadDlg.h" #include "./downloaddlg.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // 用於應用程序“關於”菜單項的 CAboutDlg 對話框 class CAboutDlg : public CDialog { public: CAboutDlg(); // 對話框數據 enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 實現 protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() // CDownloadDlg 對話框 CDownloadDlg::CDownloadDlg(CWnd* pParent /*=NULL*/) : CDialog(CDownloadDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_pDownloader = NULL; } void CDownloadDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } //#define WM_XXXX 10O //const static int WM_DOWNLOADED_MESSAGE = WM_USER+100; BEGIN_MESSAGE_MAP(CDownloadDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_MESSAGE(WM_DOWNLOADED_MESSAGE, OnDownloadMessage) ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1) ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2) END_MESSAGE_MAP() // CDownloadDlg 消息處理程序 BOOL CDownloadDlg::OnInitDialog() { CDialog::OnInitDialog(); // 將/“關於.../”菜單項添加到系統菜單中。 // IDM_ABOUTBOX 必須在系統命令範圍內。 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 設置此對話框的圖標。當應用程序主窗口不是對話框時,框架將自動 // 執行此操作 SetIcon(m_hIcon, TRUE); // 設置大圖標 SetIcon(m_hIcon, FALSE); // 設置小圖標 // TODO: 在此添加額外的初始化代碼 // GetDlgItem(IDC_EDIT1)->SetWindowText("http://codeproject.com/");//http://192.168.8.246:8080/sogou_wubi_15b.exe GetDlgItem(IDC_EDIT1)->SetWindowText("ftp://me:me@192.168.69.86/inet.h");//ftp://192.168.14.130/InstallPack/5.9.5.x/5.9.5.976/5.9.5.976.txt"); GetDlgItem(IDC_EDIT2)->SetWindowText("4"); return TRUE; // 除非設置了控件的焦點,否則返回 TRUE } void CDownloadDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // 如果向對話框添加最小化按鈕,則需要下面的代碼 // 來繪製該圖標。對於使用文檔/視圖模型的 MFC 應用程序, // 這將由框架自動完成。 void CDownloadDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用於繪製的設備上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // 使圖標在工作矩形中居中 int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // 繪製圖標 dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } //當用戶拖動最小化窗口時系統調用此函數取得光標顯示。 HCURSOR CDownloadDlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); } #include "InternetDownload.h" #include "FtpDownload.h" #include "HttpDownload.h" void CDownloadDlg::OnBnClickedButton1() { CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST1); for (int i = pListBox->GetCount() - 1; i >= 0; i--) { pListBox->DeleteString( i ); } //downloader.Download("http://192.168.8.246:8080/sogou_wubi_15b.exe"); CString strUrl; GetDlgItem(IDC_EDIT1)->GetWindowText(strUrl); CString strThreads; CWnd* pEditCtrl = GetDlgItem(IDC_EDIT2); pEditCtrl->GetWindowText(strThreads); if (!strUrl.IsEmpty() && !strThreads.IsEmpty()) { GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE); GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE); CString strServerName, strObject, strUserName, strPassword; DWORD dwServiceType; INTERNET_PORT nPort; if (!AfxParseURLEx(strUrl, dwServiceType, strServerName, strObject, nPort, strUserName, strPassword)) { TRACE(_T("Not a valid Quest! /n")); GetDlgItem(IDC_EDIT2)->EnableWindow(); GetDlgItem(IDC_EDIT2)->EnableWindow(); } if (dwServiceType == INTERNET_SERVICE_HTTP) { m_pDownloader = new CHttpDownload(this); m_pDownloader->Download(strUrl, _ttoi(strThreads)); } else if (dwServiceType = INTERNET_SERVICE_FTP) { m_pDownloader = new CFtpDownload(this); m_pDownloader->Download(strUrl, _ttoi(strThreads)); } else { TRACE(_T("Not a valid Quest! /n")); GetDlgItem(IDC_EDIT2)->EnableWindow(); GetDlgItem(IDC_EDIT2)->EnableWindow(); } //delete pDownloader; } else { AfxMessageBox("please input url & thread number"); return; } } LRESULT CDownloadDlg::OnDownloadMessage(WPARAM wParam, LPARAM lParam) { CString strInfo; strInfo.Format("part %d complete.", lParam); CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST1); pListBox->InsertString(pListBox->GetCount(), strInfo); CString strThreads; CWnd* pEditCtrl = GetDlgItem(IDC_EDIT2); pEditCtrl->GetWindowText(strThreads); if (pListBox->GetCount() == wParam) { //MergeFile(); CFile resultFile("c://temp.exe", CFile::modeCreate | CFile::modeWrite); if (!resultFile) return -1; char buffer[1024]; for (size_t i = 0; i < wParam; i++) { int nLength; CString strFileName; strFileName.Format("c:/temp.part%d", i); CFile tempFile(strFileName, CFile::modeRead); //nLength = file2.GetLength(); if (!tempFile) { AfxMessageBox("error: cannot find.."); return -1; } while ( (nLength = tempFile.Read(buffer, 1024)) > 0) { resultFile.Write(buffer, nLength); if (nLength < 1024) break; } tempFile.Close(); } resultFile.Close(); pListBox->InsertString(wParam, "Merge Ok!"); GetDlgItem(IDC_EDIT1)->EnableWindow(); GetDlgItem(IDC_EDIT2)->EnableWindow(); delete m_pDownloader; m_pDownloader = NULL; } return 0; } void CDownloadDlg::OnBnClickedButton2() { CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST1); for (int i = pListBox->GetCount() - 1; i >= 0; i--) { pListBox->DeleteString( i ); } }
一、 背景 最近在做大模型相關的項目,其中有個模塊需要提取在線視頻語音爲文本並輸出給用戶。作爲一個純後端Jave工程師,搞這個確實是初次嘗試。 二、 調研 基於上述功能模塊,主要有三大任務:1、 提取網頁中的視頻 2、 視頻轉語音 3、 語
本文分享自華爲雲社區《JDBC連接openGauss6.0和PostgreSQL16.2性能對比》,作者: Gauss松鼠會小助手。 PostgreSQL vs openGauss 01 前置準備 安裝JDK: 詳細安裝步驟請問度娘,輸
本文介紹了 InnoDB 支持哪幾類表鎖,以及它們分別都用在什麼場景下,還介紹了其中兩類表鎖爲什麼要存在。 作者:操盛春,愛可生技術專家,公衆號『一樹一溪』作者,專注於研究 MySQL 和 OceanBase 源碼。 愛可生開源社區出品,
原方法 /** * 動態更新form * @param form */ updateForm(form) { this.form.manholeId = form.manholeId; this.form
詳解 binlog 時間戳與 exec_time 的關係。 作者:李錫超,蘇商銀行DBA,負責數據庫和中間件運維和建設。擅長 MySQL、Python、Oracle,愛好騎行、技術研究和分享。 愛可生開源社區出品,原創內容未經授權不得隨意
背景 最近領導分配了個任務,測試sit環境一些功能相比之前慢了許多,需要優化一下。 問題排查過程 瀏覽器F12查看相關接口的響應,看到底是哪個接口反應慢,根據互聯網的要求,頁面3秒還沒有顯示出來,用戶體驗會非常差。 查看相關代碼的提交
JSON簡介: JSON(Java Script Object Notation)是一種輕量級的數據交換格式,通常用於在不同系統之間傳輸數據。它基於 JavaScript 對象語法,但已成爲一種獨立於語言的格式。JSON 數據以鍵值對的形式
一、簡單介紹: CaffeineCache和Guava的Cache是應用廣泛的本地緩存。 在開發中,爲了達到降低依賴、提高訪問速度的目的。會使用它存儲一些維表接口的返回值和數據庫查詢結果,在有些場景下也會在分佈式緩存上再加上一層本地緩存,
作者:vivo 互聯網大前端團隊 - Ma Lian 本文主要描述了FileProvider,startAnyWhere實現,Parcel不對稱漏洞以及這三者結合產生的漏洞利用實戰,另外闡述了漏洞利用的影響和修復預防措施,這個漏洞波及了
本文分享自華爲雲社區《GaussDB SQL基本語法示例-CASE表達式》,作者:Gauss松鼠會小助手2。 一、前言 SQL是用於訪問和處理數據庫的標準計算機語言。GaussDB支持SQL標準(默認支持SQL2、SQL3和SQL4的主要
Impala目前支持Hadoop中幾種常見的文件格式 Parquet 、 ORC 、 Text 、 Avro 、 RCFile 和 SequenceFile 。下面簡要說明各種格式的使用、限制和一些注意事項。 不同的文件格式有着不同的適用場
1-前言 本文根據實際遇到的線程池使用導致的性能問題,從代碼層面解析 線程池 核心線程數、最大線程數、工作隊列三個參數配置不佳容易產生的問題,以及對這些問題的建議 對線程池的更多解析,這篇文章講得已經比較詳細了,建議大家仔細研讀:《阿里規
爲了更好地分類閱讀 52im.net 總計 1000 多篇精編文章,我將在每週三推送新的一期技術文集,本次是第 3 期。 第 1 篇 [標題] 高性能網絡編程 (一):單臺服務器併發 TCP 連接數到底可以有多少 [鏈接] http:
上篇從服務粒度角度進行了探討,本文繼續從服務內的分層角度探討。 本文的觀點源自我在學習與實踐過程中的深思熟慮,尚處於不斷探索和驗證的階段。希望能“拋磚引玉”,激發更多的討論與交流。讓我們共同進步,在探討與實證中尋求真知。 一、背景 應用
註冊賬號 https://uums.tianditu.gov.cn/register 登錄已有賬號 https://sso.tianditu.gov.cn/login?service=https%3A%2F%2Fuums.tiandi