在android中通过ftp上传视频文件

时间:2012-04-28 13:13:46

标签: android

嗨朋友们,我正在做一个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-> properties-> java build path- >添加外部罐子。即使我添加了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 actoivity中做到这一点:

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();
        }
    }
} 

1 个答案:

答案 0 :(得分:1)

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

ftp.enterLocalPassiveMode();