If you are looking for a FTP client for Android, then the following would work for you-
1. Use Apache FTP client
http://commons.apache.org/net/download_net.cgi
you will also need
http://commons.apache.org/io/download_io.cgi
2. FTP client used in Android
A nicely written class with provision to show progress.
http://code.google.com/p/andro-ftp/source/browse/trunk/src/net/abachar/androftp/transfers/manager/FTPTransferTask.java?r=32
I am pasting the class here in case it disappears from the source-
1. Use Apache FTP client
http://commons.apache.org/net/download_net.cgi
you will also need
http://commons.apache.org/io/download_io.cgi
2. FTP client used in Android
A nicely written class with provision to show progress.
http://code.google.com/p/andro-ftp/source/browse/trunk/src/net/abachar/androftp/transfers/manager/FTPTransferTask.java?r=32
I am pasting the class here in case it disappears from the source-
package net.abachar.androftp.transfers.manager; |
import java.io.FileInputStream; |
import java.io.FileOutputStream; |
import java.io.IOException; |
import java.io.PrintWriter; |
import java.net.SocketException; |
import java.util.List; |
import net.abachar.androftp.MainApplication; |
import net.abachar.androftp.filelist.manager.FTPFileManager; |
import net.abachar.androftp.servers.Logontype; |
import org.apache.commons.io.input.CountingInputStream; |
import org.apache.commons.io.output.CountingOutputStream; |
import org.apache.commons.net.PrintCommandListener; |
import org.apache.commons.net.ftp.FTP; |
import org.apache.commons.net.ftp.FTPClient; |
import org.apache.commons.net.ftp.FTPReply; |
/** |
* |
* @author abachar |
*/ |
public class FTPTransferTask extends TransferTask { |
/** */ |
private FTPClient mFTPClient; |
/** |
* |
*/ |
public FTPTransferTask(TransferTaskProgressListener progressListener, List<Transfer> transferList) { |
super(progressListener, transferList); |
} |
/** |
* @see android.os.AsyncTask#doInBackground(Params[]) |
*/ |
@Override |
protected String doInBackground(Transfer... transfers) { |
// Create ftp client |
mFTPClient = new FTPClient(); |
mFTPClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); |
String retval = super.doInBackground(transfers); |
// Disconnect ftp client |
if ((mFTPClient != null) && mFTPClient.isConnected()) { |
try { |
mFTPClient.logout(); |
mFTPClient.disconnect(); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
return retval; |
} |
/** |
* @see net.abachar.androftp.transfers.manager.TransferTask#doInBackgroundDownload() |
*/ |
@Override |
protected void doInBackgroundDownload() { |
try { |
// Connect it if disconnected |
if (!mFTPClient.isConnected()) { |
connect(); |
} |
// if (binaryTransfer) { |
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE); |
// } |
// Go to directory |
if (!mFTPClient.printWorkingDirectory().equals(mCurrentTransfer.getSourcePath())) { |
mFTPClient.changeWorkingDirectory(mCurrentTransfer.getSourcePath()); |
} |
// Open local file |
FileOutputStream fos = new FileOutputStream(mCurrentTransfer.getFullDestinationPath()); |
CountingOutputStream cos = new CountingOutputStream(fos) { |
protected void beforeWrite(int n) { |
super.beforeWrite(n); |
int progress = Math.round((getCount() * 100) / mCurrentTransfer.getFileSize()); |
mCurrentTransfer.setProgress(progress); |
publishProgress(mCurrentTransfer.getId(), progress); |
} |
}; |
// Download file |
mFTPClient.retrieveFile(mCurrentTransfer.getName(), cos); |
// Close local file |
fos.close(); |
// End of transfer |
publishProgress(mCurrentTransfer.getId(), 101); |
} catch (SocketException e) { |
e.printStackTrace(); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
/** |
* @see net.abachar.androftp.transfers.manager.TransferTask#doInBackgroundUpload() |
*/ |
@Override |
protected void doInBackgroundUpload() { |
try { |
// Connect it if disconnected |
if (!mFTPClient.isConnected()) { |
connect(); |
} |
// if (binaryTransfer) { |
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE); |
// } |
// Open local file |
FileInputStream fis = new FileInputStream(mCurrentTransfer.getFullSourcePath()); |
CountingInputStream cis = new CountingInputStream(fis) { |
protected void afterRead(int n) { |
super.afterRead(n); |
int progress = Math.round((getCount() * 100) / mCurrentTransfer.getFileSize()); |
mCurrentTransfer.setProgress(progress); |
publishProgress(mCurrentTransfer.getId(), progress); |
} |
}; |
// Go to directory |
if (!mFTPClient.printWorkingDirectory().equals(mCurrentTransfer.getDestinationPath())) { |
mFTPClient.changeWorkingDirectory(mCurrentTransfer.getDestinationPath()); |
} |
// Upload file |
mFTPClient.storeFile(mCurrentTransfer.getName(), cis); |
// Close local file |
fis.close(); |
// End of transfer |
publishProgress(mCurrentTransfer.getId(), 101); |
} catch (SocketException e) { |
e.printStackTrace(); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
/** |
* @throws IOException |
* @throws SocketException |
* |
*/ |
private void connect() throws SocketException, IOException { |
FTPFileManager fileManager = (FTPFileManager) MainApplication.getInstance().getServerFileManager(); |
// Connect to server |
mFTPClient.connect(fileManager.getHost(), fileManager.getPort()); |
// Check the reply code to verify success. |
int reply = mFTPClient.getReplyCode(); |
if (!FTPReply.isPositiveCompletion(reply)) { |
return; |
} |
if (fileManager.getLogontype() == Logontype.NORMAL) { |
if (!mFTPClient.login(fileManager.getUsername(), fileManager.getPassword())) { |
mFTPClient.logout(); |
return; |
} |
} |
// Use passive mode as default because most of us are |
// behind firewalls these days. |
mFTPClient.enterLocalPassiveMode(); |
} |
} |
MainActivity
ReplyDeletepublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ImageUploadTask().execute(i);
}
public class ImageUploadTask extends AsyncTask {
protected String doInBackground(String...args) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(InetAddress.getByName(SERVER));
ftpClient.login(USERNAME, PASSWORD);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
int reply = ftpClient.getReplyCode(); // 응답코드 체크
//textTargetUri.setText("good");
final String remote = "/1.jpg";//FTP adress/filename
String savefilepath = "/sdcard/download/1.jpg";
File get_file = new File(savefilepath);//download
OutputStream outputStream = new FileOutputStream(get_file);
ftpClient.retrieveFile(remote, outputStream );
long fileSize = 0;
FTPFile[] files = ftpClient.listFiles(remote);
if (files.length == 1 && files[0].isFile()) {
fileSize = files[0].getSize();
String a = String.valueOf(fileSize);
Log.d("File Size", a);
}
CountingOutputStream cos = new CountingOutputStream(outputStream) {
protected void beforeWrite(int n) {
super.beforeWrite(n);
int progress = Math.round((getCount() * 100) / 879394);
String b = String.valueOf(progress);
Log.d("File persent", b);
}
};
ftpClient.retrieveFile(remote, cos);
outputStream.close();
ftpClient.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
//textTargetUri.setText("finish");
}
return s;
}
}
CountingOutputStream error.
code no error.
APP start error.
What's wrong with the code, I do not know how?
please help me.