java ftp详解_Java FTP 基本操作

最近工作中用到了 FTP 相关的操作,所以借此机会了解了下具体内容。

FTP基础

关于 FTP 基础推荐阅读《使用 Socket 通信实现 FTP 客户端程序》,其中需要特别注意的是主动模式和被动模式,这一部分在日常使用中经常被忽略,但生产环境中可能会出问题,关键在于防火墙对端口的控制。

主动模式:服务器采用 21 和 20 端口,客户端采用大于 1024 的随机端口,连接指令和文件传输指令由服务端发送。

被动模式:服务端采用 21 和大于 1024 的随机端口,客户端采用大于 1024 的随机端口,连接指令由客户端发送。

程序操作 FTP 过程在上面推荐的文章中有所提及,大家可以看到过程还是比较复杂的,不过好在有 apache 的 commons-net 给我们提供了相关的工具类可以使用,本文使用的是 3.6 版本。以下通过代码进行说明,此代码仅演示功能,很多地方并不完善,如果用作生产请自行修改。

Java FTP 上传

/**

* FTP发送至目标服务器

* @apiNote 依赖apache commons-net 包

* @param server

*/

public static void sendToServerByFTP(String server, int port, String username, String password,

String encoding, String fileLocalPath, String fileRemotePath, String fileRemoteName) throws IOException {

// 获取 FTPClient

FTPClient ftpClient = new FTPClient();

ftpClient.connect(server, port);

ftpClient.login(username, password);

int replyCode = ftpClient.getReplyCode();

if (!FTPReply.isPositiveCompletion(replyCode)) {

System.out.println("connected failed");

}

// 设置编码,当文件中存在中文且上传后文件乱码时可使用此配置项

//ftpClient.setControlEncoding(encoding);

// 切换为本地被动模式,可以解决FTP上传后文件为空的问题,但需要服务器将FTP服务添加至防火墙白名单

//ftpClient.enterLocalPassiveMode();

// 切换到指定目录

ftpClient.changeWorkingDirectory(fileRemotePath);

// 获取文件并上传

File file = new File(fileLocalPath);

InputStream inputStream = new FileInputStream(file);

//文件名为中文名且上传后出现乱码时启用此项

//String fileName = new String(fileRemoteName.getBytes(encoding), "ISO8859-1");

boolean flag = ftpClient.storeFile(fileRemoteName, inputStream);

// 关闭已占用资源

inputStream.close();

ftpClient.logout();

}

FTP 下载

FTP 下载和上传基本步骤类似,依赖的方法由 storeFile 变为 retrieveFile

public void downloadFile(String server, int port, String username, String password,

String serverPath, String localPath, String fileName) throws IOException {

// 登录

FTPClient ftpClient = new FTPClient();

ftpClient.connect(server, port);

ftpClient.login(username, password);

// 验证登录情况

int replyCode = ftpClient.getReplyCode();

if (!FTPReply.isPositiveCompletion(replyCode)) {

throw new RuntimeException("登录FTP服务器失败,错误代码:" + replyCode);

}

// 切换服务器至目标目录

ftpClient.changeWorkingDirectory(serverPath);

// 下载文件

File file = new File(localPath);

FileOutputStream fileOutputStream = new FileOutputStream(file);

ftpClient.retrieveFile(fileName, fileOutputStream);

// 关闭资源占用

fileOutputStream.close();

ftpClient.logout();

}

FTP 删除

public void deleteFile(String server, int port, String username, String password,

String serverPath, String fileName) throws IOException {

// 登录

FTPClient ftpClient = new FTPClient();

ftpClient.connect(server, port);

ftpClient.login(username, password);

// 验证登录情况

int replyCode = ftpClient.getReplyCode();

if (!FTPReply.isPositiveCompletion(replyCode)) {

throw new RuntimeException("登录FTP服务器失败,错误代码:" + replyCode);

}

ftpClient.changeWorkingDirectory(serverPath);

ftpClient.deleteFile(fileName);

}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值