Wishlist 0 ¥0.00

File Transfer Resume

Basic Usage

 

WinSCP supports resuming file transfers with SFTP and FTP protocols. SCP and WebDAV protocols do not allow it. With S3 protocol, transfer can be resumed only immediately by reconnecting a lost session, not later.

Automatic Resume / Transfer to Temporary Filename

 

WinSCP is able to store file being transferred to temporary filename first, renaming it to the target name only once the transfer successfully finishes.

Reasons to do that:

  • If the transfer is interrupted, on the next attempt (either manual or automatic), WinSCP is able to automatically detect the partial/temporary file and offer you to resume the transfer (or even resume automatically).
  • While updating an existing file, the original version of the file exists during whole transfer. This is particularly useful when updating frequently accessed file on web sites.
  • If the server runs a process that automatically picks new files and processes them, it can be configured to ignore distinct names of temporary files. It prevents the process from picking an incomplete file, while it’s uploaded. See locking files while uploading.

Reasons not to do that:

  • As the file is uploaded to a temporary name, it is technically a new file, even though the file with target name already exists. As such:
    • after the original file is deleted and temporary file is renamed to the original file name, you lose all attributes of the original file (such as owner and group) except for basic permissions;1
    • while you may have permissions to overwrite the existing file, you might not have permissions to create new (temporary) file.

For performance reasons transfer to temporary file name is enabled by default for files larger than a given threshold. The threshold is initially 100 KB. You can change the threshold. You can also enable the feature for all files, particularly when looking to achieve the second impact. You may also need to disable the feature altogether, particularly if you do not have permissions required (e.g. permissions to create a new file in target directory). This can be configured on Endurance page of Preferences dialog.

If your transfer is interrupted, a partial file (with .filepart extension) remains in the target folder. After you try to transfer the file a second time, WinSCP looks for the partial file. If it finds a partial file, it offers you chance to resume the transfer (the confirmation can be disabled).

Note that partial files can be distinguished in file panel by special “torn sheet” overlay icon.

To configure transfer to temporary file name in scripting, use -resumesupport switch of file transfer commands. In .NET assembly use TransferOptions.ResumeSupport.

Transfer to temporary filename is supported with SFTP protocol only and only for binary transfers.

Manual Resume

 

There’s an alternative manual method to resume transfer, which can be used with FTP protocol.

If you attempt to transfer a file already in the target directory and the target file is smaller then the source file, the overwrite confirmation dialog displays a Resume command or an Append command (when appending is supported), in the drop down menu of the No button. After you click Append, WinSCP opens another prompt that lets you append source file or resume your transfer.

If you resume the transfer, WinSCP will skip a part of the source file equal to the target file’s size. The program will only transfer the remainder.

Note that you must have overwrite confirmations enabled to use the method.

The method is also useful for resuming transfers initiated by other clients and also to update log files. Manual resuming of text mode transfers with SFTP-4 and newer is not supported.

Common Problems

 

If you want to resume your transfer, you must use the same source file version as the interrupted transfer. If you do not, your target file will include the source file’s previous version and its current version. For automatic resume, if the partial file is bigger than the source file, WinSCP will display error and disallow resume.

If the target file exists and it is not write-able, but the target directory is, the file will may be transferred anyway with automatic resume. The error overwriting the file will occur only after the transfer finishes. It is because the file is first transferred into temporary file.

Resuming from Broken Connections

 

With SFTP and FTP protocols, when connection is broken, WinSCP can automatically reconnect and resume the transfer.2 This can be configured on Endurance page of Preferences dialog.

  1. For this reason WinSCP never transfers to temporary file name first, if the target file is know to be owner by another user. Note that the owner name can be detected with SFTP-4 and newer only.Back
  2. If transfer resuming/transfer to temporary files name is disabled, file transfer will start from the beginning, with SFTP protocol.Back

断点续传,续传文件是否是同文件判断

想做一个断点续传的程序,基本的已经实现,但我不知道怎么判断前后两次传,传输的两个文件是不是同一个文件,不是简单的判断文件名相同就可以了

假如,开始我向对方传c:a.rm文件,中途断掉,下次我传c:directorya.rm,虽然这两个文件名都是a.rm,但内容是完全不同的,那对方怎么知道是新的文件还是上次未传完的文件,或者说,是续传,还是重新传?

 

行走在赤道

  • 4.8k

 

 

上传:如果是上传的话这种需要客户端的支持,单纯凭服务器是没法做的,就像楼主说的一样没办法确定用户的行为。如果有客户端的话,可以利用MD5校验确定文件是否相同。

下载:服务器具体下载的文件是由URL决定,所以只要参数能对并且客户端也支持断点继续就没啥问题。

 
评论 赞赏 2016-09-21
  • luxianming

    md5校验,本地已经下了一半的和服务器需要下载的完整的文件的md5两者不一样的吧。为什么说下载url一样参数能对就是同一个文件,万一下载的软件有更新呢?url却是一样的,能详细说明一下吗?

     

 

  • 回复 2016-09-22
  • 行走在赤道

    url当然具有唯一性了。如果有2个不同的文件是使用同一个url下载,那就是程序的问题。

     
  • 回复 2016-09-22
  • luxianming

    谢谢,已经解决了,我的情况是服务端已经有文件的md5了,所以现在是更新的时候把服务端的md5也保存了一份在本地,发现是同名文件就进行比较~

 

解读断点续传的基本原理

       断点续传的理解可以分为两部分:一部分是断点,一部分是续传。断点的由来是在下载过程中,将一个下载文件分成了多个部分,同时进行多个部分一起的下载,当某个时间点,任务被暂停了,此时下载暂停的位置就是断点了。续传就是当一个未完成的下载任务再次开始时,会从上次的断点继续传送。
  
  使用多线程断点续传下载的时候,将下载或上传任务(一个文件或一个压缩包)人为的划分为几个部分,每一个部分采用一个线程进行上传或下载,多个线程并发可以占用服务器端更多资源,从而加快下载速度。
  在下载(或上传)过程中,如果网络故障、电量不足等原因导致下载中断,这就需要使用到断点续传功能。下次启动时,可以从记录位置(已经下载的部分)开始,继续下载以后未下载的部分,避免重复部分的下载。断点续传实质就是能记录上一次已下载完成的位置。
 
  断点续传的过程
  ①断点续传需要在下载过程中记录每条线程的下载进度;
  ②每次下载开始之前先读取数据库,查询是否有未完成的记录,有就继续下载,没有则创建新记录插入数据库;
  ③在每次向文件中写入数据之后,在数据库中更新下载进度;
  ④下载完成之后删除数据库中下载记录。
  
  断点续传在HTTP请求上和一般的下载有所不同,客户端浏览器传给Web服务器的时候要多加一条信息——从哪里开始(HTTP请求变量)。要实现HTTP断点续传,Web服务器必须支持HTTP/1.1(相对于HTTP/1.0老版本)。
  HTTP请求是有一个Header的,里面有个Range属性是定义下载区域的,它接收的值是一个区间范围,比如:Range:bytes=0-10000。这样我们就可以按照一定的规则,将一个大文件拆分为若干很小的部分,然后分批次的下载,每个小块下载完成之后,再合并到文件中;这样即使下载中断了,重新下载时,也可以通过文件的字节长度来判断下载的起始点,然后重启断点续传的过程,直到最后完成下载过程。
 
  FTP实现断点续传
  FTP协议也可以支持断点续传下载数据,基本原理是用get命令拿数据的时候在文件名后面加上要获取的起始位置。FTP实现断点续传有三个条件:
  ①断点续传需要服务器的支持,FTP服务器必须能提供断点续传的功能。
  传统的FTP Server是不支持断点续传的,因为它不支持REST指令;目前包括IIS和大部分的FTP架设软件都有了这个功能。用Serv-U架设FTP服务器就能支持断点续传。
  ②支持断点续传的下载工具软件
  QQ旋风、迅雷、影音传送带等大多下载软件都支持断点续传;IE浏览器5.0以前的版本默认的自带下载方式不支持断点续传。在手机上,UC浏览器支持断点续传,能够自动存储已下载的部分,重新打开之后可以继续在已下载部分的基础上继续下载。
  ③FTP服务器上的文件要与下载到硬盘中的文件名相同。
  在使用IE下载文件时,遇到网络中断,不需要重新启动机器,也可实现断点续传。前提是,在恢复下载、开始断点续传并提示再次保存文件时,要使用和第一次下载时相同的路径和文件名。

FTP断点续传

一,最重要的一点,断点续传需要服务器的支持,这个是必要条件。       
  传统的FTP   SERVER是不支持断点续传的,因为它不支持REST指令,传统的FTP指令(我是指服务器端指令)并不包括REST指令。       
    
二,客户端要知道使用REST等一系列指令来作断点续传。       
    
  看看断点续传的详细过程(FTP   SERVER):       
  首先客户端使用REST指令来告诉FTP   SERVER它需要从文件的某个点开始传,接着用STOR或者RETR命令开始传文件,大概的命令的流程如下:       
    
  TYPE   I       
  200   Type   set   to   I.       
  PASV       
  227   Entering   Passive   Mode   (204,48,18,69,98,250)       
  REST   187392       
  350   Restarting   at   187392.   Send   STORE   or   RETRIEVE   to   initiate   transfer.       
  RETR   /pub/audio/pci/maestro-3/win2k/1056.zip       
  150   Opening   BINARY   mode   data   connection   for   /pub/audio/pci/maestro-3/win2k/1056.zip   (936098   bytes).       
    
  首先使用TYPE命令告诉FTP   SERVER使用BINARY模式传送文件;       
  然后使用PASV命令告诉FTP   SERVER使用被动打开模式来传送文件;       
  接着使用REST   187392指令告诉FTP   SERVER要从文件的187392字节开始传送;       
  最后使用RETR指令来传送文件。       
    
  从上面可以看出,这个FTP   SERVER支持REST指令,有的FTP   SERVER(特别的老的)是不支持这个指令的,这时即使FTP   CLIENT支持断点续传也一点用都没有!       
    
  支持断点的FTP   SERVER:Serv-U   FTP,还有一系列的新出现的FTP   SERVER;       
  不支持断点的:IIS4以前版本所带的都不行,IIS5   有,不家可以测试一下,登录进FTP   SERVER,然后输入REST   1000命令,看服务器是否认识,认识就是支持断点。

FTP实现断点续传

 

FTP客户端实现要建立两个通道,一个控制命令通道,让FTP服务器知道客户端要干什么,一个数据传输通道。所谓的两个通道只不过是两个调用了connect函数的连接,只是控制命令通道专门用来传输一些字符串命令信息,而数据通道则是用来传输文件。控制命令通道一定是由客户端向服务器的连接(默认的端口是21,也可以指定端口,这要看服务器的设置)。连接的过程完成了FTP的登录。数据通道则不一定啦,具体哪个连哪个,请看下面对PASV命令的解释。 

 

  其实FTP断点续传的原理很简单,可分为断点下载和断点上传。 

  客户端的实现步骤如下: 

一、下载:  

1、向服务器发送“REST + 本地文件长度”命令,告诉服务器,客户端要断点下载了。这时服务器还不知道客户端要下载哪个文件; 

   要实现FTP的断点续传,FTP服务器必须支持REST指令,这条指令在FTP协议文本RFC959中就已经定义了,不过它不是FTP服务器必须支持的指令。一般,你可以在下载前使用REST 100命令进行实验,如果服务器正常执行了这条命令,说明该服务器支持FTP断点续传。REST后面跟的数表示下载文件的起始位置,而REST 0表示从文件最开始处下载。REST命令本身并不执行下载功能,你仍需要使用RETR命令执行下载工作。 

2、向服务器发送“RETR + 文件名”命令,通知服务器要下载的文件名,这时服务器开始定位文件指针读文件并发送数据。 

3、客户端定位本地文件指针(文件末尾); 

4、两端的准备工作都做完了以后,客户端创建socket,以被动或非被动方式建立数据通道,循环调用recv接收数据并追加入本地文件; 

 

二、上传: 

1、获取服务器上和本地要上传文件的同名文件大小; 

2、向服务器发送“APPE + 文件名”,通知服务器,接下来从数据通道发送给你的数据要附加到这个文件末尾。 

3、定位本地文件指针(和FTP上文件大小相同的位置) 

4、从文件指针处读数据并发送。 

 

  好了,FTP断点续传的原理就这么简单。代码里将断点上传和断点下载放到同一个函数(MoveFile)里,通过get参数说明是上传还是下载。当然,整个FTP类的实现有800多行,包括登录、退出、获取FTP文件大小、删除FTP服务器上文件、响应服务器,解析响应信息等函数。相应的注释代码里都有,这里就不一一熬述了。 

 

  这里重点说说PASV模式,即被动模式,这是FTP命令里比较不容易理解的一个,这条命令请求服务器在某个端口(非FTP默认端口或控制命令端口)创建一个监听socket,服务器创建的端口号会在客户端的控制命令通道上得到响应。得到这个端口号后,客户端就可以创建新的socket(数据通道)connect过去,并进行文件传输等工作。否则,如果为非被动模式,那么监听的socket由客户端创建,服务器connect过来。 

 

  对于这条命令的存在我是这么理解的,存在这么一种情况:客户端的IP是个内网的IP,服务器的IP是个外网的,当进行数据传输时内网的IP对于服务器是不可见的,只有由服务器启动监听socket才能建立数据通道,所以必须以被动模式进行。:)

About Us

Since 1996, our company has been focusing on domain name registration, web hosting, server hosting, website construction, e-commerce and other Internet services, and constantly practicing the concept of "providing enterprise-level solutions and providing personalized service support". As a Dell Authorized Solution Provider, we also provide hardware product solutions associated with the company's services.
 

Contact Us

Address: No. 2, Jingwu Road, Zhengzhou City, Henan Province

Phone: 0086-371-63520088 

QQ:76257322

Website: 800188.com

E-mail: This email address is being protected from spambots. You need JavaScript enabled to view it.