您的位置:首页 > 运维架构 > Apache

Apache FTPClient上传下载文件

2017-09-30 11:32 531 查看
使用Java开发FTPClient,完成文件的上传下载

步骤如下

第一步:导入maven依赖jar包

<dependencies>
<!--FtpClient所在的包-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<!--日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>


第二步:代码实现

/**
* 上传文件到FTP服务器
*/
@Test
public void uploadFile() throws IOException {
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("UTF-8");
ftpClient.connect("192.168.93.108",2121);
ftpClient.login("admin", "123456");
int replyCode = ftpClient.getReplyCode();
ftpClient.setDataTimeout(120000);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//设置为二进制文件

if (!FTPReply.isPositiveCompletion(replyCode)) {
ftpClient.disconnect();
System.out.println("FTP连接失败");
}else {
System.out.println("FTP连接成功");
}
//本地文件流
InputStream in = new FileInputStream("F://love.txt");
//指定写入目录,注意:假如指定的目录不存在,会直接上传到根目录,假如存在,就上传到指定路径
//      ftpClient.changeWorkingDirectory("test2");
//远程文件名
String removePath = new String("./test2/love8.txt".getBytes("UTF-8"),"iso-8859-1");
//上传
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
if(ftpClient.storeFile(removePath, in)){
System.out.println("文件上传成功");
}else{
System.out.println("文件上传失败");
}
//关闭文件流
in.close();
//关闭连接
if (ftpClient != null) {
ftpClient.logout();
ftpClient.disconnect();
}
}
/**
* 从FTP服务器下载文件
*/
@Test
public void downLoadFile() throws SocketException, IOException {
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("UTF-8");
ftpClient.connect("192.168.93.108",2121);
ftpClient.login("admin", "123456");
int replyCode = ftpClient.getReplyCode();
ftpClient.setDataTimeout(120000);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//设置为二进制文件

if (!FTPReply.isPositiveCompletion(replyCode)) {
ftpClient.disconnect();
System.out.println("FTP连接失败");
}else {
System.out.println("FTP连接成功");
}
//同理,假如指定不存在的路径,会去根路径下查找
//        ftpClient.changeWorkingDirectory("test2");
File file=new File("F://abc4.txt");
FileOutputStream fos=new FileOutputStream(file);
boolean result = ftpClient.retrieveFile("./test/love6.txt",fos);
if(result) {
System.out.println("下载成功!");
}else {
System.out.println("下载失败!");
}
//关闭文件流
fos.close();
//关闭连接
if (ftpClient != null) {
ftpClient.logout();
ftpClient.disconnect();
}
}


注:上面没有对异常做详细的捕获处理。

需要注意,对于ftpClient.changeWorkingDirectory(“path”)这个函数,如果指定的目录不存在,上传的时候会自动上传到根目录下,不会报错;同理,下载的时候,如果指定的目录不存在,也会从根目录下查找并下载该文件,如果根目录下也不存在,才会下载失败!

建议在上传下载的时候,文件名指定为绝对路径,如下:





也可以在上传的时候创建文件夹:

if (!ftpClient.changeWorkingDirectory(path)) {
if (ftpClient.makeDirectory(path)) {
System.out.println("目录创建成功!");
ftpClient.changeWorkingDirectory(path);
} else {
System.out.println("目录创建失败!");
}
}
//远程文件名
String removePath = new String("love7.txt".getBytes("UTF-8"),"iso-8859-1");
//上传
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
if(ftpClient.storeFile(removePath, in)){
System.out.println("文件上传成功");
}else{
System.out.println("文件上传失败");
}


同一个链接FTP多文件的下载

for("遍历FTP多个路径"){
InputStream in = ftpClient.retrieveFileStream("");
//每下一个文件,需要执行下面的这句话
ftpClient.completePendingCommand();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: