1

嗨朋友们
,我正在做一个 android 应用程序,我想从我的 sd 卡中选择一个视频并将其上传到 ftp 服务器。当我在模拟器中运行项目时,我得到了04-28 18:22:00.810: ERROR/AndroidRuntime(1053): java.lang.NoClassDefFoundError: org.apache.commons.net.ftp.FTPClient。有没有办法解决这个问题。我包括两个jar文件org.apache.commons.net.jar和commons-net-1.4.1.jar通过右键单击我的项目->属性->java构建路径->添加外部jars。即使我添加了jar文件,项目没有运行。如果有人知道,请帮助我...
我正在使用以下代码:

package net.jeema.UploadFTP;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

**import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;**

import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class UploadFTPActivity extends Activity {
    SQLiteDatabase myDataBase;
    // public String[] gur = new String[4];

    private static final int SELECT_VIDEO = 1;
    String strXmlResponse, url = null;
    String selectedPath, extension;
    Button bnupload, bnbrowse = null;
    String fname = null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addnewvideo);
        bnupload = (Button) findViewById(R.id.buttonUpload);
        bnbrowse = (Button) findViewById(R.id.buttonBrowse);
        addVIDEO();
    }

    public void addVIDEO {      
        bnbrowse.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setType("video/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Video"),
                        SELECT_VIDEO);
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode,
            final Intent data) {

        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_VIDEO) {
                System.out.println("SELECT VIDEO");
                Uri selectedImageUri = data.getData();
                selectedPath = getPath(selectedImageUri);
                System.out.println("SELECT_VIDEO Path : " + selectedPath);
                int pos = selectedPath.lastIndexOf(".");
                if (pos > 0) {
                    extension = selectedPath.substring(pos + 1);
                }               
                doFileUpload();
            }
        }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Video.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

    public void doFileUpload() {
        bnupload.setOnClickListener(new OnClickListener() {
            final SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());

            public void onClick(View arg0) {
                UUID uniqueKey = UUID.randomUUID();
                fname = uniqueKey.toString();
                Log.e("UNIQUE NAME", fname);        
                // TODO Auto-generated method stub
                String hostName = "MY HOST NAME";
                String username = "****";
                String password = "****";
                String location = selectedPath;
                FTPClient ftp = null;

                InputStream in = null;
                try {
                    ftp = new FTPClient();
                    ftp.connect(hostName);
                    ftp.login(username, password);

                    ftp.setFileType(FTP.BINARY_FILE_TYPE);

                    ftp.changeWorkingDirectory("/uploads");

                    int reply = ftp.getReplyCode();
                    System.out.println("Received Reply from FTP Connection:" + reply);

                    if (FTPReply.isPositiveCompletion(reply)) {
                        System.out.println("Connected Success");
                    }

                    File f1 = new File(location);
                    in = new FileInputStream(f1);

                    ftp.storeFile(fname+"."+extension, in);

                    System.out.println("SUCCESS");

                    ftp.logout();
                    ftp.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                }           
            }
            public String getCharacterDataFromElement(Element e) {
                Node child = e.getFirstChild();
                if (child instanceof CharacterData) {
                    CharacterData cd = (CharacterData) child;
                    return cd.getData();
                }
                return "";
            }
        });
    }
}

我使用带有以下代码的简单 java 程序获得了输出。但我想在 android 活动中执行此操作:

import java.io.File;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FtpTest {

    public static void main(String args[]) {
        String hostName = "MY HOST NAME";
        String username = "****";
        String password = "****";
        String location = "F:\\droid-samples\\VID-20120421-103134.3gp";
        FTPClient ftp = null;

        InputStream in = null;
        try {
            ftp = new FTPClient();
            ftp.connect(hostName);
            ftp.login(username, password);

            ftp.setFileType(FTP.BINARY_FILE_TYPE);

            ftp.changeWorkingDirectory("/uploads");

            int reply = ftp.getReplyCode();
            System.out.println("Received Reply from FTP Connection:" + reply);

            if (FTPReply.isPositiveCompletion(reply)) {
                System.out.println("Connected Success");
            }

            File f1 = new File(location);
            in = new FileInputStream(f1);

            ftp.storeFile("VID-20120421-103134.3gp", in);

            System.out.println("SUCCESS");

            ftp.logout();
            ftp.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 
4

1 回答 1

1

您可以尝试解决 0 字节问题:

ftp.enterLocalPassiveMode();
于 2012-05-05T05:47:24.090 回答