无法将文件上传到ftp服务器

时间:2014-01-25 10:06:17

标签: android ftp ftp-client

我从google svn repository获得了一个android ftp项目。当它从ftp服务器下载文件到android设备时工作正常。但是当我从Android设备上传文件到ftp服务器时,它会显示一些异常,例如文件未找到异常,但我无法解决这个问题,请帮助我解决这个问题。

package com.example.ftptest;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.net.ftp.FTPFile;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class UploadActivity extends Activity {

    private static final String TAG = "UploadActivity";
    private  ListView maLV;
    private String  pass, host, user;
    private int port;
    private  MyFTPClient ftpClient;
    private  FTPFile[] ftpFiles;
    private File fichierLocal;
    private FTPFile fichierServeur, dossierServeur;
    private Context monContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        Button button1=(Button)findViewById(R.id.button1);
        monContext = this;
        //récupère les donnéees
         Bundle extra= this.getIntent().getExtras();

            if(extra!=null){
                pass = extra.getString("pass");
                host = extra.getString("host");
                user = extra.getString("user");
                port = extra.getInt("port");
            }
            //se connecte au serveur FTP
           ftpClient = new MyFTPClient();
           Log.d(TAG, "HOST " +host);
           Log.d(TAG, "USER " +user);
           Log.d(TAG, "PASS " +pass);
           Log.d(TAG, "PORT " +port);
           ftpClient.ftpConnect(host, user, pass, port);

           uploadRefresh();
           downloadRefresh();

        button1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub  

                downloadRefresh();
            }

        });

        Button button2=(Button)findViewById(R.id.button2);
        button2.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub  

                uploadRefresh();
            }

        });

        Button upload=(Button)findViewById(R.id.upload);
        upload.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    Log.i(TAG, "upload");
                    Log.i(TAG, "fichier : " + fichierLocal.getPath());
                    //Log.i(TAG, "dossier : " + dossierServeur.getName());
                    final String FPATH = fichierLocal.getPath();
                    Filename filename = new Filename(FPATH, '/', '.');
                    System.out.println("Extension = " + filename.extension());
                    System.out.println("Filename = " + filename.filename());
                    System.out.println("Path = " + filename.path());
                    ftpClient.ftpUpload(fichierLocal.getPath(), filename.filename()+filename.extension(),"/", monContext);
                    //Refresh de la liste

                    uploadRefresh();
                } catch (Exception e) {

                    System.out.println("sarath:from upload");
                    e.printStackTrace();
                }
            }
        });

        Button download=(Button)findViewById(R.id.download);
        download.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    Log.i(TAG, "download");
                    Log.i(TAG, "fichier : " + fichierServeur.getName());
                    //Log.i(TAG, "dossier : " + dossierLocal.getName());
                    ftpClient.ftpDownload(fichierServeur.getName(), Environment.getExternalStorageDirectory().getName() + "/" + fichierServeur.getName());
                    //Refresh de la liste
                    downloadRefresh();

                } catch (Exception e) {
                    System.out.println("sarath:from upload");
                    e.printStackTrace();
                }               
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.upload, menu);
        return true;
    }
    private File[] localFiles;
    public void localPrintFilesList(File dir, String header)
    {
        try {
            Log.d(TAG, "recuperation des données du repertoires");
            maLV = (ListView) findViewById(R.id.listView1);
            localFiles = dir.listFiles();
            int length = localFiles.length;

            //Création de la ArrayList qui nous permettra d'alimenter la listView
             ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();

             //On déclare la HashMap qui contiendra les informations pour un item
             HashMap<String, String> map;

            //String[] values = new String[length];
            Log.d(TAG, "nombre de fichier dans le repertoire : " +length);
            for (int i = 0; i < length; i++) {
                // initialisation du HashMap
                map = new HashMap<String, String>();

                String name = localFiles[i].getName();
                boolean isFile = localFiles[i].isFile();
                //values[i] = name;
                if (isFile) {
                    Log.i(TAG, "File : " + name);
                    map.put("img", String.valueOf(R.drawable.fichier));
                }
                else {
                    Log.i(TAG, "Directory : " + name);
                    map.put("img", String.valueOf(R.drawable.dossier));
                }
                map.put("title", name);        

                //Ajoute à la liste    
                listItem.add(map);
            }

            //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                //R.layout.listviewitem, android.R.id.text1, values);
            SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.listviewitem,
                       new String[] {"img", "title"}, new int[] {R.id.img, R.id.title});
            //TextView tv = new TextView(this);
            //tv.setText(header);
            //maLV.addHeaderView(tv);
            maLV.setAdapter(mSchedule);


            maLV.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                      Log.i(TAG, "Position : " + position);
                      Log.i(TAG, "longueur liste : " + localFiles.length);
                    //On regarde si c'est un fichier ou un dossier
                    boolean isFile = localFiles[position].isFile();
                    if (isFile)
                    {
                        fichierLocal =  localFiles[position];
                    }
                    else
                    {
                        localPrintFilesList(localFiles[position], localFiles[position].getName());
                    }

                }
            });
        } catch(Exception e) {
            e.printStackTrace();
        }
    } 


    public void serverPrintFilesList(String ftpDir, String header)
    {
        try {
            maLV = (ListView) findViewById(R.id.ListView2);
            Log.d(TAG, "recuperation des données du repertoires");
            ftpFiles = ftpClient.mFTPClient.listFiles(ftpDir);
            for (FTPFile ftpliste : ftpFiles){
            Log.d(TAG, "liste des repertoire : " +ftpliste.getName() );
            }
            int length = ftpFiles.length;
            Log.d(TAG, "nombre de fichier dans le repertoire : " +length);
            //Création de la ArrayList qui nous permettra d'alimenter la listView
             ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();

             //On déclare la HashMap qui contiendra les informations pour un item
             HashMap<String, String> map;

            for (int i = 0; i < length; i++) {
                map = new HashMap<String, String>();

                String name = ftpFiles[i].getName();
                boolean isFile = ftpFiles[i].isFile();

                if (isFile) {
                    Log.i(TAG, "File : " + name);
                    map.put("img", String.valueOf(R.drawable.fichier));
                }
                else {
                    Log.i(TAG, "Directory : " + name);
                    map.put("img", String.valueOf(R.drawable.dossier));
                }
                map.put("title", name);        

                //Ajoute à la liste    
                listItem.add(map);
            }

          //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            //R.layout.listviewitem, android.R.id.text1, values);
        SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.listviewitem,
                   new String[] {"img", "title"}, new int[] {R.id.img, R.id.title});
        //TextView tv = new TextView(this);
        //tv.setText(header);
        //maLV.addHeaderView(tv);
        maLV.setAdapter(mSchedule);


        maLV.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                //On récupère le titre de l'Item dans un String
                boolean isFile = ftpFiles[position].isFile();
                if (isFile)
                {
                    fichierServeur = ftpFiles[position];
                }
                else
                {
                    dossierServeur = ftpFiles[position];
                    serverPrintFilesList(ftpFiles[position].getName(), ftpFiles[position].getName());
                }

            }
        });
        } catch(Exception e) {
            e.printStackTrace();
        }
    } 

    public void downloadRefresh(){
        //Rafrechit la liste
        Log.d(TAG, "avant recuperation du repertoire");
        File dir= Environment.getExternalStorageDirectory();
        Log.d(TAG, "apres recuperation du repertoire");
        Log.d(TAG, dir.toString());
        localPrintFilesList(dir, "LOCAL FILES");

    }

    public void uploadRefresh(){
        //Refresh the uploadlist
        Log.d(TAG, "Liste du repertoire du server1 : " +ftpClient.ftpGetCurrentWorkingDirectory());
        try {
            serverPrintFilesList(ftpClient.ftpGetCurrentWorkingDirectory(), "SERVER FILES");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.d(TAG, "Impossible de recuperer la liste  ");
            e.printStackTrace();
        }

    }

}

logcat的

01-25 15:22:35.679: W/System.err(22285): java.lang.IllegalArgumentException: File /mnt/sdcard/ringtone-billa.mp3 contains a path separator
01-25 15:22:35.710: W/System.err(22285):    at android.app.ContextImpl.makeFilename(ContextImpl.java:1673)
01-25 15:22:35.710: W/System.err(22285):    at android.app.ContextImpl.openFileInput(ContextImpl.java:416)
01-25 15:22:35.710: W/System.err(22285):    at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
01-25 15:22:35.710: W/System.err(22285):    at com.example.ftptest.MyFTPClient.ftpUpload(MyFTPClient.java:225)
01-25 15:22:35.710: W/System.err(22285):    at com.example.ftptest.UploadActivity$3.onClick(UploadActivity.java:96)
01-25 15:22:35.710: W/System.err(22285):    at android.view.View.performClick(View.java:2485)
01-25 15:22:35.710: W/System.err(22285):    at android.view.View$PerformClick.run(View.java:9080)
01-25 15:22:35.710: W/System.err(22285):    at android.os.Handler.handleCallback(Handler.java:587)
01-25 15:22:35.710: W/System.err(22285):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 15:22:35.710: W/System.err(22285):    at android.os.Looper.loop(Looper.java:130)
01-25 15:22:35.710: W/System.err(22285):    at android.app.ActivityThread.main(ActivityThread.java:3687)
01-25 15:22:35.710: W/System.err(22285):    at java.lang.reflect.Method.invokeNative(Native Method)
01-25 15:22:35.710: W/System.err(22285):    at java.lang.reflect.Method.invoke(Method.java:507)
01-25 15:22:35.710: W/System.err(22285):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-25 15:22:35.710: W/System.err(22285):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-25 15:22:35.710: W/System.err(22285):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案