PowerShell FTP 下载文件 Returns 421 服务不可用错误
PowerShell FTP Downloading Files Returns 421 Service Not Avaliable Error
我已经构建了一个用于从 FTP 服务器下载文件的脚本。该脚本在我测试过的所有服务器上都运行良好。对于此服务器,我能够获取目录列表,但是当我尝试下载文件时它失败并且 returns 出现“421 服务不可用,正在关闭控制连接”错误。下面是我用来下载文件的代码。
function Get-FtpFile
{
Param ([string]$fileUrl, $credentials, [string]$destination)
try
{
$FTPRequest = [System.Net.FtpWebRequest]::Create($fileUrl)
if ($credentials)
{
$FTPRequest.Credentials = $credentials
}
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UseBinary = $true
$FTPRequest.UsePassive = $true
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse()
# Get a download stream from the server response
$ResponseStream = $FTPResponse.GetResponseStream()
# Create the target file on the local system and the download buffer
$LocalFile = New-Object IO.FileStream ($destination,[IO.FileMode]::Create)
[byte[]]$ReadBuffer = New-Object byte[] 1024
# Loop through the download
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$LocalFile.Write($ReadBuffer,0,$ReadLength)
}
while ($ReadLength -ne 0)
$ResponseStream.Close()
$ReadBuffer.clear()
$LocalFile.Close()
$FTPResponse.Close()
}
catch [Net.WebException]
{
return "Unable to download because: $($_.exception)"
}
}
我可以使用 Windows 文件资源管理器 FTP 从该服务器下载文件,所以我认为这不是服务器本身的问题。
我通过测试注意到的一件有趣的事情是,当服务器 returns 列出每个文件名的目录时,每个文件名都包含一个尾随 NULL。我试过下载包括和不包括这个尾随 NULL 的文件,两次尝试都产生相同的错误代码。
有没有人见过类似的错误?或者有人知道 Windows 文件资源管理器使用 FTP 的方式与我上面列出的 PowerShell 脚本相比有何不同?
我遇到的这个问题是我没有正确删除每个文件名中的尾随 NULL。一旦我能够正确删除 NULL,我就不再有任何问题。
如果以后有人遇到这个问题,在尝试下载文件时遇到 421 错误,请检查目录列表提供的文件名是否有尾随 NULL 字符。如果是这样,请在尝试下载每个文件之前从文件名字符串中删除这些字符。
我已经构建了一个用于从 FTP 服务器下载文件的脚本。该脚本在我测试过的所有服务器上都运行良好。对于此服务器,我能够获取目录列表,但是当我尝试下载文件时它失败并且 returns 出现“421 服务不可用,正在关闭控制连接”错误。下面是我用来下载文件的代码。
function Get-FtpFile
{
Param ([string]$fileUrl, $credentials, [string]$destination)
try
{
$FTPRequest = [System.Net.FtpWebRequest]::Create($fileUrl)
if ($credentials)
{
$FTPRequest.Credentials = $credentials
}
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UseBinary = $true
$FTPRequest.UsePassive = $true
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse()
# Get a download stream from the server response
$ResponseStream = $FTPResponse.GetResponseStream()
# Create the target file on the local system and the download buffer
$LocalFile = New-Object IO.FileStream ($destination,[IO.FileMode]::Create)
[byte[]]$ReadBuffer = New-Object byte[] 1024
# Loop through the download
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$LocalFile.Write($ReadBuffer,0,$ReadLength)
}
while ($ReadLength -ne 0)
$ResponseStream.Close()
$ReadBuffer.clear()
$LocalFile.Close()
$FTPResponse.Close()
}
catch [Net.WebException]
{
return "Unable to download because: $($_.exception)"
}
}
我可以使用 Windows 文件资源管理器 FTP 从该服务器下载文件,所以我认为这不是服务器本身的问题。
我通过测试注意到的一件有趣的事情是,当服务器 returns 列出每个文件名的目录时,每个文件名都包含一个尾随 NULL。我试过下载包括和不包括这个尾随 NULL 的文件,两次尝试都产生相同的错误代码。
有没有人见过类似的错误?或者有人知道 Windows 文件资源管理器使用 FTP 的方式与我上面列出的 PowerShell 脚本相比有何不同?
我遇到的这个问题是我没有正确删除每个文件名中的尾随 NULL。一旦我能够正确删除 NULL,我就不再有任何问题。
如果以后有人遇到这个问题,在尝试下载文件时遇到 421 错误,请检查目录列表提供的文件名是否有尾随 NULL 字符。如果是这样,请在尝试下载每个文件之前从文件名字符串中删除这些字符。