Reputation:
Hi friends
Iam doing an android application in that i want to select one video from my sd card and want to upload it to ftp server.While iam running the project in the emulator i got 04-28 18:22:00.810: ERROR/AndroidRuntime(1053): java.lang.NoClassDefFoundError: org.apache.commons.net.ftp.FTPClient
.Is there any way to solve this problem.I included two jar files org.apache.commons.net.jar and commons-net-1.4.1.jar by right click my project->properties->java build path->add external jars.Even i added the jar files,the project did'nt gets runned.Please help me if anybody knows...
Iam using the below code:
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 "";
}
});
}
}
I got the output using simple java program with code below.But i want to do it in 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();
}
}
}
Upvotes: 1
Views: 1909
Reputation: 1481
You could try to solve the 0 bytes issue:
ftp.enterLocalPassiveMode();
Upvotes: 1