Android · 2015年2月10日 0

Android FTP客户端实现、Telnet客户端

Android FTP客户端实现

android中使用第三方库来操作FTP,这里使用Apache的包,下载地址为:http://commons.apache.org/proper/commons-net/download_net.cgi
其文件名称为:commons-net-3.3-bin.zip
步骤1:在项目中引入包commons-net-3.3.jar,导入需要的FTPClient类;
步骤2:初始化FTPClient
mFtp=new FTPClient();
步骤3:设置登录的地址和端口
mFtp.connect(ftpUrl,21);
步骤4:设置登录用户名和密码
mFtp.login(name,pwd);
步骤5:设置文件类型和采用被动传输方式
mFtp.setFileType(FTP.BINARY_FILE_TYPE);
mFtp.enterLocalPassiveMode();
步骤6:传输文件
boolean aRtn=mFtp.storeFile(remoteFileName,aInputStream);//成功返回true
aInputStream.close();
步骤7:关闭连接
mFtp.disconnect();

核心代码:
//导入需要的FTPClient类
import org.apache.commons.net.ftp.FTPClient;
//初始化FTPClient
FTPClient ftpClient=new FTPClient();
try{
//连接到指定的FTP服务器
ftpClient.connect(InetAddress.getByName(SERVER));
//使用用户名和密码登录FTP
ftpClient.login(USERNAME,PASSWORD);
//检查返回的字符串中是否包含250–250响应代码表示“行为完成”
if(ftpClient.getReplyString().contains(“250”)){
//设置文件类型
ftpClient.setFileType(//默认使用的是ASCII编码的,这里设置为二进制文件
org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE
);
//定义一个缓冲区
BufferedInputStream buffIn=null;
//将文件加载到缓冲区中
buffIn=new BufferedInputStream(new FileInputStream(FULL_PATH_TO_LOCAL_TYPE));
//设置客户端的PASV模式(客户端主动连服务器:端口用20)
ftpClient.enterLocalPassiveMode();
//存储文件。返回是否成功
boolean result=ftpClient.storeFile(localAsset.getFileName,progressInput);
//关闭缓冲区
buffIn.close();
//登出
ftpClient.logout();
//断开连接
ftpClient.disconnect();
}

}catch(SocketException e){

}catch(UnKnowHostException e){

}catch(IOEception ioe){

}
Telnet客户端
实现的远程控制Web服务器。
android使用第三方库Telnet,这里使用Apache的包,下载地址:http://commons.apache.org/proper/commons-net/download_net.cgi
其文件名称为:commons-net-3.3-bin.zip
步骤1:在项目中引入包commons-net-3.3.jar,导入需要的TelnetClient类;
步骤2:初始化TelnetClient
tc=new TelnetClient();
步骤3:打开连接
tc.connect(remoteip,remoteport);
步骤4:读写数据
tc.getInputStream(); tc.getOutputStream();
步骤5:断开Telnet连接
tc.disconnect();
核心代码:
//定义一个TelnetClient
TelnetClient tc=new TelnetClient();
try{
//连接到指定的FTP服务器
tc.connect(remoteip,remoteport);
}catch(IOEception ioe){
System.exit(1);
}
IOUtil.readWrite(tc.getInputStream(),tc.getOutputStream(),System.in,System.out);
try{
//断开连接
tc.disconnect();
}catch(IOEception ioe){

}

其中调用的IOUtil类封装了一些读写操作
public final class IOUtil{
public staic final void readWrite(final InputStream remoteInput,final OutputStream remoteOutput,final InputStream localInput,final OutputStream localOutput){
//定义读写的线程
Thread read,writer;
//定义读线程的具体操作
reader=new Thread(){
@Override
public void run(){
int ch;
try{
//判断没被中断的时候
while(!interrupred()&&(ch=localInput.read())!=-1){
//写自己到远程输入里面
remoteOutput.write(ch);
//刷新发送
remoteOutput.flush();
}
}catch(IOException e){

}
}
};
//定义写线程的具体操作
reader=new Thread(){
@Override
public void run(){
int ch;
try{
//把数据从输入流复制到输出流
Util.copyStream(remoteInput,localOutput);
}catch(IOException e){
System.exit(1);
}
}
};

//设置Writer线程
writer.setPriority(Thread.currentThread().getPriority()+1);
//启动writer线程
writer.start();
//设置reader为后台运行
reader.setDaemon(true);
//启动readre线程
reader.start();
try{
//使得writer线程完成run()方法后,在执行join()方法后的代码
writer.join();
//中断reader线程
reader.interrupt();
}catch(InterruptedException e){

}
};

}

Share this: