我正在尝试访问 Android 中 FTP 服务器上的文件。
出于某种原因,每当我导入任何外部 jar(例如 commons-net 或 ftp4j)时,我的应用程序都会在运行时崩溃。我已经通过注释掉所有引用 jar 的行来验证这一点,并且应用程序可以正常运行。
我想访问 FTP 服务器上的文本文件中的数据并将其打印到 TextView。我整天都被困在这上面。我该如何解决?包括在清单中的权限是互联网和外部存储。
package com.testing.pack;
import java.io.BufferedInputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.http.client.HttpClient;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class TestingActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
TextView tv;
Button go;
HttpClient client;
FTPClient ftp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tvTry);
Typeface face = Typeface.createFromAsset(getAssets(),
"Roboto-LightItalic.ttf");
tv.setTypeface(face);
go = (Button) findViewById(R.id.bGO);
go.setOnClickListener(this);
try {
//Attempt using commons-net
FTPClient ftpClient = new FTPClient();
ftpClient.connect("ftp://........./");
ftpClient.login(".......", ".....");
ftpClient.changeWorkingDirectory("......");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
BufferedInputStream buffIn=null;
buffIn=new BufferedInputStream(new FileInputStream(file));
ftpClient.enterLocalPassiveMode();
ftpClient.storeFile("test.txt", buffIn);
buffIn.close();
ftpClient.logout();
ftpClient.disconnect();
//Attempt using ftp4j
//FTPClient ftp = new FTPClient();
//ftp.connect("........");
//ftp.login(".......", ".....");
//ftp.changeDirectoryUp();
//ftp.changeDirectory(".....");
//ftp.changeDirectory("public_ftp");
//ftp.download("test.txt", new java.io.File("....."));
} catch (Exception e) {
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
getData ex = new getData();
try {
tv.setText(ex.getInternetData());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}