Android通过ftp上传、下载文件

                在开发中有可能会遇到通过ftp协议来上传和下载文件,网上也有很多的帖子,但大部分都没什么用,通过参考其他和自己思考写了两个经测试可用的方法,这两个方法需要一个commons-net-3.x的jar包,具体可以去这里下载,里面是我放的一些常用的jar包,会持续更新。下面具体看一下实现的方法。


1.上传文件

/**
	 * ftp上传
	 * @param url	ftp地址
	 * @param port  ftp连接端口号
	 * @param username  登录用户名
	 * @param password  登录密码
	 * @param fileNamePath  本地文件保存路径
	 * @param fileName  本地文件名
	 * @return
	 */
	public static String ftpUpload(String url, String port, String username,String password, String fileNamePath,String fileName){
		 FTPClient ftpClient = new FTPClient();
		 FileInputStream fis = null;  
		 String returnMessage = "0";  
		 try {  
		     ftpClient.connect(url, Integer.parseInt(port));  
		     boolean loginResult = ftpClient.login(username, password);  
		     int returnCode = ftpClient.getReplyCode();  
		     if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
		         ftpClient.setBufferSize(1024);  
		         ftpClient.setControlEncoding("UTF-8");  
		         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
		         ftpClient.enterLocalPassiveMode();  
		         fis = new FileInputStream(fileNamePath + fileName);  
		         ftpClient.storeFile(fileName, fis);  
		         
		         returnMessage = "1";   //上传成功        
		     } else {// 如果登录失败  
		         returnMessage = "0";  
		     }  
		  
		 } catch (IOException e) {  
		     e.printStackTrace();  
//		     throw new RuntimeException("FTP客户端出错!", e);
			 returnMessage = "-1";
		 } finally {  
		     //IOUtils.closeQuietly(fis);  
			 try {  
			     ftpClient.disconnect();  
			 } catch (IOException e) {  
			        e.printStackTrace();  
			        throw new RuntimeException("关闭FTP连接发生异常!", e);  
			 }  
		 }  
		 return returnMessage;  
	}

2.下载文件

/**
	 * ftp下载
	 * @param url
	 * @param port
	 * @param username
	 * @param password
	 * @param filePath 存放文件的路径
	 * @param FTP_file 要下载的文件名
	 * @param SD_file 本地文件名
	 */
	public static String ftpDown(String url, int port, String username,String password, String filePath,String FTP_file,String SD_file){
		BufferedOutputStream buffOut = null;
		
		FTPClient ftpClient = new FTPClient();
		String returnMessage = "0";  
		 try {  
		     ftpClient.connect(url, port);  
		     boolean loginResult = ftpClient.login(username, password);  
		     int returnCode = ftpClient.getReplyCode();  
		     if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
		         ftpClient.setBufferSize(1024);  
		         ftpClient.setControlEncoding("UTF-8");  
		         ftpClient.enterLocalPassiveMode();  
		         
		         buffOut = new BufferedOutputStream(new FileOutputStream(filePath+SD_file),8*1024);
		         ftpClient.retrieveFile(FTP_file, buffOut);
		         buffOut.flush();
		         buffOut.close();
		         
		         ftpClient.logout();
		         ftpClient.disconnect();
		         
		         returnMessage = "1";   //上传成功        
		     } else {// 如果登录失败  
		         returnMessage = "0";  
		     }  
		  
		 } catch (IOException e) {  
		     e.printStackTrace();  
		     throw new RuntimeException("FTP客户端出错!", e);  
		 } finally {  
		     //IOUtils.closeQuietly(fis);  
			 try {  
			     ftpClient.disconnect();  
			 } catch (IOException e) {  
			        e.printStackTrace();  
			        throw new RuntimeException("关闭FTP连接发生异常!", e);  
			 }  
		 }  
		 return returnMessage;
	}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值