FTP2me is project is to create a J2ME (CLDC 1.0, MIDP 2.0) compatible ftp client library. The main component of the library is a class named Ftp2Me. It implements all of the ftp commands described in RFC959.
The purpose is to connect to a FTP server and download a file from the server to the client (mobile).
You can program it like this:
Code:
try {
ftpcon = new Ftp2Me(host, port, user, pass);
String cheminftp = ftpcon.pwd() ; // to know the working directory of my server
String url = "file:///Mes images/ftppp2.txt" ; // ftppp2 it's the path of the file I want to create on the mobile phone using JSR 75 FCA API
FileConnection fconn = (FileConnection)Connector.open(url);
if (!fconn.exists()) fconn.create(); // create the file if it doesn't exist
OutputStream os = fconn.openOutputStream() ;
ftpcon.retr(os,cheminftp+"/text.txt") ; // It needs OS only not DOS
// The above line retr, fetchs the text.txt file from server and writes it to OutputStream i.e. on the file system
fconn.close();
} catch (Exception e) {
ex.printStackTrace();
} catch (FtpProtocolException ex) {
ex.printStackTrace();
}
Try this code, it will work if you have the permissions to write to the target folder on the device and if your text.txt exist on the FTP server.
I could write this code after reading the implementation of retr method of FTP2ME
Code:
public void retr(OutputStream outputStream, String filename) throws IOException, FtpProtocolException {
PasvInfo pi = pasv();
sendLine("RETR " + filename);
SocketConnection dataInput = (SocketConnection) Connector.open("socket://"+pi.ip+":"+pi.port);
expectResponse(150);
InputStream in = dataInput.openInputStream();
byte[] buffer = new byte[128];
int bytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
in.close();
outputStream.close();
dataInput.close();
expectResponse(226);
}