// uploadingDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "uploading.h"
#include "uploadingDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CuploadingDlg 对话框
CuploadingDlg::CuploadingDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CuploadingDlg::IDD, pParent)
, m_OpTypeString(_T(""))
, m_usernameString(_T(""))
, m_passwordString(_T(""))
, m_dataBaseString(_T(""))
, m_hostString(_T(""))
, m_IpAddressString(_T(""))
, m_InnoString(_T(""))
, m_szFilepPathString(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CuploadingDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT5, m_OpType);
DDX_Text(pDX, IDC_EDIT5, m_OpTypeString);
DDX_Control(pDX, IDC_EDIT1, m_username);
DDX_Text(pDX, IDC_EDIT1, m_usernameString);
DDX_Control(pDX, IDC_EDIT2, m_password);
DDX_Text(pDX, IDC_EDIT2, m_passwordString);
DDX_Control(pDX, IDC_EDIT3, m_dataBase);
DDX_Text(pDX, IDC_EDIT3, m_dataBaseString);
DDX_Control(pDX, IDC_EDIT4, m_host);
DDX_Text(pDX, IDC_EDIT4, m_hostString);
DDX_Control(pDX, IDC_EDIT6, m_IpAddress);
DDX_Text(pDX, IDC_EDIT6, m_IpAddressString);
DDX_Control(pDX, IDC_INNO, m_Inno);
DDX_Text(pDX, IDC_INNO, m_InnoString);
DDX_Control(pDX, IDC_EDIT7, m_szFilepPath);
DDX_Text(pDX, IDC_EDIT7, m_szFilepPathString);
}
BEGIN_MESSAGE_MAP(CuploadingDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, &CuploadingDlg::OnBnClickedOk)
ON_BN_CLICKED(IDC_BUTTON1, &CuploadingDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
// CuploadingDlg 消息处理程序
BOOL CuploadingDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
m_IpAddress.SetWindowTextA("127.0.0.1");
m_host.SetWindowTextA("1521");
m_OpType.SetWindowTextA("jpg");
m_dataBase.SetWindowTextA("orcl");
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CuploadingDlg::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
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CuploadingDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
UINT Send(LPVOID lParam)
{
CuploadingDlg *pthis = (CuploadingDlg*)lParam;
int i=0;
int errorcount = 0;
for ( i=0;i<pthis->v.size();++i)
{
if(pthis->SendPhoto(pthis->v[i]) != 0 )
{
errorcount++;
continue;
}
CString temp;
temp.Format("正在处理第%d条",i+1);
pthis->m_Inno.SetWindowTextA(temp);
pthis->GetDlgItem(IDOK)->EnableWindow(FALSE);
}
CString temp;
temp.Format("处理完成,共有%d条数据,正确处理%d条,错误数据%d条",pthis->v.size(),pthis->v.size() - errorcount,errorcount);
pthis->m_Inno.SetWindowTextA(temp);
pthis->GetDlgItem(IDOK)->EnableWindow(TRUE);
if (pthis->pRect)
{
pthis->pRect->Close();
pthis->pRect = NULL;
}
if (pthis->pConn)
{
pthis->pConn->Close();
pthis->pConn = NULL;
}
}
void CuploadingDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
//CDialogEx::OnOK();
UpdateData(TRUE);
if (m_usernameString.IsEmpty() || m_passwordString.IsEmpty() || m_IpAddressString.IsEmpty() || m_hostString.IsEmpty() || m_dataBaseString.IsEmpty() || m_OpTypeString.IsEmpty() || m_szFilepPathString.IsEmpty())
{
return ;
}
std::vector<CString>().swap(v);
TraverseDir(m_szFilepPathString, v);
if(OpenDataBase() != 0 )return ;
CWinThread *IninThread = AfxBeginThread(Send,this);
}
void CuploadingDlg::TraverseDir(CString& dir,std::vector<CString>& vec)//深度遍历文件夹
{
CFileFind ff;
if (dir.Right(1) != "\\")
{
dir += "\\";
}
dir += "*.*";
BOOL ret = ff.FindFile(dir);
while (ret)
{
ret = ff.FindNextFileA();
if (ff.IsDirectory() && !ff.IsDots())
{
CString path = ff.GetFilePath();
TraverseDir(path,vec);
}
if (!ff.IsDirectory() && !ff.IsDots())
{
CString name = ff.GetFileName();
CString path = ff.GetFilePath();
int dotpos = name.ReverseFind(‘.‘);
CString fileExt = name.Right(name.GetLength() - dotpos-1);
m_OpTypeString = "";
m_OpType.GetWindowTextA(m_OpTypeString);
m_OpTypeString.MakeLower();
if (fileExt == m_OpTypeString)
{
vec.push_back(path);
}
}
}
ff.Close();
}
int CuploadingDlg::SendPhoto(CString photoPath)
{
CStdioFile file;
if (file.Open(photoPath,CFile::modeRead|CFile::typeBinary) == FALSE)
{
Writelog("打开文件失败:%s",photoPath);
return -1;
}
long filelen = file.GetLength();
byte *fielBuffer = new byte[filelen+1];
byte *oldfieldBuffer = fielBuffer;
file.Read(fielBuffer,filelen);
file.Close();
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = filelen;
SAFEARRAY *safeArray;
safeArray = SafeArrayCreate(VT_UI1,1,rgsabound);
for (long i=0;i< filelen;i++)
{
SafeArrayPutElement(safeArray,&i,fielBuffer++);
}
VARIANT varBLOB;
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = safeArray;
if (fielBuffer)
{
delete[] oldfieldBuffer;
fielBuffer = NULL;
}
CString str;
try
{
CString Index;
str.Format("select faceid from (select * from userface order by faceid desc) where rownum=1");
_variant_t vt;
pRect = pConn->Execute(_bstr_t(str),&vt,adCmdText);
if (vt.lVal < 1)
{
Index = "0";
}else
{
Index = LPSTR(_bstr_t(pRect->GetCollect("faceid")));
}
CString filename;
int dotpos = photoPath.ReverseFind(‘.‘);
filename = photoPath.Mid(0,dotpos);
dotpos = filename.ReverseFind(‘\\‘);
filename = filename.Right(filename.GetLength() - dotpos-1);
str.Format("insert into userface(faceid,USERID,FEATURE) values(%d,‘%s‘,‘000‘)",atoi(Index)+1,filename);
pRect = pConn->Execute(_bstr_t(str),&vt,adCmdText);
if (vt.lVal < 1)
{
Writelog("插入数据失败:%s",str);
SafeArrayDestroy(safeArray);
if (pRect)
{
pRect->Close();
pRect = NULL;
}
if (pConn)
{
pConn->Close();
pConn = NULL;
}
return -1;
}
str.Format("select * from userface where faceid = %d",atoi(Index) +1);
HRESULT hr;
hr = pRect->Open(str.GetBuffer(0),(IDispatch*)pConn,adOpenDynamic,adLockOptimistic,adCmdText);
hr = pRect->GetFields()->GetItem("IMAGE")->AppendChunk(varBLOB);
if (hr == S_OK)
{
pRect->Update();
pRect->Requery(0);
}
SafeArrayDestroy(safeArray);
if (pRect)
{
pRect->Close();
pRect = NULL;
}
}
catch (_com_error &e)
{
Writelog("SendCommand捕获异常:%s",e.ErrorMessage());
if (pRect)
{
pRect->Close();
pRect = NULL;
}
if (pConn)
{
pConn->Close();
pConn = NULL;
}
return -1;
}
return 0;
}
int CuploadingDlg::OpenDataBase(void)
{
pConn.CreateInstance(__uuidof(Connection));//连接对像
pRect.CreateInstance(__uuidof(Recordset));//记录集对象
CString strConn;
strConn.Format("Provider=OraOLEDB.Oracle.1;User ID=%s;Password=%s;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=%s))(CONNECT_DATA=(SERVICE_NAME=%s)))",m_usernameString,m_passwordString,m_IpAddressString,m_hostString,m_dataBaseString);
HRESULT rValue = pConn->Open(_bstr_t(strConn),"","",adModeUnknown);
if (rValue == S_FALSE)
{
MessageBox(_T("打开失败"));
return -1;
}
return 0;
}
void CuploadingDlg::Writelog(char* buf,...)
{
try
{
CString sdlog;
m_szFilepPath.GetWindowTextA(m_szFilepPathString);
if (m_szFilepPathString.IsEmpty())
{
sdlog =" C:\\Send";
}else
{
sdlog = m_szFilepPathString;
}
sdlog += "\\log\\";
CreateDirectory(sdlog, NULL);
long i=0;
char tmp[32],tmpfile[256];
//char tmpPrint[4096]={0};
char buffer[1524]={0};
char Data[1024]={0};
FILE *fp;
SYSTEMTIME SystemTimeStruct;
::GetLocalTime(&SystemTimeStruct);
sprintf(tmp,"%04d%02d%02d ",SystemTimeStruct.wYear,SystemTimeStruct.wMonth,
SystemTimeStruct.wDay);
sprintf(tmpfile,"%s%s.log",sdlog,tmp);
fp=fopen(tmpfile,"at");
if (fp == NULL)
{
return;
}
va_list arglist;
va_start(arglist, buf);
vsprintf(&Data[strlen(Data)], buf, arglist);
va_end(arglist);
sprintf(buffer,"%02d:%02d:%02d %s",SystemTimeStruct.wHour,SystemTimeStruct.wMinute,
SystemTimeStruct.wSecond,Data);
fputs(buffer,fp);
fputs("\n",fp);
fclose(fp);
}
catch(...)
{
return;
}
}
void CuploadingDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
char szPath[MAX_PATH];
ZeroMemory(szPath,sizeof(szPath));
BROWSEINFO bi;
bi.hwndOwner = m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szPath;
bi.lpszTitle = "请选择文件夹";
bi.ulFlags = 0;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
LPITEMIDLIST lp = SHBrowseForFolder(&bi);
if (lp && SHGetPathFromIDList(lp,szPath))
{
m_szFilepPathString.Format("%s",szPath);
m_szFilepPath.SetWindowTextA(m_szFilepPathString);
}
}