文件传输 (100分)

  • 主题发起人 主题发起人 steven.xu
  • 开始时间 开始时间
S

steven.xu

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠,请指教如何编程实现文件传输?(最好能给我一个实例)非常感谢!!!
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Psock, NMSTRM;

type
TForm1 = class(TForm)
NMStrm1: TNMStrm;
NMStrmServ1: TNMStrmServ;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure NMStrmServ1MSG(Sender: TComponent; const sFrom: String;
strm: TStream);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
MyStream: TMemoryStream;
begin
MyStream:= TMemoryStream.Create;
try
NMStrm1.Host:='127.0.0.1';
MyStream.LoadFromFile('c:/test.exe');
NMStrm1.FromName:='c:/test1.exe'; //发送一个文件名
NMStrm1.PostIt(MyStream);
finally
MyStream.Free;
end;
end;

procedure TForm1.NMStrmServ1MSG(Sender: TComponent; const sFrom: String;
strm: TStream);
var
MyStream: TMemoryStream;
begin
MyStream := TMemoryStream.Create;
MyStream.LoadFromStream(strm);
MyStream.SaveToFile(sFrom);
end;

end.
 
请这为大虾给我一个在DELPHI环境下用UDP或FTP编写的程序来实现文件传输。(由于本人
太菜,请注明要用到那些控件,万分感谢!!!现在积分有限,将来一定重谢!)
 
UDP,FTP?
其实用Stream(FastNet下的控件NMStrm,NMStrmServ)就很好实现
看看Delphi自带的一个例子吧。在Delphi/demo/fastnet/strm目录里
就是实现的文件传输。
 
给个通俗的:(ftp客户方用fastnet下的nmftp控件)
nmftp1.Host:=xxx.xxx.xxx.xxx;
nmftp1.port:=21;
nmftp1.Connect;
nmftp1.userid:=xxx;
nmftp1.password:=xxx;
nmftp1.reportlevel:=1;
nmftp1.timeout:=xxxx;
nmftp1.Download(ftp服务器上的文件,存为本地哪个文件); (下载)
nmftp1.Abort;(暂停下载)
nmftp1.Downloadrestore(ftp服务器上的文件,存为本地哪个文件);(下载的断点续传)
nmftp1.upload(xxxxx,xxxxx); (上传)
nmftp1.upload(xxx.xxxx); (上传的断点续传)
如果你想自己做一个ftp服务器,delphi没有自带的控件
可以用ics控件组上的ftpserver控件
 
我自己写了一个,速度还不错,应该符合你的要求!
 
又找到个socket传文件的例子
----------------------------------------------
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls;

type
TCon = record
FileName : String;
TotalSize : Integer;
Status : Integer;
end;

PCON = ^TCON;

TForm1 = class(TForm)
SS: TServerSocket;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure SSClientConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure SSClientRead(Sender: TObject; Socket: TCustomWinSocket);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
SS.Port := 9000;
SS.Active := True;
end;

procedure TForm1.SSClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var c : pcon;
begin

c :=new(pcon);
c.FileName := '';
c.TotalSize := 0 ;
c.Status := 0;
Socket.Data := c;
Socket.SendText('已经连接,请输入UPLOAD FILENAME SIZE'#13#10);

end;

procedure TForm1.SSClientRead(Sender: TObject; Socket: TCustomWinSocket);
var C : PCON;
cmd:String;
Buffer : pointer;
nRetr : integer;
fs : TFileStream;
const bufferSize = 1024 ;

begin
C:= Socket.Data ;
case c.Status of
0 :
begin
cmd := trim(Socket.ReceiveText) ;

if Pos('UPLOAD ',uppercase(cmd)) > 0 then
begin
c.FileName := trim(Copy(cmd,Pos(' ',cmd)+1,Length(cmd)));
c.TotalSize := StrToInt(Copy(c.FileName,Pos(' ',c.FileName)+1,Length(c.FileName)));
c.FileName := trim(Copy(c.FileName,1,Pos(' ',c.FileName)));
c.Status := 1;
Socket.Data := C;
Socket.SendText('you can send File !'#13#10);
end;
end;
1 : begin
GetMem(Buffer,BufferSize);
nRetr := Socket.ReceiveBuf(Buffer^,BufferSize);

if not FIleExists('c:/'+c.FileName) then
begin
fs :=TFileStream.Create('c:/'+c.FileName,fmCreate or fmShareDenyNone);
fs.Seek(0,soFromBeginning);
end
else
begin
fs :=TFileStream.Create('c:/'+c.FileName,fmOpenWrite or fmShareDenyNone);
fs.Seek(0,soFromEnd);
end;

fs.WriteBuffer(Buffer^,nRetr);

fs.Destroy;
FreeMem(Buffer);
end;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Form2.Show;
end;

end.




--------------------------------------------------------------------------------
来自:唐晓锋 时间:99-11-30 01:17:19 ID:162654
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;

type
TForm2 = class(TForm)
CS: TClientSocket;
OpenDialog1: TOpenDialog;
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
SendCommand: TButton;
Label1: TLabel;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure SendCommandClick(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure CSRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.DFM}
function GetFileSize(const FileName: string):integer;
var f : TFileStream;
begin
f := TFileStream.Create(FileName,fmOpenRead or fmShareDenyNone);
Result :=f.Size;
F.Free;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
with OpenDialog1 do
begin
Execute;
if FileName <> '' then
begin
Edit1.Text := 'UPLOAD '+ ExtractFileName(FileName) +' '+Inttostr(GetFileSize(FileName));
Label1.Caption := FileName;
cs.Socket.SendText(edit1.Text);
end;
end;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
CS.Active := True;

end;

procedure TForm2.SendCommandClick(Sender: TObject);
var fs : TFileStream;
Buf : pointer;

begin
//CS.Socket.SendText(Edit1.Text+#13#10);
//Memo1.Lines.Add();
fs := TFileStream.Create(Label1.Caption ,fmOpenRead or fmShareDenyNone);

GetMem(Buf,fs.Size);
fs.Seek(0,soFromBeginning);

fs.ReadBuffer(Buf^,fs.Size);

memo1.Lines.Add('has send : '+inttostr(Cs.Socket.SendBuf(Buf^,fs.Size)));

end;

procedure TForm2.Button3Click(Sender: TObject);
begin
cs.Close;
end;

procedure TForm2.CSRead(Sender: TObject; Socket: TCustomWinSocket);
begin

Memo1.Lines.add(socket.receiveText);

end;

end.
 
你能将你的源代码给我吗?定重谢!!!
 
谢谢你的帮助!!!虽然我还没试,但我很感谢你的热心!!!
 
就是看不太懂
 
后退
顶部