日期:2014-05-17 浏览次数:20787 次
#获取文件列表 $listFTP = [system.net.ftpwebrequest] [system.net.webrequest]::create("ftpurl") $listFTP.UseBinary = $true; $listFTP.Credentials = New-Object System.Net.NetworkCredential("username","password") $listFTP.Method=[system.net.WebRequestMethods+ftp]::listdirectorydetails $listFTP.Timeout=2000 $listFTP.ReadWriteTimeout=2000 $response = $listFTP.getresponse() $stream = New-Object System.Io.StreamReader($response.getresponsestream(),[System.Text.Encoding]::UTF8) while(-not $stream.EndOfStream){ $stream.ReadLine() } $stream.Close() $response.Close() #上传文件 $fileinf=New-Object System.Io.FileInfo("localfilename") $upFTP = [system.net.ftpwebrequest] [system.net.webrequest]::create("ftpurl"+$fileinf.name) $upFTP.UseBinary = $true; $upFTP.Credentials = New-Object System.Net.NetworkCredential("username","password") $upFTP.Method=[system.net.WebRequestMethods+ftp]::UploadFile $upFTP.KeepAlive=$false $sourceStream = New-Object System.Io.StreamReader($fileInf.fullname) $fileContents = [System.Text.Encoding]::UTF8.GetBytes($sourceStream.ReadToEnd()) $sourceStream.Close(); $upFTP.ContentLength = $fileContents.Length; $requestStream = $upFTP.GetRequestStream(); $requestStream.Write($fileContents, 0, $fileContents.Length); $requestStream.Close(); $response =$upFTP.GetResponse(); $response.StatusDescription $response.Close(); #ftpurl:ftp路径 #username:ftp用户名 #password:ftp密码 #localfilename:要上传的文件名 下载和续传因为没有业务需求所以没做出来,日后有心情再做