?
//读取配置文件方法
CString config(CString title,CString column)
{
?? ??? ?CString iniFilePath = "C://二次上传//config.ini";
?? ??? ??? ??? ?DWORD ret;
?? ??? ??? ??? ?FILE * fp = fopen((CT2A)(iniFilePath), "r");
?? ??? ??? ??? ?CString returnString;
?? ??? ??? ??? ?TCHAR szValue[MAX_PATH + 1] = _T("");
?? ??? ??? ??? ?if (fp) {
?? ??? ??? ??? ??? ?ret = GetPrivateProfileString(title,column, _T(""), szValue, MAX_PATH, iniFilePath);
?? ??? ??? ??? ??? ?returnString = szValue;
?? ??? ??? ??? ??? ?//OutputDebugString(returnString);
?? ??? ??? ??? ??? ?return returnString;
?? ??? ??? ??? ?}
}
//调用方法
CString IP = config("WHTK","IP");
//配置文件内容
[WHTK]
IP=192.168.191.163
//日志文件输出方法
void WriteLog(char * szLog)
{
?? ?SYSTEMTIME st;
?? ?GetLocalTime(&st);
?? ?FILE *fp;
?? ?char * folderPath = "C:\\二次上传\\logs";?
? ? if (!PathIsDirectory(folderPath)) ?// 是否有重名文件夹
? ? {
? ? ? ? ::CreateDirectory(folderPath, 0);
? ? }
?? ?char * Path = "\\";?
?? ?time_t t = time(NULL);
?? ?char ch[64] = {0};
?? ?strftime(ch, sizeof(ch) - 1, "%Y-%m-%d", localtime(&t));
?? ?char * logPath = "log.txt";?
?? ?char test[100];
?? ?strcpy_s(test,folderPath);
?? ?strcat_s(test,Path);
?? ?strcat_s(test,ch);
?? ?strcat_s(test,logPath);
?? ?int n = strlen(test);
?? ?fp = fopen(test, "at");
?? ?//fp = fopen(("C:\\logs\\log.txt"), "at");
?? ?fprintf(fp, "%d-%d-%d %d:%d:%d:%d ?",st.wYear,st.wMonth,st.wDay,st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
?? ?fprintf(fp, szLog);
?? ?fclose(fp);
?? ?OutputDebugStringA(szLog);
}
//调用方法
WriteLog("获取到未上传文件\n");
?
//连接FTP 上传文件
BOOL FTP_Upload(char *szHostName, char *szUserName, char *szPassword, char *szUrlPath, char *FilePath)
{
HINTERNET hInternet, hConnect, hFTPFile = NULL;
DWORD dwBytesReturn = 0;
DWORD UploadDataSize = 0;
BYTE *pUploadData = NULL;
DWORD dwRet, bRet = 0;
hInternet = ::InternetOpen("WinInet Ftp Upload V1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
hConnect = ::InternetConnect(hInternet, szHostName, INTERNET_INVALID_PORT_NUMBER, szUserName, szPassword,
INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
hFTPFile = ::FtpOpenFile(hConnect, szUrlPath, GENERIC_WRITE, FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_RELOAD, NULL);
HANDLE hFile = ::CreateFile(FilePath, GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ |
FILE_SHARE_WRITE, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE, NULL);//每调用一次CreateFile函数都会打开一个文件内核对象用于管理文件,每个内核对象内部都维护了一个文件指针,表示应该在哪里执行下一次同步读取或写入操作。
if (INVALID_HANDLE_VALUE == hFile)
return FALSE;
UploadDataSize = ::GetFileSize(hFile, NULL);
pUploadData = new BYTE[UploadDataSize];
::ReadFile(hFile, pUploadData, UploadDataSize, &dwRet, NULL);
UploadDataSize = dwRet;
bRet = ::InternetWriteFile(hFTPFile, pUploadData, UploadDataSize, &dwBytesReturn);
if (FALSE == bRet)
{
delete[]pUploadData;
return FALSE;
}
delete[]pUploadData;
InternetCloseHandle(hInternet);
InternetCloseHandle(hConnect);
InternetCloseHandle(hFTPFile);
//SetEndOfFile(hFile);函数会根据文件对象的文件指针当前所在的位置来截断文件的大小或增大文件的大小
CloseHandle(hFile);//关闭当前打开的文档流
return TRUE;
}
//调用示例 IP地址,用户名,密码,upfilepath上传路径,filepath本地路径
BOOL bRET = FTP_Upload((LPSTR)(LPCSTR)IP, (LPSTR)(LPCSTR)NAME, (LPSTR)(LPCSTR)PWD, (LPSTR)(LPCSTR)upfilepath,(LPSTR)(LPCSTR)filepath);
?
UINT SetTimer(UINT nIDEvent,UINT nElapse,void(CALLBACK EXPORT *lpfnTimer)(HWND,UINT ,DWORD));
CString strtype ="1";
//定时器函数
void CTwoUploadDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
switch (nIDEvent)
{
case 1:
// 如果收到ID为1的定时器的消息则调用func1函数
// func1();
if(strtype=="1")
{
CString log="";
//获取需要上传的目录文件
CString strPath="C://Erro//*.jpg";
CFileFind find;//文件搜索类对象
BOOL gofind =find.FindFile(strPath);//开启查找
while(gofind)
{
WriteLog("获取到未上传文件\n");
gofind =find.FindNextFile();
CString filename =find.GetFileName();
CString filepath = find.GetFilePath();
log="文件路径:"+filepath+"\n";
WriteLog((LPSTR)(LPCSTR)log);
CString upfilepath = config("WHTK","UPFILE")+filename;
log="上传目录:"+ upfilepath +"\n";
WriteLog((LPSTR)(LPCSTR)log);
CString IP = config("WHTK","IP");
CString NAME = config("WHTK","NAME");
CString PWD = config("WHTK","PWD");
BOOL bRET = FTP_Upload((LPSTR)(LPCSTR)IP, (LPSTR)(LPCSTR)NAME, (LPSTR)(LPCSTR)PWD, (LPSTR)(LPCSTR)upfilepath,(LPSTR)(LPCSTR)filepath);
if(bRET)
{
DeleteFile(filepath);
log="删除目录文件:"+ filepath +"\n";
WriteLog((LPSTR)(LPCSTR)log);
// find.Close();关闭文件
/* char *savePath =(LPSTR)(LPCSTR)filepath;
if(remove(savePath)==0)
{
AfxMessageBox(_T("删除成功!"));
}
else
{
AfxMessageBox(_T("删除失败!"));
}*/
//remove(filepath);
}
else
{
WriteLog("连接失败上传失败\n");
}
WriteLog("***************************************************\n");
}
}
else
{
KillTimer(nIDEvent);//关闭定时器
}
break;
case 2:
// 如果收到ID为2的定时器的消息则调用func2函数
// fun2();
// KillTimer(nIDEvent);
break;
default:
break;
}
CDialogEx::OnTimer(nIDEvent);
}
//按钮点击事件调用方案
void CTwoUploadDlg::OnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
/*CTime timeNow;
CString strTime;
timeNow = CTime::GetCurrentTime();
strTime = timeNow.Format("%Y/%m/%d %H:%M:%S");*/
CString str;
GetDlgItemText(IDC_BUTTON1, str);
if (str == _T("开始"))
{
SetDlgItemText(IDC_BUTTON1, _T("暂停"));
GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE);
strtype="1";
CString str;
GetDlgItemText(IDC_EDIT2,str);
SetTimer(1,atoi(str) *60000,NULL);
}
else
{
SetDlgItemText(IDC_BUTTON1, _T("开始"));
GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE);
//BOOL KillTimer(UINT_PTR nIDEvent);
strtype="2";
SetTimer(1,1000,NULL);
}
}
|