灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:1715回复:0

android小记之FTP文件上传

楼主#
更多 发布于:2012-09-06 13:44


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

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

直接上代码:

[java]
/**
* 通过ftp上传文件
* @param url ftp服务器地址 如: 192.168.1.110
* @param port 端口如 : 21
* @param username  登录名
* @param password   密码
* @param remotePath  上到ftp服务器的磁盘路径
* @param fileNamePath  要上传的文件路径
* @param fileName      要上传的文件名
* @return
*//www.atcpu.com
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;
}


喜欢0 评分0
游客

返回顶部