Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1386474
  • 博文数量: 188
  • 博客积分: 1784
  • 博客等级: 上尉
  • 技术积分: 2772
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-05 22:20
个人简介

发上等愿,结中等缘,享下等福;择高处立,就平处坐,向宽处行。

文章分类

全部博文(188)

文章存档

2020年(12)

2019年(11)

2018年(4)

2017年(3)

2016年(11)

2015年(22)

2014年(19)

2013年(25)

2012年(32)

2011年(49)

分类: 嵌入式

2012-04-11 19:30:36

android客户端实现FTP文件(包括图片)上传应该没什么难度。写下来就了为了记录一下,望能帮到新手。

 

需要用到 commons-net-3.0.1.jar,后面附上jar包。

 

直接上代码:

 

  1. /** 
  2.  * 通过ftp上传文件 
  3.  * @param url ftp服务器地址 如: 192.168.1.110 
  4.  * @param port 端口如 : 21 
  5.  * @param username  登录名 
  6.  * @param password   密码 
  7.  * @param remotePath  上到ftp服务器的磁盘路径 
  8.  * @param fileNamePath  要上传的文件路径 
  9.  * @param fileName      要上传的文件名 
  10.  * @return 
  11.  */  
  12. public String ftpUpload(String url, String port, String username,String password, String remotePath, String fileNamePath,String fileName) {  
  13.  FTPClient ftpClient = new FTPClient();  
  14.  FileInputStream fis = null;  
  15.  String returnMessage = "0";  
  16.  try {  
  17.      ftpClient.connect(url, Integer.parseInt(port));  
  18.      boolean loginResult = ftpClient.login(username, password);  
  19.      int returnCode = ftpClient.getReplyCode();  
  20.      if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功   
  21.          ftpClient.makeDirectory(remotePath);  
  22.          // 设置上传目录   
  23.          ftpClient.changeWorkingDirectory(remotePath);  
  24.          ftpClient.setBufferSize(1024);  
  25.          ftpClient.setControlEncoding("UTF-8");  
  26.          ftpClient.enterLocalPassiveMode();  
  27.                  fis = new FileInputStream(fileNamePath + fileName);  
  28.          ftpClient.storeFile(fileName, fis);  
  29.            
  30.          returnMessage = "1";   //上传成功         
  31.      } else {// 如果登录失败   
  32.          returnMessage = "0";  
  33.          }  
  34.                
  35.   
  36.  } catch (IOException e) {  
  37.      e.printStackTrace();  
  38.      throw new RuntimeException("FTP客户端出错!", e);  
  39.  } finally {  
  40.      //IOUtils.closeQuietly(fis);   
  41.  try {  
  42.      ftpClient.disconnect();  
  43.  } catch (IOException e) {  
  44.         e.printStackTrace();  
  45.         throw new RuntimeException("关闭FTP连接发生异常!", e);  
  46.     }  
  47.  }  
  48.  return returnMessage;  
  49. }  
/** * 通过ftp上传文件 * @param url ftp服务器地址 如: 192.168.1.110 * @param port 端口如 : 21 * @param username 登录名 * @param password 密码 * @param remotePath 上到ftp服务器的磁盘路径 * @param fileNamePath 要上传的文件路径 * @param fileName 要上传的文件名 * @return */ public String ftpUpload(String url, String port, String username,String password, String remotePath, 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.makeDirectory(remotePath); // 设置上传目录 ftpClient.changeWorkingDirectory(remotePath); ftpClient.setBufferSize(1024); ftpClient.setControlEncoding("UTF-8"); 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); } finally { //IOUtils.closeQuietly(fis); try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭FTP连接发生异常!", e); } } return returnMessage; }


 

jar包地址 :

阅读(2620) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~