校园动态 更多>>>
 学习贯彻党的二十届三中全...
 利州中专召开行政会
 铭记历史、勿忘国耻、珍爱...
 勿忘国耻 警钟长鸣——学...
 利州中专召开“清廉学校”...
 利州中专教师四支部开展“...
  当前位置:首页 >> 校园动态 >> FTP文件上传源代码(部分,原发)
FTP文件上传源代码(部分,原发)

信息来源:利州中专   更新时间:2006-04-19 00:00:00   点击:2832   [双击滚动]
//---------------------------------------------------------------------------
#include <vcl.h>

#pragma hdrstop

#include "UMFTP.h"

#include "ULogin.h"

#include "UUpLoad.h"

#include "UAttr.h"

#include "UAbout.h"

#include "UMsg.h"

#include "UNewFloder.h"

#include "UReName.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma link "SUIButton"

#pragma link "SUIForm"

#pragma link "SUIImagePanel"

#pragma link "SUIListView"

#pragma link "SUIStatusBar"

#pragma link "SUIToolBar"

#pragma link "SUITreeView"

#pragma link "SUIPopupMenu"

#pragma resource "*.dfm"

TFFTP *FFTP;

//---------------------------------------------------------------------------

__fastcall TFFTP::TFFTP(TComponent* Owner)

: TForm(Owner)

{

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::suiButton1Click(TObject *Sender)

{

if(suiButton1->Caption=="连接服务器")

{if(LoginToServer())suiButton1->Caption="断开服务器" ;

}

else

{F->Disconnect();

Connected=false;

suiButton1->Caption="连接服务器" ;

}

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::FormShow(TObject *Sender)

{

FileList=new TList();

P->Panels->Items[0]->Text="未连接";

P->Panels->Items[1]->Text="";

P->Panels->Items[2]->Text="";

suiListView1->Enabled=false;

suiButton2->Enabled=false;

suiButton3->Enabled=false;

suiButton4->Enabled=false;

suiButton5->Enabled=false;

suiButton6->Enabled=false;

suiButton7->Enabled=false;

suiButton9->Enabled=false;

suiButton10->Enabled=false;

Connected=false;

IsBusy=false;

}

//---------------------------------------------------------------------------

bool __fastcall TFFTP::LoginToServer(void)

{ bool s;

s=false;

FLogin=new TFLogin(NULL);

if(FLogin->ShowModal()==mrOk)//用户登录

{ZuoYe=Trim(FLogin->suiEdit1->Text);

StuName=Trim(FLogin->suiEdit3->Text);

if(!Connected){F->Connect();Connected=true;}

F->ChangeDir("/");

ClearList();F->List();

if(CkZuoYe())

{F->ChangeDir("/"+ZuoYe);

ClearList();F->List();

if(CkUser()==false)F->MakeDirectory(StuName);

P->Panels->Items[0]->Text="已连接";

P->Panels->Items[1]->Text="作业号:"+ZuoYe;

P->Panels->Items[2]->Text="姓名:"+StuName;

F->ChangeDir(StuName);

Statue=1;

ClearList();F->List();

UserPath=F->CurrentDir;

suiListView1->Enabled=true;

SelectItem=NULL;IsBusy=false;

s=true;}

else

{ShowMessage("你输入的作业号有误,请重新输入!");Statue=0;}

}

delete FLogin;

return s;

}

void __fastcall TFFTP::FSuccess(TCmdType Trans_Type)

{ FileAttr *D;

AnsiString s;

switch(Trans_Type)

{ case cmdList:

if(Statue==1)//用户列表

{ListToView();

P->Panels->Items[3]->Text="文件数:"+IntToStr(FileList->Count);

SelectItem=NULL;}

break;

case cmdChangeDir:

if(UserPath==F->CurrentDir)

{suiButton9->Enabled=false;

suiButton10->Enabled=false;

}

else

{suiButton9->Enabled=true;

suiButton10->Enabled=true;

}

case cmdMakeDir:

break;

}

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::FConnect(TObject *Sender)

{

Statue=0;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::FDisconnect(TObject *Sender)

{

P->Panels->Items[0]->Text="未连接";

P->Panels->Items[1]->Text="";

P->Panels->Items[2]->Text="";

P->Panels->Items[3]->Text="";

P->Panels->Items[4]->Text="";

suiListView1->Enabled=false;

SelectItem=NULL;

suiButton2->Enabled=false;

suiButton3->Enabled=false;

suiButton4->Enabled=false;

suiButton5->Enabled=false;

suiButton6->Enabled=false;

suiButton7->Enabled=false;

suiButton9->Enabled=false;

suiButton10->Enabled=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::FListItem(AnsiString Listing)

{FileAttr *File;

File=new FileAttr;

SplitFileName(Listing,File);

FileList->Add((void *)File);

}

//---------------------------------------------------------------------------

bool __fastcall TFFTP::CkZuoYe(void)

{//检查作业号是否存在

FileAttr *D;bool j=false;

for(int i=0;i<FileList->Count;i++)

{D=(FileAttr *)FileList->Items[i];

if(D->Name==ZuoYe&&D->IsDir)

{j=true;break;}

}

return j;

}

bool __fastcall TFFTP::CkUser(void)

{//检查用户文件夹是否存在

FileAttr *D;bool j=false;

for(int i=0;i<FileList->Count;i++)

{D=(FileAttr *)FileList->Items[i];

if(D->Name==StuName&&D->IsDir)

{j=true;break;}

}

return j;

}

void __fastcall TFFTP::SplitFileName(AnsiString S, FileAttr * F)

{//划分文件信息

AnsiString Dir;

F->Date=S.SubString(0,8);

F->Time=S.SubString(11,7);

Dir=S.SubString(25,5);

if(Trim(Dir)=="<DIR>")

{F->IsDir=true;

F->Size=-1;}

else

{F->IsDir=false;

F->Size=StrToInt(Trim(S.SubString(31,8)));}

F->Name=Trim(S.SubString(39,S.Length()-38));

}

void __fastcall TFFTP::FormClose(TObject *Sender, TCloseAction &Action)

{ClearList();

delete FileList;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::ClearList(void)

{//清除文件列表

FileAttr *D;

for(int i=0;i<FileList->Count;i++)

{D=(FileAttr *)FileList->Items[i];delete D;D=NULL;}

FileList->Clear();

}

void __fastcall TFFTP::FFailure(bool &Handled, TCmdType Trans_Type)

{

//

IsBusy=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::ListToView(void)

{ //列表到ListView

FileAttr *D;

TListItem *V;

suiListView1->Items->Clear();

for(int i=0;i<FileList->Count;i++)

{D=(FileAttr *)FileList->Items[i];

V=suiListView1->Items->Add();

V->Caption=D->Name;

V->ImageIndex=D->IsDir;

}

}

void __fastcall TFFTP::suiButton2Click(TObject *Sender)

{//上传文件

if(IsBusy)return;

IsBusy=true;

if(OpenDialog1->Execute())

{FUpLoad->suiProgressBar1->Position=0;

FUpLoad->Show();

FUpLoad->Label1->Caption="正在上传文件:"+ExtractFileName(OpenDialog1->FileName)+",请稍候。。。";

F->Mode(MODE_IMAGE);

F->Upload(OpenDialog1->FileName,ExtractFileName(OpenDialog1->FileName));

ClearList();F->List();

FUpLoad->Hide();

}

IsBusy=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::FormResize(TObject *Sender)

{

if(Width<540)Width=540;

if(Height<200)Height=200;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::suiListView1MouseDown(TObject *Sender,

TMouseButton Button, TShiftState Shift, int X, int Y)

{

TPoint P,P1;

TListItem *p;p=GetItem(X,Y);

if(Button==mbRight)

{if(p){suiListView1->PopupMenu=HasSelect;}

else {suiListView1->PopupMenu=NoSelect;}

P.x=X;P.y=Y;

P1=suiListView1->ClientToScreen(P);

suiListView1->PopupMenu->Popup(P1.x,P1.y);}

}

//---------------------------------------------------------------------------

TListItem * __fastcall TFFTP::GetItem(int X, int Y)

{ //找到位于X,Y处的item,并标记为选取

TListItem *p,*p1;TRect S;p1=NULL;

for(int i=0;i<suiListView1->Items->Count;i++)

{p=suiListView1->Items->Item[i];S=p->DisplayRect(drBounds);

if(X>S.Left&&X<S.Right&&Y<S.Bottom&&Y>S.Top){p1=p;p1->Selected=true;break;}}

SelectItem=p1;

return p1;

}

void __fastcall TFFTP::SetSelectItem(TListItem * value)

{ AnsiString Path;

Path=F->CurrentDir.SubString(UserPath.Length()+1,F->CurrentDir.Length()-UserPath.Length());

if(value==NULL)

{P->Panels->Items[4]->Text="当前位置:/"+Path;

suiButton2->Enabled=true;

suiButton3->Enabled=true;

suiButton4->Enabled=false;

suiButton5->Enabled=false;

suiButton6->Enabled=false;

suiButton7->Enabled=false;

}

else

{P->Panels->Items[4]->Text=value->Caption;

if(value->ImageIndex==1)//文件夹

{suiButton4->Enabled=false;

suiButton5->Enabled=true;

suiButton6->Enabled=false;

suiButton7->Enabled=true;

N11->Enabled=true;

N10->Enabled=false;

}

else //文件

{suiButton4->Enabled=true;

suiButton5->Enabled=false;

suiButton6->Enabled=true;

suiButton7->Enabled=false;

N11->Enabled=false;

N10->Enabled=true;

}

}

FSelectItem=value;

}

TListItem * __fastcall TFFTP::GetSelectItem()

{return FSelectItem;}

void __fastcall TFFTP::N10Click(TObject *Sender)

{//文件属性

FileAttr * D;

D=NULL;

if(SelectItem==NULL)return;

if(SelectItem->ImageIndex==1)return;

for(int i=0;i<FileList->Count;i++)

{D=(FileAttr *)FileList->Items[i];

if(D->Name==SelectItem->Caption)break;

}

if(D!=NULL)

{FAttrib->Label1->Caption="文件名:"+D->Name;

FAttrib->Label2->Caption="文件大小:"+IntToStr(D->Size)+"字节";

FAttrib->Label3->Caption="创建时间:"+D->Date+" "+D->Time;

FAttrib->Show();

}

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::suiButton3Click(TObject *Sender)

{//上传文件夹

if(IsBusy)return;

IsBusy=true;

AnsiString Path;

bool (__stdcall *OpenWin)(LPTSTR)=NULL;

HINSTANCE hlnst=LoadLibrary("SelectPath.dll");

if(hlnst)

{

(FARPROC)OpenWin=GetProcAddress(hlnst,"SelectPath");

if(OpenWin)

{ char a[200];

if(OpenWin(a))

{Path=AnsiString(a);

FUpLoad->suiProgressBar1->Position=0;

FUpLoad->Show();

UpDir(Path);

ClearList();F->List();

FUpLoad->Hide();

}

}

FreeLibrary(hlnst);

}

IsBusy=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::suiButton4Click(TObject *Sender)

{//删除文件

if(SelectItem==NULL)return;

if(IsBusy)return;

IsBusy=true;

if(SelectItem->ImageIndex==0)

{F->Delete(SelectItem->Caption);

ClearList();F->List();}

IsBusy=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::suiButton5Click(TObject *Sender)

{//删除文件夹

if(SelectItem==NULL)return;

if(IsBusy)return;

IsBusy=true;

if(SelectItem->ImageIndex==1)

{FMsg->Label2->Caption="文件夹:"+SelectItem->Caption;

if(FMsg->ShowModal()==mrOk)

{Statue=0;

DelDir(SelectItem->Caption);

Statue=1;

ClearList();F->List();}

}

IsBusy=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::suiButton6Click(TObject *Sender)

{

//下载文件

if(SelectItem==NULL)return;

if(IsBusy)return;

IsBusy=true;

if(SelectItem->ImageIndex==0)

{SaveDialog1->FileName=SelectItem->Caption;

if(SaveDialog1->Execute())

{FUpLoad->suiProgressBar1->Position=0;

FUpLoad->Show();

FUpLoad->Label1->Caption="正在下载文件:"+ExtractFileName(SaveDialog1->FileName)+",请稍候。。。";

F->Mode(MODE_IMAGE);

F->Download(SelectItem->Caption,SaveDialog1->FileName);

FUpLoad->Hide();

}

}

IsBusy=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::suiButton7Click(TObject *Sender)

{//下载文件夹

if(IsBusy)return;

IsBusy=true;

IsBusy=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::suiButton8Click(TObject *Sender)

{//版权信息

FAbout->Show();

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::N3Click(TObject *Sender)

{//重新列表

if(IsBusy)return;

IsBusy=true;

ClearList();F->List();

IsBusy=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::N11Click(TObject *Sender)

{//打开文件夹

if(SelectItem==NULL)return;

if(IsBusy)return;

IsBusy=true;

if(SelectItem->ImageIndex==1)

{F->ChangeDir(SelectItem->Caption);

ClearList();F->List();}

IsBusy=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::N5Click(TObject *Sender)

{//下载

if(SelectItem==NULL)return;

if(SelectItem->ImageIndex==1)//文件夹下载

{suiButton7Click(Sender);}

else

{suiButton6Click(Sender);}

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::N7Click(TObject *Sender)

{//删除

if(SelectItem==NULL)return;

if(SelectItem->ImageIndex==1)//文件夹删除

{suiButton5Click(Sender);}

else

{suiButton4Click(Sender);}

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::FPacketRecvd(TObject *Sender)

{//收到数据

FUpLoad->suiProgressBar1->Max=F->BytesTotal/1000;

FUpLoad->suiProgressBar1->Position=F->BytesRecvd/1000;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::FPacketSent(TObject *Sender)

{//发送数据

FUpLoad->suiProgressBar1->Max=F->BytesTotal/1000;

FUpLoad->suiProgressBar1->Position=F->BytesSent/1000;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::N12Click(TObject *Sender)

{

//新建文件夹

if(IsBusy)return;

IsBusy=true;

if(FNewFloder->ShowModal()==mrOk)

{F->MakeDirectory(Trim(FNewFloder->suiEdit1->Text));

ClearList();F->List();

}

IsBusy=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::N13Click(TObject *Sender)

{

//重新命名

if(SelectItem==NULL)return;

if(IsBusy)return;

IsBusy=true;

FCgName->suiEdit1->Text=SelectItem->Caption;

FCgName->suiEdit2->Text=SelectItem->Caption;

if(FCgName->ShowModal()==mrOk)

{F->Rename(SelectItem->Caption,Trim(FCgName->suiEdit2->Text));

ClearList();F->List();}

IsBusy=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::N4Click(TObject *Sender)

{//转到上一级目录

if(IsBusy)return;

IsBusy=true;

if(UserPath!=F->CurrentDir)

{F->ChangeDir("..");

ClearList();F->List();}

IsBusy=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::N14Click(TObject *Sender)

{//转到根目录

if(IsBusy)return;

IsBusy=true;

F->ChangeDir(UserPath);

ClearList();F->List();

IsBusy=false;

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::suiListView1DblClick(TObject *Sender)

{

//双击,文件夹打开,文件下载

if(SelectItem==NULL)return;

if(SelectItem->ImageIndex==0)

{suiButton6Click(Sender);}

else

{N11Click(Sender);}

}

//---------------------------------------------------------------------------

void __fastcall TFFTP::UpDir(AnsiString Path)

{//上传文件夹

AnsiString tmpName;

AnsiString NewPath;

NewPath=ExtractFileName(Path);

F->MakeDirectory(NewPath);

F->ChangeDir(NewPath);

TSearchRec sr,sf;

int iAttributes = 0;

iAttributes |= faArchive;

iAttributes |= faDirectory;

if(FindFirst(Path#########)

{do

{tmpName=Trim(sr.Name);

if(tmpName=="."||tmpName=="..")continue;

if((sr.Attr & faDirectory) == sr.Attr)

{F->MakeDirectory(tmpName);

F->ChangeDir(tmpName);

UpDir(Path######);

F->ChangeDir("..");}

else

{FUpLoad->Label1->Caption="正在上传文件:"+tmpName;

F->Upload(Path+"\\"+tmpName,tmpName);}

}

while(FindNext(sr)==0);

FindClose(sr);

}

F->ChangeDir("..");

}

void __fastcall TFFTP::DelDir(AnsiString Path)

{//删除文件夹

int s;

FileAttr * D;

F->ChangeDir(Path);

do {

s=0;

ClearList();

F->List();

for(int i=0;i<FileList->Count;i++)

{if(s==1)continue;

D=(FileAttr *)FileList->Items[i];

if(D->IsDir){DelDir(D->Name);s=1;}

else F->Delete(D->Name);

}

}

while (s!=0);

F->ChangeDir("..");

F->RemoveDir(Path);

}

打印本页 | 关闭窗口 
广元市利州中等专业学校 版权所有    地址:四川省广元市利州区上西坝天后路113号 邮编:628017 电话:0839-3604791
蜀ICP备19022898号   川公网安备 51080202000039号
技术支持:四川维诚信息技术有限公司    网站维护:利州中专信息中心 联系人:陶波 QQ:41376242