ftp客户端上的noClassDefFound错误:org.apache.commons.net.ftp.FTPClient

时间:2012-10-04 15:47:31

标签: java android ftp apache-commons ftp-client

我正在开发一个将文件发送到url数据库的应用程序。我开始只是试图发送图片。目前我的java中出现了noclassDefFound错误。我已经尝试过更新java,删除并再次添加jar文件,然后我仔细检查了我的清单以获得适当的权限,所以我来这里寻求帮助。我发布了LogCat,但我把问题缩小到两行代码:FTP类中有24行,上传类中有82行。我还包括有问题的课程和我的清单。

logcat的

    10-04 11:33:36.028: E/AndroidRuntime(5013): FATAL EXCEPTION: main
10-04 11:33:36.028: E/AndroidRuntime(5013): java.lang.NoClassDefFoundError: org.apache.commons.net.ftp.FTPClient
10-04 11:33:36.028: E/AndroidRuntime(5013):     at com.cameratest.UploadFTP.FTPUpload(UploadFTP.java:23)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at com.cameratest.UploadMedia.onActivityResult(UploadMedia.java:80)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at android.app.Activity.dispatchActivityResult(Activity.java:5192)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at android.app.ActivityThread.access$1100(ActivityThread.java:130)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at android.os.Looper.loop(Looper.java:137)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at java.lang.reflect.Method.invoke(Method.java:511)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-04 11:33:36.028: E/AndroidRuntime(5013):     at dalvik.system.NativeStart.main(Native Method)

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cameratest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="sate2012.avatar.android.UploadMedia"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="sate2012.avatar.android.MailSenderActivity"
            android:label="Upload Data Point" >
            <intent-filter>
                <action android:name="android.intent.action.MAILSENDERACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="sate2012.avatar.android.GMailSender"
            android:label="@string/title_mail_sender_activity"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="sate2012.avatar.android.GMAILSENDER" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="sate2012.avatar.android.UploadData"
            android:label="UploadData" >
            <intent-filter>
                <action android:name="sate2012.avatar.android.UPLOADDATA" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".UploadMedia"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.UPLOAD_MEDIA" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Photographer"
            android:label="photoclass" >
            <intent-filter>
                <action android:name="android.intent.action.PHOTOGRAPHER" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".UploadFTP"
            android:label="photoclass" >
            <intent-filter>
                <action android:name="android.intent.action.UPLOAD_FTP" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

FTP课程

package com.cameratest;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.net.InetAddress;
import org.apache.commons.net.ftp.FTPClient;


public class UploadFTP extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static String FTPUpload(String filepath, String extension, Context thisContext) {
        FTPClient ftpClient = new FTPClient();
        long time = (System.currentTimeMillis());
        String filename = "T" + time;
        try {
            ftpClient.connect(InetAddress.getByName("24.123.68.146"));
            ftpClient.login("opensim", "widdlyscuds");
            ftpClient.changeWorkingDirectory("../../var/www/avatar/Uploaded");
            if (ftpClient.getReplyString().contains("250")) {
                Handler progressHandler = new Handler();
                ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
                BufferedInputStream buffIn = null;
                buffIn = new BufferedInputStream(new FileInputStream(filepath));
                ftpClient.enterLocalPassiveMode();
                ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler);
                ftpClient.storeFile(filename + extension, progressInput);
                buffIn.close();
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException e) {
        }
        return filename + extension;
    }
}

上传媒体:调用ftp class

package com.cameratest;


import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

/**
 * The Upload Menu Allows the user to select different media types to upload to
 * the server
 */
public class UploadMedia extends Activity implements OnClickListener {
    private File sd;
    private File storageFolder;
    private File mediaFolder;
    private ImageButton pictureB;
    private String dataType;
    private String media_filepath;
    private String media_filename;
    private String media_extension;
    private static String image_filepath;
    public static Context thisContext;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        thisContext = getApplicationContext();
        setContentView(R.layout.upload_menu);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        createStorageDirectory();
        pictureB = (ImageButton) findViewById(R.id.cameraButton);
        pictureB.setOnClickListener(this);

    }

    /**
     * Responds to whatever button is pressed
     * 
     * @param View
     *            v - the button clicked
     */
    public void onClick(View v) {
        Intent i;
        switch (v.getId()) {
        case (R.id.cameraButton):
            dataType = getResources().getString(R.string.type_picture);
            i = new Intent(UploadMedia.this, Photographer.class);
            startActivityForResult(i, Globals.CAMERA_REQUEST);
            break;
        }
    }

    public void onBackPressed() {
        finish();
    }

    /**
     * Called when the individual activities (picture, video, audio) finish.
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {

            if (requestCode == Globals.CAMERA_REQUEST) {
                media_filepath = getImage_filepath();
                media_extension = "_P.png";
            }
            media_filename = UploadFTP.FTPUpload(media_filepath,
                    media_extension, thisContext);
            Intent MailIntent = new Intent(getApplicationContext(),
                    MailSenderActivity.class);
            MailIntent.putExtra("Type", dataType);
            MailIntent.putExtra("Filename", media_filename);
            startActivity(MailIntent);
            finish();
        }
    }

    public static void setImage_filepath(String fp) {
        image_filepath = fp;
    }

    public String getImage_filepath() {
        return image_filepath;
    }

    @Override
    public void onDestroy() {
        finish();
        super.onDestroy();
    }

    public void createStorageDirectory() {
        sd = Environment.getExternalStorageDirectory();
        storageFolder = new File(sd, Globals.STORAGE_DIRECTORY);
        if (sd.canWrite()) {
            if (!storageFolder.exists())
                storageFolder.mkdir();
            mediaFolder = new File(sd, Globals.STORAGE_DIRECTORY
                    + Globals.MEDIA_DIRECTORY);
            if (!mediaFolder.exists())
                mediaFolder.mkdir();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

在应用程序{lib}文件夹中添加commons-net-3.1.jar文件. click here

答案 1 :(得分:0)

如果由于jar文件导致此错误,此解决方案将解决您的问题。看看这个ClassDefNotFound error in android