我正在尝试使用自己的应用从我的Android智能手机通过FTP上传文件。我有一个完成的代码,但它不起作用。相反,它会抛出Logact中的异常。我希望你能帮助我。
二手图书馆:https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTP.html
我的代码:
package com.florian.ftpupload;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import com.florian.ftpupload.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class UploadActivity extends Activity{
public static final String TAG = "Contacts";
public String host = "<serverip>";
public String username = "<username>";
public String password = "<password>";
public String database = "test.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
Thread t = new Thread(new Runnable(){
@Override
public void run(){
try {
DatabaseUpload();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
Log.i(TAG, "thread started");
}
protected void DatabaseUpload() throws SocketException, IOException {
FTPClient ftp = new FTPClient();
ftp.connect(host);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)){
Toast.makeText(getApplicationContext(), "FTP server refused connection.", Toast.LENGTH_SHORT).show();
}
ftp.login(username, password);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
InputStream fis = new FileInputStream(database);
ftp.storeFile("test.txt", fis);
fis.close();
ftp.logout();
ftp.disconnect();
}
}
我的LogCat错误:
D/Genymotion( 56): Received Ping
I/ActivityManager( 366): START u0 {act=android.intent.action.MAIN cat= [android.
intent.category.LAUNCHER] flg=0x10200000 cmp=com.florian.ftpupload/.UploadActivi
ty} from pid 586
D/dalvikvm( 366): GC_CONCURRENT freed 805K, 16% free 7450K/8816K, paused 1ms+2m
s, total 20ms
I/ActivityManager( 366): Start proc com.florian.ftpupload for activity com.flor
ian.ftpupload/.UploadActivity: pid=1421 uid=10051 gids={50051, 1028}
I/qtaguid ( 366): Failed write_ctrl(s 1 10051) res=-1 errno=1
W/NetworkManagementSocketTagger( 366): setKernelCountSet(10051, 1) failed with
errno -1
E/dalvikvm( 1421): Could not find class 'org.apache.commons.net.ftp.FTPClient',
referenced from method com.florian.ftpupload.UploadActivity.DatabaseUpload
W/dalvikvm( 1421): VFY: unable to resolve new-instance 1557 (Lorg/apache/commons
/net/ftp/FTPClient;) in Lcom/florian/ftpupload/UploadActivity;
D/dalvikvm( 1421): VFY: replacing opcode 0x22 at 0x0000
D/dalvikvm( 1421): DexOpt: unable to opt direct call 0x2a97 at 0x02 in Lcom/flor
ian/ftpupload/UploadActivity;.DatabaseUpload
I/Contacts( 1421): thread started
W/dalvikvm( 1421): threadid=10: thread exiting with uncaught exception (group=0x
a614c908)
E/AndroidRuntime( 1421): FATAL EXCEPTION: Thread-122
E/AndroidRuntime( 1421): java.lang.NoClassDefFoundError: org.apache.commons.net.
ftp.FTPClient
E/AndroidRuntime( 1421): at com.florian.ftpupload.UploadActivity.Database
Upload(UploadActivity.java:55)
E/AndroidRuntime( 1421): at com.florian.ftpupload.UploadActivity$1.run(Up
loadActivity.java:40)
E/AndroidRuntime( 1421): at java.lang.Thread.run(Thread.java:856)
W/ActivityManager( 366): Force finishing activity com.florian.ftpupload/.Uplo
adActivity
D/libEGL ( 1421): loaded /system/lib/egl/libEGL_genymotion.so
D/ ( 1421): HostConnection::get() New Host Connection established 0xb7b1a
f98, tid 1421
D/libEGL ( 1421): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
D/libEGL ( 1421): loaded /system/lib/egl/libGLESv2_genymotion.so
I/ActivityManager( 366): START u0 {act=android.intent.action.MAIN cat= [android.
intent.category.LAUNCHER] flg=0x10200000 cmp=com.florian.ftpupload/.UploadActivi
ty} from pid 586
W/EGL_genymotion( 1421): eglSurfaceAttrib not implemented
D/OpenGLRenderer( 1421): Enabling debug mode 0
I/Contacts( 1421): thread started
W/dalvikvm( 1421): threadid=11: thread exiting with uncaught exception (group=0x
a614c908)
I/Process ( 1421): Sending signal. PID: 1421 SIG: 9
I/qtaguid ( 366): Failed write_ctrl(s 0 10051) res=-1 errno=1
W/NetworkManagementSocketTagger( 366): setKernelCountSet(10051, 0) failed with
errno -1
I/ActivityManager( 366): Process com.florian.ftpupload (pid 1421) has died.
I/WindowState( 366): WIN DEATH: Window{533f06e4 u0 com.florian.ftpupload/com.fl
orian.ftpupload.UploadActivity}
W/ActivityManager( 366): Force removing ActivityRecord{5348c488 u0 com.florian.
ftpupload/.UploadActivity}: app died, no saved state
I/WindowState( 366): WIN DEATH: Window{5343b134 u0 com.florian.ftpupload/com.fl
orian.ftpupload.UploadActivity}
W/EGL_genymotion( 586): eglSurfaceAttrib not implemented
W/InputMethodManagerService( 366): Window already focused, ignoring focus gain
of: com.android.internal.view.IInputMethodClient$Stub$Proxy@53330148 attribute=n
ull, token = android.os.BinderProxy@533c6a54
D/Genymotion( 56): Received Ping
我希望你理解我的问题;)
〜弗洛里安