为什么FtpWebRequest从根目录下载文件?这会导致553错误吗? [英] Why does FtpWebRequest download files from the root directory? Can this cause a 553 error?

查看:80
本文介绍了为什么FtpWebRequest从根目录下载文件?这会导致553错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个PowerShell脚本,用于从FTP服务器下载文件.该脚本适用于我测试过的所有服务器.对于该服务器,脚本能够连接并获得目录列表,但是每次尝试下载文件时,都会返回"553 File Unavailable"错误.下面是我用来下载文件的代码.

I have built a PowerShell script for downloading files from a FTP server. This script is working with all the servers I have tested with expect one. For this one server the script is able to connect, get the directory listing, but each time it attempts to download a file a "553 File Unavailable" error is returned. Below is the code I am using to download files.

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)"
    }
}

我还实现了.NET跟踪以调试此问题,以下是尝试下载文件时收到的消息块之一.

I have also implemented .NET tracing to debug this issue, below is one of the message blocks received when attempting to download a file.

System.Net Information: 0 : [7712] FtpWebRequest#33194379::.ctor(ftp://**.**.**.**//record/5DE7D3810004)
System.Net Information: 0 : [7712] FtpWebRequest#33194379::GetResponse(Method=RETR.)
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Created connection from 666.66.66.66:61255 to **.**.**.**:21.
System.Net Information: 0 : [7712] Associating FtpWebRequest#33194379 with FtpControlStream#18792280
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Received response [220 FTP server ready. Username and password required]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Sending command [USER change]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Received response [331 Password required.]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Sending command [PASS ********]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Received response [230 Login successful. .]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Sending command [OPTS utf8 on]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Received response [502 Command not implemented, superfluous at this site.]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Sending command [PWD]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Received response [257 "/usr/apt/tesla/config" is the current directory.]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Sending command [TYPE I]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Received response [200 Switching to Binary mode.]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Sending command [PASV]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Received response [227 Entering Passive Mode (10,40,0,92,254,143)]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Sending command [RETR /record/5DE7D3810004]
System.Net Information: 0 : [7712] FtpControlStream#18792280 - Received response [553 File unavailable.]
System.Net Information: 0 : [7712] FtpWebRequest#33194379::(Releasing FTP connection#18792280.)

我注意到, FtpWebRequest 不会CD到从其下载文件的目录,而是从服务器的根目录(或设置为根目录的任何路径)下载该文件.这可能就是为什么我遇到553错误吗?如果是这样,反正还有CD到您要从中下载文件的目录吗?

I have noticed that FtpWebRequest does not CD to the directory it is downloading a file from, and instead downloads that file from the servers root directory (or any path set as the root directory). Could this be why I am encountering 553 errors? If so, is there anyway to CD to the directory you want to download files from?

推荐答案

确实很少见,某些FTP服务器无法使用 RETR 命令处理绝对路径.必须先 CWD 到目录,然后仅将 RETR 与文件名一起使用. FtpWebRequest 不支持.您将不得不使用另一个FTP库.

While rare, indeed, some FTP servers cannot handle absolute paths with the RETR command. One has to first CWD to the directory and then use RETR with file name only. FtpWebRequest does not support that. You will have to use another FTP library.

您已询问有关 my WinSCP .其 .NET库没有明确的更改目录" 要求.但是在某些情况下,它会执行 CWD ,因此您可以根据需要进行操作.在目录列表之前,它特别执行 CWD .因此,您可以自己对源目录进行显式列出:

You have asked about my WinSCP. Its .NET library does not have an explicit "change directory" request either. But it does CWD in certain situations, so you can make it do, what you need. It particularly does CWD before directory listing. So either you can do an explicit listing of the source directory yourself:

$session.ListDirectory("/record")
$session.GetFileToDirectory("/record/5DE7D3810004", "C:\local\path")

或者如果您仍要下载所有文件,则可以提出要求,WinSCP会在内部进行列表(以及 CWD ).

Or if you want to download all files anyway, you can ask for that and WinSCP will do the listing (and CWD along) internally.

$session.GetFilesToDirectory("/record/*", "C:\local\path")


顺便说一句, WinSCP脚本具有明确的


Btw, WinSCP scripting has an explicit cd command, so you can use the scripting instead, while it is not so convenient to use from PowerShell as the assembly.

或使用另一个库.例如, FluentFTP 具有

Or use yet another library. For example FluentFTP has FtpClient.SetWorkingDirectory.

这篇关于为什么FtpWebRequest从根目录下载文件?这会导致553错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆