Apache FTPClient上传下载文件

使用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();
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值