Upload
FTPMG. Upload ("", DateTime.Now.ToString ("Yyyymmddhhmmss"));
Download
FTPMG. Download (@ "E:\", "Tt.txt", out errorinfo);
1 stringFtpserverip;2 stringFtpuserid;3 stringFTPPassword;4 ftpwebrequest reqftp;5 Private voidConnect (String Path)//Connect to FTP6 {7 //to create a FtpWebRequest object from a URI8Reqftp = (ftpwebrequest) ftpwebrequest.create (NewUri (path));9 //specifying data Transfer TypesTenReqftp.usebinary =true; One //FTP user name and password AReqftp.credentials =NewNetworkCredential (Ftpuserid, FTPPassword); - } - PublicFtpmanager (stringFtpserverip,stringFtpuserid,stringFTPPassword) the { - This. Ftpserverip =Ftpserverip; - This. Ftpuserid =Ftpuserid; - This. FTPPassword =FTPPassword; + - } + A at - - Public voidUpload (stringPathstringFileName//The above code implements the ability to upload files to an FTP server - { -FileInfo Fileinf =NewFileInfo (filename); - stringURI =""; in if(Path! ="") -URI ="ftp://"+ Ftpserverip +"/"+ Path +"/"+Fileinf.name; to Else +URI ="ftp://"+ Ftpserverip +"/"+Fileinf.name; - the *Connect (URI);//Connection $ Panax Notoginseng //The default is true and the connection is not closed - //is executed after a command theReqftp.keepalive =false; + A //specify what commands to execute theReqftp.method =WebRequestMethods.Ftp.UploadFile; + - //notifies server file size when uploading a file $Reqftp.contentlength =fileinf.length; $ - //buffer size set to KB - intBuffLength =2048; the - byte[] Buff =New byte[bufflength];Wuyi intContentlen; the - //open a file stream (System.IO.FileStream) to read the uploaded file WuFileStream fs =Fileinf.openread (); - AboutStream STRM =NULL; $ Try - { - //write the uploaded file to the stream -STRM =Reqftp.getrequeststream (); A //KB per read file stream +Contentlen = fs. Read (Buff,0, bufflength); the - //stream content does not end $ while(Contentlen! =0) the { the //write content from file stream to upload stream theStrm. Write (Buff,0, Contentlen); the -Contentlen = fs. Read (Buff,0, bufflength); in } the the //Close two streams About STRM. Close (); the FS. Close (); the } the Catch(Exception ex) + { -LOG.WRITELOGD ("Upload error:"+Ex. Message); the Console.WriteLine (ex. Message);Bayi Throwex; the } the finally - { - the FS. Close (); the } the } the - Public BOOLDownload (stringFilePath,stringFileName, out stringErrorInfo)/**/////The code above implements the ability to download files from an FTP server the { the the 94 Try the { theString Onlyfilename =Path.getfilename (fileName); the stringNewFileName = FilePath +"\\"+Onlyfilename;98 if(File.exists (newfilename)) About { -ErrorInfo =string. Format ("The local file {0} already exists and cannot be downloaded", newfilename);101 return false;102 103 }104 stringURL ="ftp://"+ Ftpserverip +"/"+FileName; theConnect (URL);//Connection106 107Reqftp.credentials =NewNetworkCredential (Ftpuserid, FTPPassword);108 109FtpWebResponse response =(FtpWebResponse) reqftp.getresponse (); the 111Stream FtpStream =Response. GetResponseStream (); the 113 LongCL =Response. ContentLength; the the intBufferSize =2048; the 117 intReadcount;118 119 byte[] buffer =New byte[buffersize]; - 121Readcount = ftpstream.read (buffer,0, buffersize);122FileStream OutputStream =NewFileStream (NewFileName, filemode.create);123 124 while(Readcount >0) the {126Outputstream.write (Buffer,0, readcount);127 -Readcount = ftpstream.read (buffer,0, buffersize);129 } the 131 ftpstream.close (); the 133 outputstream.close ();134 135 Response. Close ();136ErrorInfo ="";137 return true;138 }139 Catch(Exception ex) $ {141LOG.WRITELOGD ("Download Error:"+Ex. Message);142ErrorInfo =string. Format ("Unable to download because {0}", ex. Message);143 return false;144 }145 }146
View Code
FTP Upload and download