近期接触到一个需求,就是将文件从Android系统上传到FTP服务器,虽然以前接触过FTP服务器,了解基本的使用流程,可是将此流程从使用习惯转化为代码实现仍是有必定难度的。可是基本的流程仍是比较清楚的:java
- 1.在Android手机上建立一个FTP客户端
- 2.设置host及端口创建与FTP服务器的链接
- 3.使用用户名和密码登陆FTP服务器
- 4.更改目录,将当前客户端指向的服务端的文件夹从跟目录调整到指定的目录
- 5.上传文件,并等待上传完成的结果
- 6.断开和FTP服务器的连接
public class FTPClientFunctions { private static final String TAG = "FTPClientFunctions"; private FTPClient ftpClient = null; // FTP客户端 /** * 链接到FTP服务器 * * @param host ftp服务器域名 * @param username 访问用户名 * @param password 访问密码 * @param port 端口 * @return 是否链接成功 */ public boolean ftpConnect(String host, String username, String password, int port) { try { ftpClient = new FTPClient(); Log.d(TAG, "connecting to the ftp server " + host + " :" + port); ftpClient.connect(host, port); // 根据返回的状态码,判断连接是否创建成功 if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { Log.d(TAG, "login to the ftp server"); boolean status = ftpClient.login(username, password); /* * 设置文件传输模式 * 避免一些可能会出现的问题,在这里必需要设定文件的传输格式。 * 在这里咱们使用BINARY_FILE_TYPE来传输文本、图像和压缩文件。 */ ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); return status; } } catch (Exception e) { e.printStackTrace(); Log.d(TAG, "Error: could not connect to host " + host); } return false; } /** * 断开ftp服务器链接 * * @return 断开结果 */ public boolean ftpDisconnect() { // 判断空指针 if (ftpClient == null) { return true; } // 断开ftp服务器链接 try { ftpClient.logout(); ftpClient.disconnect(); return true; } catch (Exception e) { Log.d(TAG, "Error occurred while disconnecting from ftp server."); } return false; } /** * ftp 文件上传 * * @param srcFilePath 源文件目录 * @param desFileName 文件名称 * @param desDirectory 目标文件 * @return 文件上传结果 */ public boolean ftpUpload(String srcFilePath, String desFileName, String desDirectory) { boolean status = false; try { FileInputStream srcFileStream = new FileInputStream(srcFilePath); status = ftpClient.storeFile(desFileName, srcFileStream); srcFileStream.close(); return status; } catch (Exception e) { e.printStackTrace(); Log.d(TAG, "upload failed: " + e.getLocalizedMessage()); } return status; } /** * ftp 更改目录 * * @param path 更改的路径 * @return 更改是否成功 */ public boolean ftpChangeDir(String path) { boolean status = false; try { status = ftpClient.changeWorkingDirectory(path); } catch (Exception e) { e.printStackTrace(); Log.d(TAG, "change directory failed: " + e.getLocalizedMessage()); } return status; } }
// 网络操做,但开一个线程进行处理 new Thread(new Runnable() { @Override public void run() { // TODO 能够首先去判断一下网络 ftpClient = new FTPClientFunctions(); boolean connectResult = ftpClient.ftpConnect(FTP_SERVER, FTP_USERNAME, FTP_PASSWORD, FTP_PORT); if (connectResult) { boolean changeDirResult = ftpClient.ftpChangeDir("/**"); if (changeDirResult) { boolean uploadResult = ftpClient.ftpUpload(sourceFilePath, descFileName, ""); if (uploadResult) { Log.w(TAG, "上传成功"); boolean disConnectResult = ftpClient.ftpDisconnect(); if(disConnectResult) { Log.e(TAG, "关闭ftp链接成功"); } else { Log.e(TAG, "关闭ftp链接失败"); } } else { Log.w(TAG, "上传失败"); } } else { Log.w(TAG, "切换ftp目录失败"); } } else { Log.w(TAG, "链接ftp服务器失败"); } } }).start();
commons-net-3.6.jar : Apache Commons Net 阿帕奇提供的经常使用的网络交互的jar包。
支持如下协议:apache
- FTP/FTPS
- FTP over HTTP (experimental)
- NNTP
- SMTP(S)
- POP3(S)
- IMAP(S)
- Telnet
- TFTP
- Finger
- Whois
- rexec/rcmd/rlogin
- Time (rdate) and Daytime
- Echo
- Discard
- NTP/SNTP
在这里咱们只用的了FTP协议。不得不说,库使用起来仍是至关方便的。
相关连接:http://commons.apache.org/proper/commons-net/服务器