FTPSend2.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /////////////////////////////////////////////////////////////////////*/
  2. #include "stdafx.h"
  3. #include "CPhotoFTPSend.h"
  4. #include "FTPSend2.h"
  5. #include "CPhotoFTPSendDlg.h"
  6. #include "Shlwapi.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. extern void WriteLogin(CString str);
  13. extern CCPhotoFTPSendDlg *g_pMainWnd;
  14. extern BOOL g_bActive[MAX_THREAD];
  15. extern BOOL g_check1;
  16. extern CString g_mainpath;
  17. //////////////////////////////////////////////////////////////////////
  18. // Construction/Destruction
  19. //////////////////////////////////////////////////////////////////////
  20. #pragma comment(lib, "wininet.lib")
  21. CFTPSend2::CFTPSend2()
  22. {
  23. hFtpConn=hInetSession=NULL;
  24. }
  25. CFTPSend2::~CFTPSend2()
  26. {
  27. LogOffServer();
  28. }
  29. //////////////////////////////////////////////////////////////////////
  30. // Public Functions
  31. //////////////////////////////////////////////////////////////////////
  32. // function to connect & log on to FTP server
  33. // 使用Wininet Socket来连接ftp服务器;
  34. BOOL CFTPSend2::LogOnToServer(CString hostname,int hostport,CString username, CString password)
  35. {
  36. CString szAppName=AfxGetAppName();
  37. hInetSession = InternetOpen(szAppName,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
  38. // InternetSetOption();
  39. hFtpConn=InternetConnect(hInetSession,hostname,hostport,
  40. username,password,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
  41. if(!hFtpConn)
  42. {
  43. InternetCloseHandle(hInetSession);
  44. return 0;
  45. }
  46. return 1;
  47. }
  48. // function to log off & close connection to FTP server
  49. void CFTPSend2::LogOffServer()
  50. {
  51. if(hFtpConn)InternetCloseHandle(hFtpConn);
  52. if(hInetSession)InternetCloseHandle(hInetSession);
  53. hFtpConn=hInetSession=NULL;
  54. }
  55. extern void LoadImageFromBuf(Image **img, BYTE *pData, DWORD leng);
  56. extern void LoadImageFromBuf(Image **img, CString path);
  57. extern CString g_failstr;
  58. extern void RectFitDes(int width, int height, CRect &rc);
  59. extern void SaveImageToFile(Image *img, CString path);
  60. // function to upload/download files
  61. BOOL CFTPSend2::MoveFile(CString remotefile, CString localfile, BOOL blast)
  62. {
  63. if( (!m_ziptype.IsEmpty ()) && blast==0 )
  64. {
  65. ::CreateDirectory (g_mainpath+"\\temp", NULL);
  66. CString timestamp=CTime::GetCurrentTime ().Format ("%Y%m%d%H%M%S");
  67. CString newpath;
  68. newpath.Format ("%s\\temp\\%s%d.jpg", g_mainpath,timestamp, m_pos);
  69. Image *img=NULL;
  70. ::LoadImageFromBuf (&img, localfile);
  71. if(img==NULL)
  72. {
  73. // WriteLogin("载入照片失败,不压缩传送!");
  74. g_failstr="载入照片失败";
  75. return 0;
  76. }
  77. else
  78. {
  79. // WriteLogin("载入图片成功!");
  80. if(img->GetWidth())
  81. {
  82. int newsize=atoi(m_ziptype);
  83. CRect rc(0,0,newsize,newsize);
  84. RectFitDes(img->GetWidth(), img->GetHeight(), rc);
  85. Image *simg= img->GetThumbnailImage(rc.Width (), rc.Height (), NULL, NULL);
  86. Graphics graphic(simg);//防止GetThumbnailImage影响质量
  87. graphic.DrawImage(img, 0,0,simg->GetWidth(), simg->GetHeight() );
  88. delete img;
  89. ::SaveImageToFile(simg, newpath);
  90. delete simg;
  91. localfile=newpath;
  92. }
  93. else
  94. {
  95. // WriteLogin("载入照片失败,不压缩传送!");
  96. g_failstr="载入照片失败";
  97. return 0;
  98. }
  99. }
  100. }
  101. if(blast)
  102. {// 如果文件名为ok2.dat,创建ok2.dat;
  103. if(::PathFileExists(localfile)==0)
  104. {
  105. CFile fp;
  106. if(!fp.Open(localfile, CFile::modeCreate|CFile::modeWrite))
  107. {
  108. DWORD dwError = GetLastError();
  109. WriteTextLog(_T("创建ok2.dat文件失败,路径=%s,错误码=%ld"), localfile, dwError);
  110. return 0;
  111. }
  112. BYTE a[1024];
  113. fp.Write (a, sizeof(BYTE)*1024);
  114. fp.Close();
  115. }
  116. }
  117. if(FtpSetCurrentDirectory(hFtpConn, "\\")==0)
  118. {
  119. g_failstr="打开当前目录失败";
  120. return 0;
  121. }
  122. int pos=remotefile.Find ("\\");
  123. while(pos!=-1)
  124. {
  125. CString dir=remotefile.Left (pos);
  126. if(FtpSetCurrentDirectory(hFtpConn, dir)==0)
  127. {
  128. FtpCreateDirectory(hFtpConn, dir);
  129. if(FtpSetCurrentDirectory(hFtpConn, dir)==0)
  130. {
  131. g_failstr="打开目录失败:"+dir;
  132. return 0;
  133. }
  134. }
  135. remotefile=remotefile.Right (remotefile.GetLength ()-pos-1);
  136. pos=remotefile.Find ("\\");
  137. }
  138. // DWORD s=::GetTickCount ();
  139. // int ret=
  140. // AfxMessageBox(localfile);
  141. // AfxMessageBox(remotefile);
  142. int ret = FtpPutFile(hFtpConn,localfile,remotefile,INTERNET_FLAG_TRANSFER_BINARY ,0);
  143. DWORD dwError = GetLastError();
  144. if ( dwError != 0 )
  145. {
  146. CString strEr ;
  147. strEr.Format(_T("FtpPutFile : %s"), dwError);
  148. AfxMessageBox(strEr);
  149. }
  150. if(blast)
  151. ::DeleteFile (localfile);
  152. return ret;
  153. /* s=::GetTickCount ()-s;
  154. CString ss;
  155. ss.Format ("%d", s/1000);
  156. AfxMessageBox(ss);*/
  157. // return ret;
  158. }
  159. //上传文件失败