• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps error download file from ftp

yakup2015

Lurker
Feb 25, 2015
1
0
sorry for my bad english. i want to make ftp downloader and uploader. i find sample code. this code working in java but not working in android. i am using eclipse. how i can fix this error? :(
i am using this sample http://www.codejava.net/java-se/networking/ftp/java-ftp-file-download-tutorial-and-example

1-3 permission(phone state,internet,sd write) ok.
2-apache commons-net libs ok.

rgOy3m.png


and i get this error:

gkObvZ.png


and full code

Java:
package com.example.deneme10;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.BufferedOutputStream;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.Menu;
    import android.view.MenuItem;
    import org.apache.commons.net.ftp.FTP;
    import org.apache.commons.net.ftp.FTPClient;
    public class MainActivity extends Activity {
        private ProgressDialog dialog=new ProgressDialog(MainActivity.this);
        public class BackgroundTask extends AsyncTask<Void, Void, Void> {
              @Override
              protected void onPreExecute() {
                 super.onPreExecute();
                 dialog.setMessage("Yükleniyor");
                 dialog.show();
              }
              @Override
              protected Void doInBackground(Void... params) {
                  String server = "xxxx";
                  int port = 21;
                  String user = "xxxx";
                  String pass = "xxxx";
                  FTPClient ftpClient = new FTPClient();
                  try {
                      ftpClient.connect(server, port);
                      ftpClient.login(user, pass);
                      ftpClient.enterLocalPassiveMode();
                      ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                      // APPROACH #1: using retrieveFile(String, OutputStream)
                      String yol = Environment.getExternalStorageDirectory().toString();
                      String remoteFile1 = "/video.mp4";
                      File downloadFile1 = new File(yol+"/video.mp4");
                      OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
                      boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
                      outputStream1.close();
                      if (success) {
                          System.out.println("File #1 has been downloaded successfully.");
                      }
                      // APPROACH #2: using InputStream retrieveFileStream(String)
                      String yol2 = Environment.getExternalStorageDirectory().toString();
                      String remoteFile2 = "/sarki.mp3";
                      File downloadFile2 = new File(yol2+"/sarki.mp3");
                      OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2));
                      InputStream inputStream = ftpClient.retrieveFileStream(remoteFile2);
                      byte[] bytesArray = new byte[4096];
                      int bytesRead = -1;
                      while ((bytesRead = inputStream.read(bytesArray)) != -1) {
                          outputStream2.write(bytesArray, 0, bytesRead);
                      }
                      success = ftpClient.completePendingCommand();
                      if (success) {
                          System.out.println("File #2 has been downloaded successfully.");
                      }
                      outputStream2.close();
                      inputStream.close();
                  } catch (IOException ex) {
                      System.out.println("Error: " + ex.getMessage());
                      ex.printStackTrace();
                  } finally {
                      try {
                          if (ftpClient.isConnected()) {
                              ftpClient.logout();
                              ftpClient.disconnect();
                          }
                      } catch (IOException ex) {
                          ex.printStackTrace();
                      }
                  }
                 return null;
              }
              @Override
              protected void onPostExecute(Void result) {
                 super.onPostExecute(result);
                 dialog.dismiss();
              }
              @Override
              protected void onProgressUpdate(Void... values) {
                 super.onProgressUpdate(values);
              }
              @Override
              protected void onCancelled(Void result) {
                 super.onCancelled(result);
              }
           }
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            new BackgroundTask().execute();
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones