Skip to main content

FtpClient

Struct FtpClient 

Source
pub struct FtpClient { /* private fields */ }
Expand description

FTP 客户端

异步 FTP 协议实现,支持:

  • 被动模式优先,主动模式 fallback
  • 二进制/ASCII 传输模式切换
  • 断点续传(REST 命令)
  • 目录列表解析(Unix/Windows 格式)

§示例

use aria2_core::ftp::connection::{FtpClient, FtpMode};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = FtpClient::connect("ftp.example.com", 21, FtpMode::Passive).await?;
    client.login("anonymous", "user@example.com").await?;
    client.set_binary_mode(true).await?;

    let files = client.list_directory("/").await?;
    for file in &files {
        println!("{} {} {}", if file.is_dir { "D" } else { "F" }, file.size, file.name);
    }

    client.quit().await?;
    Ok(())
}

Implementations§

Source§

impl FtpClient

Source

pub async fn connect(host: &str, port: u16, mode: FtpMode) -> Result<Self>

连接到 FTP 服务器

§参数
  • host: FTP 服务器地址(域名或 IP)
  • port: FTP 服务器端口(通常为 21)
  • mode: 数据连接模式(被动或主动)
§错误
  • 连接超时
  • 网络错误
  • 服务器拒绝连接
Source

pub async fn login(&mut self, username: &str, password: &str) -> Result<()>

登录到 FTP 服务器

§参数
  • username: 用户名(匿名登录使用 “anonymous”)
  • password: 密码(匿名登录使用邮箱)
§错误
  • 530 未登录(认证失败)
Source

pub async fn set_binary_mode(&mut self, enabled: bool) -> Result<()>

设置传输模式(二进制/ASCII)

§参数
  • enabled: true 为二进制模式(TYPE I),false 为 ASCII 模式(TYPE A)
§错误
  • 504 不支持的传输模式
Source

pub async fn passive_mode(&mut self) -> Result<TcpStream>

进入被动模式并建立数据连接

优先尝试 EPSV(扩展被动模式),如果服务器不支持则回退到 PASV。

§返回

返回数据连接的 TcpStream

§错误
  • 425 无法打开数据连接
  • 超时错误
Source

pub async fn active_mode(&mut self) -> Result<TcpStream>

进入主动模式并建立数据连接

发送 PORT 或 EPRT 命令告知服务器客户端的数据端口, 然后在本地监听该端口等待服务器连接。

§返回

返回已接受的数据连接 TcpStream

§错误
  • 425 无法打开数据连接
  • 500/501/502 命令语法错误
Source

pub async fn list_directory(&mut self, path: &str) -> Result<Vec<FtpFileInfo>>

列出目录内容

支持两种格式:

  • MLSD(机器可读列表,如果服务器支持)
  • LIST(传统 Unix/Windows 格式)
§参数
  • path: 要列出的目录路径
§返回

返回文件信息向量

§错误
  • 550 目录不存在或不可访问
  • 425/426 数据连接错误
Source

pub async fn download_file( &mut self, remote_path: &str, offset: Option<u64>, ) -> Result<TcpStream>

下载文件

支持断点续传,通过 REST 命令指定偏移量。

§参数
  • remote_path: 远程文件路径
  • offset: 可选的起始偏移量(用于断点续传)
§返回

返回数据连接的 TcpStream,用于读取文件内容

§错误
  • 550 文件不存在
  • 425/426 数据连接错误
Source

pub async fn cwd(&mut self, path: &str) -> Result<()>

更改工作目录

§参数
  • path: 目标目录路径
§错误
  • 550 目录不存在或无权限
Source

pub async fn pwd(&mut self) -> Result<String>

获取当前工作目录

§返回

返回当前目录路径字符串

§错误
  • 500/501/502 命令执行错误
Source

pub async fn abort(&mut self) -> Result<()>

中止正在进行的传输

发送 ABOR 命令,中断当前的数据传输操作。 注意:ABOR 的行为在不同服务器上可能不同。

§错误
  • 网络错误(如果控制连接已断开)
Source

pub async fn quit(self) -> Result<()>

断开 FTP 连接

发送 QUIT 命令并优雅地关闭控制连接。

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more