. Net class library has some FTP operations, but it is not very intuitive to use, it needs some encapsulation for better use, on SourceForge find a person to write ftpclient, this class pair. net class library system. net. sockets is encapsulated. It uses the tcpclient class to send FTP-defined commands, such as list and retr, obtain the relevant information from the FTP server.CodeYou can achieve resumable upload.
Resumable upload is divided into two parts: resumable download and upload. In the FTP protocol, the location of the file to be downloaded or uploaded is specified by using rest.
Download the resumable upload part first:
In this solution, the main code is in the ftpconnection class. Download is implemented through the GetFile class. In the original implementation, it uses create when opening the file, in this way, a new file will be created each time. We need to change the file to append for resumable data transfer, so that the file can be downloaded as follows:
Code
Public Virtual Void GetFile ( String Remotefilename, String Localfilename, ftpfiletransfertype)
{
Filestream FS = New Filestream (localfilename, filemode. append );
Getstream (remotefilename, FS, type );
FS. Close ();
}
Then you need to modify its getstream method. In the original implementation, the RETR command is directly sent to the FTP server, so that it downloads the file from the beginning. If you want to download the file from the breakpoint, before sending the RETR name, use the REST command to tell the FTP server where you want to start downloading and change it to the following code:
Code
// Begin adds the resumable upload function to send the REST command to the FTP server.
Tempmessagelist = New Arraylist ();
Tempmessagelist = Sendcommand ( " Rest " + Stream. Length );
Returnvalue = Getmessagereturnvalue (( String ) Tempmessagelist [ 0 ]);
If (Returnvalue ! = 350 )
{
Throw New Exception (( String ) Tempmessagelist [ 0 ]);
}
// End adds the resumable upload function to send REST commands to the FTP server.
Tempmessagelist = New Arraylist ();
Tempmessagelist = Sendcommand ( " RETR " + Remotefilename );
Returnvalue = Getmessagereturnvalue (( String ) Tempmessagelist [ 0 ]);
If ( ! (Returnvalue = 150 | Returnvalue = 125 ))
{
Throw New Exception (( String ) Tempmessagelist [ 0 ]);
}
The parameter stream. length after rest is the length of the file downloaded to the local device. After this modification, you can continue to download the downloaded content.
Upload the resumable upload part
According to the definition of the FTP protocol, before resumable upload, you also need to send the REST command to tell the FTP server where you want to continue sending data. What is different from the normal upload, generally, you can use the STOR command to upload files. If you want to support resumable data transfer, you must run the APPE Command. In addition, you must first obtain the extent to which files are uploaded on the FTP server, this can be used to obtain the file list under a directory on the FTP server through xdir of this class, and obtain the file size. Compare it with the file to be uploaded. If the file name is the same, the previous upload will continue, you can add a sendstream method in the ftpconnection class. The main modification is to send the rest and APPE commands. You do not need to modify other methods.
Code
// Send REST command
Tempmessagelist = Sendcommand ( " Rest " + Startposition );
Returnvalue = Getmessagereturnvalue (( String ) Tempmessagelist [ 0 ]);
If (Returnvalue ! = 350 )
{
Throw New Exception (( String ) Tempmessagelist [ 0 ]);
}
Tempmessagelist = Sendcommand ( " Appe " + Remotefilename );
Returnvalue = Getmessagereturnvalue (( String ) Tempmessagelist [ 0 ]);
If ( ! (Returnvalue = 150 | Returnvalue = 125 ))
{
Throw New Exception (( String ) Tempmessagelist [ 0 ]);
}
The startposition parameter of the rest command is the size of the previously uploaded file obtained from the FTP server.
In addition, this class does not support Chinese characters. You can modify the Readline method of this class and change the encoding from ASCII to gb2312.
Tmpmes + = Encoding. getencoding ( " Gb2312 " ). Getstring (buffer, 0 , Bytes );
That's all.