Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- server = "192.168.0.100";
- port = 21;
- user = "anonymous";
- pass = "anonymous";
- ftpConnect(server, user, pass, port);
- ftpPrintFilesList("/");
- ftpChangeDirectory("/datalogger/");
- ftpPrintFilesList("/datalogger/");
- ftpDownload("/datalogger/", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
- ftpDisconnect();
- public boolean ftpConnect(String host, String username, String password, int port)
- {
- try {
- ftpClient = new FTPClient();
- // connecting to the host
- ftpClient.connect(host, port);
- // now check the reply code, if positive mean connection success
- if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
- ftpClient.enterLocalPassiveMode();
- // login using username & password
- boolean status = ftpClient.login(username, password);
- /* Set File Transfer Mode
- *
- * To avoid corruption issue you must specified a correct
- * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
- * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE
- * for transferring text, image, and compressed files.
- */
- ftpClient.setFileType(org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE);
- ftpClient.enterLocalPassiveMode();
- Log.d(TAG, "Buffer Size:" + ftpClient.getBufferSize());
- this.ftpClient.setBufferSize(1024 * 1024);
- Log.d(TAG, "Buffer Size:" + ftpClient.getBufferSize());
- ftpClient.setAutodetectUTF8(true);
- return status;
- }
- } catch(Exception e) {
- Log.d(TAG, "Error: could not connect to host " + host );
- }
- return false;
- }
- public void ftpPrintFilesList(String dir_path)
- {
- try {
- FTPFile[] ftpFiles = ftpClient.listFiles(dir_path);
- int length = ftpFiles.length;
- for (int i = 0; i < length; i++) {
- String name = ftpFiles[i].getName();
- boolean isFile = ftpFiles[i].isFile();
- if (isFile) {
- Log.i(TAG, "File : " + name);
- }
- else {
- Log.i(TAG, "Directory : " + name);
- }
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement