vc ftp定时传输Word下载.docx

上传人:b****6 文档编号:19846317 上传时间:2023-01-10 格式:DOCX 页数:11 大小:17.44KB
下载 相关 举报
vc ftp定时传输Word下载.docx_第1页
第1页 / 共11页
vc ftp定时传输Word下载.docx_第2页
第2页 / 共11页
vc ftp定时传输Word下载.docx_第3页
第3页 / 共11页
vc ftp定时传输Word下载.docx_第4页
第4页 / 共11页
vc ftp定时传输Word下载.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

vc ftp定时传输Word下载.docx

《vc ftp定时传输Word下载.docx》由会员分享,可在线阅读,更多相关《vc ftp定时传输Word下载.docx(11页珍藏版)》请在冰豆网上搜索。

vc ftp定时传输Word下载.docx

服务器接收文件名规则:

\timsdata\210\traffic\tr20060418.rpt

读取当天时间20060419则上传前一天的数据也就是20060418.rpt—〉tr20060418.rpt

定时器每隔10分钟检测一次,是否到了3:

00或3:

30。

可以判断最后一位是否为0在判断,倒数第二位是否是0或3或者其他算法均可。

总之如果到了3:

00就开始上传文件。

3.上传文件:

根据配置文件中组合的地址,上传所有局的话务数据。

4.日志:

对上传成功失败记录日志;

对程序启动时间记录日志。

并形成文本文件在该程序所在目录下

二.具体实现:

1.配置文件:

配置文件根据需求设置如下:

[path]

MAX_ROW=9

path=D:

/tims/timsdata/

subpath=/traffic/

[office]

officename1=111

officename2=222

officename3=333

officename4=444

[Server]

IP=111.111.1.1

port=21

username=***

pwd=***

path1=/******

path2=/******

officename1=SZDH

officename2=SZGR

officename3=TDIR

officename4=TDER

subpath=/spcdata

[startup]

time=15:

36

[firstcreate]

flag=0

2.定时功能由VC中的SetTimer()和OnTimer()来实现:

voidCFTPClientView:

:

OnInitialUpdate()

{

CView:

OnInitialUpdate();

SetTimer(1,600000,NULL);

}

OnTimer(UINTnIDEvent)

........

if(nHour==nTime)

{

if(nMinute==Minute)

if(bTimer==TRUE)

bTimer=FALSE;

CDataTxtsyslog;

syslog.WriteLogFile("

ftp上传程序启动"

);

//写日志

AfxBeginThread((AFX_THREADPROC)StartRun,&

m_nFileStatus);

}

else

bTimer=TRUE;

3.链接服务器:

BOOLCDataTxt:

Connection()

CStringhostname;

inthostport;

CStringusername;

CStringpassword;

//读配置文件FTPClient.ini

chara[255];

CStrings;

GetPrivateProfileString("

Server"

"

IP"

"

a,255,"

./FTPClient.ini"

s.Format("

%s"

a);

hostname=s;

port"

21"

hostport=atoi(s);

username"

username=s;

pwd"

password=s;

//连接FTPserver

pInetSession=newCInternetSession(AfxGetAppName,(),1,PRE_CONFIG_INTERNET_ACCESS);

try

pFtpConnection=pInetSession->

GetFtpConnection(hostname,username,password,hostport,FALSE);

catch(CInternetException*pEx)

pEx->

Delete();

pFtpConnection=NULL;

LogOffServer();

//断开链接

returnfalse;

WriteLogFile("

ftp登录成功成功"

returntrue;

LogOffServer()

{//断开链接

if(pFtpConnection!

=NULL)

pFtpConnection->

Close();

deletepFtpConnection;

deletepInetSession;

断开与服务器的连接"

returnTRUE;

voidCDataTxt:

WriteLogFile(CStringlog)

{//写日志

FILE*fp;

CHARstrhead[30]="

;

SYSTEMTIMEst;

GetLocalTime(&

st);

memset(strhead,0,sizeof(strhead));

sprintf(strhead,"

%4d-%02d-%02d%02d:

%02d:

%02d"

st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);

fp=fopen("

./syslog.txt"

"

a+"

if(fp!

fwrite(strhead,sizeof(CHAR),strlen(strhead),fp);

fwrite(log,sizeof(CHAR),strlen(log),fp);

fwrite("

\n"

1,1,fp);

fclose(fp);

3.上传文件:

非常简单用一个函数就解决了:

pFtpConnection->

PutFile(localfile,remotefile)//其中localfile是从配置文件中读取的本地文件所在目录文件名,remotefile是服务器端要创建的目录以及文件名.

//判断文件是否存在:

CFileFindfinder;

if(!

finder.FindFile(localfile))

{returnFALSE;

//在服务器端创建文件所用函数:

CreateDirectory(s);

//s为目录名,需要注意的是文件已经创建时,再创建就会返回false

以上只是给出了主要功能的部分实现代码,还请大家多多指教啊!

publicfinalclassFTPExample

publicstaticfinalStringUSAGE=

"

Usage:

ftp[-s][-b]<

hostname>

<

username>

password>

remotefile>

localfile>

+

\nDefaultbehavioristodownloadafileanduseASCIItransfermode.\n"

\t-sstorefileonserver(upload)\n"

\t-busebinarytransfermode\n"

publicstaticfinalvoidmain(String[]args)

intbase=0;

booleanstoreFile=false,binaryTransfer=false,error=false;

Stringserver,username,password,remote,local;

FTPClientftp;

for(base=0;

base<

args.length;

base++)

if(args[base].startsWith("

-s"

))

storeFile=true;

elseif(args[base].startsWith("

-b"

binaryTransfer=true;

break;

if((args.length-base)!

=5)

System.err.println(USAGE);

System.exit

(1);

server=args[base++];

username=args[base++];

password=args[base++];

remote=args[base++];

local=args[base];

ftp=newFTPClient();

ftp.addProtocolCommandListener(newPrintCommandListener(

newPrintWriter(System.out)));

intreply;

ftp.connect(server);

System.out.println("

Connectedto"

+server+"

."

//Afterconnectionattempt,youshouldcheckthereplycodetoverify

//success.

reply=ftp.getReplyCode();

if(!

FTPReply.isPositiveCompletion(reply))

ftp.disconnect();

System.err.println("

FTPserverrefusedconnection."

catch(IOExceptione)

if(ftp.isConnected())

catch(IOExceptionf)

//donothing

Couldnotconnecttoserver."

e.printStackTrace();

__main:

ftp.login(username,password))

ftp.logout();

error=true;

break__main;

Remotesystemis"

+ftp.getSystemName());

if(binaryTransfer)

ftp.setFileType(FTP.BINARY_FILE_TYPE);

//Usepassivemodeasdefaultbecausemostofusare

//behindfirewallsthesedays.

ftp.enterLocalPassiveMode();

if(storeFile)

InputStreaminput;

input=newFileInputStream(local);

ftp.storeFile(remote,input);

input.close();

OutputStreamoutput;

output=newFileOutputStream(local);

ftp.retrieveFile(remote,output);

output.close();

catch(FTPConnectionClosedExceptione)

Serverclosedconnection."

finally

System.exit(error?

1:

0);

}//endmain

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 其它语言学习

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1