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

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

直接上代码:

/**  * 通过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包地址 :http://download.csdn.net/detail/baolong47/4096104

转:http://blog.csdn.net/baolong47/article/details/7301394

更多相关文章

  1. Android客户端和Struts交互Json数据
  2. Ubuntu和Win下:Android(安卓)Studio and Gradle are using differ
  3. Android简易聊天室软件(HTTP实现)
  4. android 手机获取外置SD卡路径
  5. AssetBundle
  6. 【5.30】android开发环境的搭建
  7. SD卡文件列表
  8. Android(安卓)服务端将位置信息发送给客户端的实现
  9. web端、android端的文件上传

随机推荐

  1. Android小项目————聊天室(网络编程篇)
  2. android 手把手教你打造万能的ListView G
  3. Handler入门及操作
  4. Android开发全程记录(十四)——Android显示
  5. 用vector drawable加快应用图片加载速度
  6. Android服务器时间校准
  7. 使用Android(安卓)Studio检测内存泄露
  8. Camera 总结
  9. Android调用WebService系列之对象构建传
  10. Android(安卓)ViewDragHelper实现QQ侧滑