栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Java将FTP服务器上的文件复制到同一服务器上的目录中?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Java将FTP服务器上的文件复制到同一服务器上的目录中?

如果您使用的是apache commons net

FTPClient
,则有一种直接方法将文件从一个位置移动到另一位置(如果
user
具有适当的权限)。

ftpClient.rename(from, to);

或者,如果您熟悉

ftp commands
,可以使用类似

ftpClient.sendCommand(FTPCommand.yourCommand, args);if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {     //command successful;} else {     //check for reply pre, and take appropriate action.}

如果您使用任何其他客户端,请阅读文档,客户端实现之间不会有太大变化。

更新:

上面的方法将文件移动到

to
目录,即文件
from
不再在目录中。基本上ftp协议意味着要从服务器中传输文件
local <->remote
或从
remote <-> other remote
服务器中不传输文件。

解决此问题的方法会更简单,将完整文件获取到本地,

InputStream
然后将其作为备份目录中的新文件写回到服务器。

获取完整的文件,

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();ftpClient.retrieveFile(fileName, outputStream);InputStream is = new ByteArrayInputStream(outputStream.toByteArray());

现在,将此流存储到备份目录。首先,我们需要将工作目录更改为备份目录。

// assuming backup directory is with in current working directoryftpClient.setFileType(FTP.BINARY_FILE_TYPE);//binary filesftpClient.changeWorkingDirectory("backup");//this overwrites the existing fileftpClient.storeFile(fileName, is);//if you don't want to overwrite it use storeUniqueFile

希望这对您有帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507357.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号