123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- /////////////////////////////////////////////////////////////////////*/
- #include "stdafx.h"
- #include "CPhotoFTPSend.h"
- #include "FTPSend2.h"
- #include "CPhotoFTPSendDlg.h"
- #include "Shlwapi.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- extern void WriteLogin(CString str);
- extern CCPhotoFTPSendDlg *g_pMainWnd;
- extern BOOL g_bActive[MAX_THREAD];
- extern BOOL g_check1;
- extern CString g_mainpath;
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- #pragma comment(lib, "wininet.lib")
- CFTPSend2::CFTPSend2()
- {
- hFtpConn=hInetSession=NULL;
- }
- CFTPSend2::~CFTPSend2()
- {
- LogOffServer();
- }
- //////////////////////////////////////////////////////////////////////
- // Public Functions
- //////////////////////////////////////////////////////////////////////
- // function to connect & log on to FTP server
- // 使用Wininet Socket来连接ftp服务器;
- BOOL CFTPSend2::LogOnToServer(CString hostname,int hostport,CString username, CString password)
- {
- CString szAppName=AfxGetAppName();
- hInetSession = InternetOpen(szAppName,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
- // InternetSetOption();
- hFtpConn=InternetConnect(hInetSession,hostname,hostport,
- username,password,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
- if(!hFtpConn)
- {
- InternetCloseHandle(hInetSession);
- return 0;
- }
- return 1;
- }
- // function to log off & close connection to FTP server
- void CFTPSend2::LogOffServer()
- {
- if(hFtpConn)InternetCloseHandle(hFtpConn);
- if(hInetSession)InternetCloseHandle(hInetSession);
- hFtpConn=hInetSession=NULL;
- }
- extern void LoadImageFromBuf(Image **img, BYTE *pData, DWORD leng);
- extern void LoadImageFromBuf(Image **img, CString path);
- extern CString g_failstr;
- extern void RectFitDes(int width, int height, CRect &rc);
- extern void SaveImageToFile(Image *img, CString path);
- // function to upload/download files
- BOOL CFTPSend2::MoveFile(CString remotefile, CString localfile, BOOL blast)
- {
- if( (!m_ziptype.IsEmpty ()) && blast==0 )
- {
- ::CreateDirectory (g_mainpath+"\\temp", NULL);
- CString timestamp=CTime::GetCurrentTime ().Format ("%Y%m%d%H%M%S");
- CString newpath;
- newpath.Format ("%s\\temp\\%s%d.jpg", g_mainpath,timestamp, m_pos);
- Image *img=NULL;
- ::LoadImageFromBuf (&img, localfile);
- if(img==NULL)
- {
- // WriteLogin("载入照片失败,不压缩传送!");
- g_failstr="载入照片失败";
- return 0;
- }
- else
- {
- // WriteLogin("载入图片成功!");
- if(img->GetWidth())
- {
- int newsize=atoi(m_ziptype);
- CRect rc(0,0,newsize,newsize);
- RectFitDes(img->GetWidth(), img->GetHeight(), rc);
- Image *simg= img->GetThumbnailImage(rc.Width (), rc.Height (), NULL, NULL);
- Graphics graphic(simg);//防止GetThumbnailImage影响质量
- graphic.DrawImage(img, 0,0,simg->GetWidth(), simg->GetHeight() );
- delete img;
- ::SaveImageToFile(simg, newpath);
- delete simg;
- localfile=newpath;
- }
- else
- {
- // WriteLogin("载入照片失败,不压缩传送!");
- g_failstr="载入照片失败";
- return 0;
- }
- }
- }
- if(blast)
- {// 如果文件名为ok2.dat,创建ok2.dat;
- if(::PathFileExists(localfile)==0)
- {
- CFile fp;
- if(!fp.Open(localfile, CFile::modeCreate|CFile::modeWrite))
- {
- DWORD dwError = GetLastError();
- WriteTextLog(_T("创建ok2.dat文件失败,路径=%s,错误码=%ld"), localfile, dwError);
- return 0;
- }
- BYTE a[1024];
- fp.Write (a, sizeof(BYTE)*1024);
- fp.Close();
- }
- }
- if(FtpSetCurrentDirectory(hFtpConn, "\\")==0)
- {
- g_failstr="打开当前目录失败";
- return 0;
- }
- int pos=remotefile.Find ("\\");
- while(pos!=-1)
- {
- CString dir=remotefile.Left (pos);
- if(FtpSetCurrentDirectory(hFtpConn, dir)==0)
- {
- FtpCreateDirectory(hFtpConn, dir);
- if(FtpSetCurrentDirectory(hFtpConn, dir)==0)
- {
- g_failstr="打开目录失败:"+dir;
- return 0;
- }
- }
- remotefile=remotefile.Right (remotefile.GetLength ()-pos-1);
- pos=remotefile.Find ("\\");
- }
- // DWORD s=::GetTickCount ();
- // int ret=
- // AfxMessageBox(localfile);
- // AfxMessageBox(remotefile);
- int ret = FtpPutFile(hFtpConn,localfile,remotefile,INTERNET_FLAG_TRANSFER_BINARY ,0);
- DWORD dwError = GetLastError();
- if ( dwError != 0 )
- {
- CString strEr ;
- strEr.Format(_T("FtpPutFile : %s"), dwError);
- AfxMessageBox(strEr);
- }
- if(blast)
- ::DeleteFile (localfile);
- return ret;
- /* s=::GetTickCount ()-s;
- CString ss;
- ss.Format ("%d", s/1000);
- AfxMessageBox(ss);*/
- // return ret;
- }
- //上传文件失败
|