开发环境:
1、模拟器用sun java(TM)wireless Toolkit 2.5 CLDC
2、FTP 用filezilla 开源FTP服务器
?
手机FTP开源项目ftp2me的应用
用的规范是RFC959,下面是主要的调用方法,附件中是工程实例,有空的朋友可以下下来看看
?
package main;
?
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
?
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
?
import ftp.Ftp2Me;
import ftp.FtpProtocolException;
?
public class FtpMethod implements Runnable{
private static String hostip = null;
private static int port;
private static String username = null;
private static String password = null;
Ftp2Me fm = null;
public FtpMethod(String hostip,int port,String username,String password){
this.hostip = hostip;
this.port = port;
this.username = username;
this.password = password;
Thread t = new Thread();
t.run();
}
?
public void run() {
try {
fm = new Ftp2Me(hostip,port,username,password);
makeDirectory("bb");
//getList("bb");
//getNlst("bb");
//upLoad("file:///root1/EditPlus.txt","bb/EditPlus.txt");
//downLoad("file:///root1/EditPlus.txt","bb/EditPlus.txt");
//reName("bb/ff.txt","bb/bb.txt");
//delfile("bb/aa.txt");
//remove("bb");
} catch (IOException e) {
e.printStackTrace();
} catch (FtpProtocolException e){
e.printStackTrace();
}
}
/**
* 删除文件
* @param url
*/
public void delfile(String url){
try {
fm.dele(url);
fm.disconnect();
} catch (IOException e) {
e.printStackTrace();
} catch (FtpProtocolException e) {
e.printStackTrace();
}
}
/**
* 移除(空)文件夹
* @param fileBoxUrl
* @return
*/
public boolean remove(String fileBoxUrl){
try {
fm.rmd(fileBoxUrl);
fm.disconnect();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (FtpProtocolException e) {
e.printStackTrace();
return false;
}
}
/**
* 退出
*/
public void quit(){
try {
fm.quit();
} catch (IOException e) {
e.printStackTrace();
} catch (FtpProtocolException e) {
e.printStackTrace();
}
}
?
/**
* 文件及文件夹重命名
* @param srcUrl
* @param newUrl
* @return
*/
public boolean reName(String srcUrl,String newUrl){
try {
fm.rnfr(srcUrl);
fm.rnto(newUrl);
fm.disconnect();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (FtpProtocolException e) {
e.printStackTrace();
return false;
}
}
/**
* 创建文件夹
* @param url
* @return
*/
public boolean makeDirectory(String url){
try {
fm.mkd(url);
fm.disconnect();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (FtpProtocolException e) {
e.printStackTrace();
return false;
}
}
/**
* 下载单个文件
* @param localurl
* @param ftpurl
* @return
*/
public boolean downLoad(String localurl,String ftpurl){
try {
FileConnection f = (FileConnection) Connector.open(localurl);
if(!f.exists()){
f.create();
}
OutputStream os = ?f.openOutputStream();
fm.retr(os,ftpurl);
fm.disconnect();
f.close();
os.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (FtpProtocolException e) {
e.printStackTrace();
return false;
}
}
?
/**
* 上传单个文件
* @param localurl
* @param ftpurl
* @return
*/
public boolean upLoad(String localurl,String ftpurl){
try {
FileConnection f = (FileConnection) Connector.open(localurl);
InputStream is = f.openInputStream();
fm.appe(is,ftpurl);
fm.disconnect();
f.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (FtpProtocolException e) {
e.printStackTrace();
return false;
}
}
/**
* 显示文件夹及文件
* @param fileBoxUrl
* @return
*/
public void getNlst(String fileBoxUrl){
try {
fm.cwd(fileBoxUrl);
Enumeration e = fm.list();
if(e != null){
while(e.hasMoreElements()){
Object o = e.nextElement();
String t = (String)o;
System.out.println(t);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (FtpProtocolException e) {
e.printStackTrace();
}
fm.disconnect();
}
/**
*显示文件
* @param fileBoxUrl
*/
public void getList(String fileBoxUrl){
try {
fm.cwd(fileBoxUrl);
Enumeration e = fm.list();
if(e != null){
while(e.hasMoreElements()){
Object o = e.nextElement();
String t = (String)o;
System.out.println(t);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (FtpProtocolException e) {
e.printStackTrace();
}
fm.disconnect();
}
}
?