导语: 如果ftp server支持REST指令(可以通过ftp client登入,然后输入REST 100,然后检查服务端响应),则上传或下载失败后,下次连接成功后可以从断点开始继续传输数据;
下面是基于Delphi7的组件IdFTP的示例代码,比自己用指令实现方便。
断点上传:
procedure TFtpThread.Execute; var AFileStream: TFileStream; sFileName: string; begin sFileName := ExtractFileName(AFileName); AFileStream := TFileStream.Create(AFileName, fmOpenRead); try with frmMain.IdFTP2 do try if Connected then Disconnect; //重新连接 Username := 'zzzz'; Password := 'yyyyyy'; Host := 'xx.xx.xx.xx'; Port := 21; Passive := mypassive; Connect; except on e: Exception do begin frmMain.StatusBar1.Panels[0].Text := '出错,异常:' + E.Message; frmMain.WriteLog(E.Message); AFileStream.Free; exit; end; end; // 获取断点 finishedSize := frmMain.IdFTP2.Size(sFileName); AFileStream.Seek(finishedSize, soFromBeginning); // 开始断点续传 BytesToTransfer := AFileStream.Size; frmMain.ProgressBar1.Max := BytesToTransfer div 1024; AFileStream.Position := finishedSize; try {while (not Terminated) and (AFileStream.Position < AFileStream.Size) do begin frmMain.IdFTP2.Put(AFileStream, sFileName, true); end; AMemStream.Free; } frmMain.IdFTP2.Put(AFileStream, sFileName, true); except on e: Exception do begin frmMain.StatusBar1.Panels[0].Text := '出错,异常:' + E.Message; frmMain.WriteLog(E.Message); AFileStream.Free; exit; end; end; // 判断是否上传完毕 if AFileStream.Position = AFileStream.Size then frmMain.StatusBar1.Panels[0].Text := '上传完毕!' else frmMain.StatusBar1.Panels[0].Text := '未完成上传,点击上传继续传输!'; AFileStream.Free; except on e: Exception do begin frmMain.StatusBar1.Panels[0].Text := '出错,异常:' + E.Message; frmMain.WriteLog(E.Message); AFileStream.Free; exit; end; end; end;
断点下载:
procedure TfrmMain.FtpDownLoad(aURL, aFile: string); var tStream: TFileStream; sName, sPass, sHost, sPort, sDir,sFile: string; bResume: Boolean; begin //ftp方式下载 if FileExists(aFile) then //建立文件流 tStream := TFileStream.Create(aFile, fmOpenWrite) else tStream := TFileStream.Create(aFile, fmCreate); GetFTPParams(aURL, sName, sPass, sHost, sPort, sDir,sFile); with IdFTP1 do try if Connected then Disconnect; //重新连接 Username := sName; Password := sPass; Host := sHost; Port := StrToInt(sPort); Passive := mypassive; Connect; except on e: Exception do begin Application.MessageBox(PChar(E.Message), 'error', 16); WriteLog(E.Message); exit; end; end; IdFTP1.ChangeDir(sDir); //改变目录 BytesToTransfer := IdFTP1.Size(sFile); ProgressBar1.Max := BytesToTransfer div 1024; if FileExists(aFile) then bResume := true else bResume := false; try if bResume then //续传 begin //ProgressBar1.Position := tStream.Size div 1024; finishedSize := tStream.Size; tStream.Position := tStream.Size; IdFTP1.Get(sFile, tStream, True); end else begin IdFTP1.Get(sFile, tStream, False); end; if tStream.Position = BytesToTransfer then begin StatusBar1.Panels[0].Text := '下载完成'; end; finally tStream.Free; end; end; //ftp://user:pwd@foolish.6600.org:2003/soft/list.mp4 procedure TfrmMain.GetFTPParams(var aURL, sName, sPass, sHost, sPort, sDir,sFile: String); var URI: TIdURI; begin URI := TIdURI.Create(aURL); //建立 try sHost := URI.Host; //主机 sPort := URI.Port; sName := URI.Username; sPass := URI.Password; sDir := URI.Path; sFile := URI.Document; finally URI.Free; end; end;
相关推荐
利用libcurl实现ftp断点续传,文件压缩包里面只有dll,lib,.h和cpp文件,不是工程。
ftp实现断点续传,ftp支持上传续传 ftp断点续传基于c语言的实现。
精炼的内容,看过基本可以完成FTP断点续传的编程。
用apache的FTP实现断点续传 - janestone的专栏 - 博客频道 - CSDN.NET (2012年5月21日) java实现FTP多线程断点续传,上传下载! - java学习与交流 - j2ee学习网 - j2ee学习网 (2012年5月21日) 用 Java 实现断点续传 ...
Java上传文件到FTP服务器,支持断点续传
java实现FTP多线程断点续传
java实现ftp断点续传
FTP断点续传的一些命令
delphi Ftp断点续传源码.rar(源码)
如何设计FTP断点续传 思路
java实现ftp断点续传Java实用源码整理learns
FTP上传(断点续传)更新进度条 公司项目第一个版本
c#FTP断点续传客户端 对文件进行删除 上传下载 重命名
C语言实现ftp断点续传.rar.rar
android实现ftp断点续传下载类,完成后自动通知调用程序
基于MFC实现的FTP可断点续传的客户端,实现了断点上传和断点下载的功能,可以运行,无论是学习还是拿到项目中使用都很有用。
FTP客户端实现要建立两个通道,一个控制命令通道,让FTP服务器知道客户端要干什么,一个数据传输通道。所谓的两个通道只不过是两个调用了connect函数的连接,只是控制命令通道专门用来传输一些字符串命令信息,而...
java FTP多线程 批量 断点续传,使用 1.建一个支持多线程FTP服务端; 如:xxftp有Windows和Linux版本; 2.建一个项目导入文件测试;
c的ftp断点续传的客户端源代码 VC中利用FTP协议实现多线程断点续传(附源码)
c#上传下载ftp(支持断点续传) 代码完全通俗易懂 注释完整