我正在构建一个简单的应用程序,它在FTP服务器上传文件。 - 我在参考库中使用ftp4j-1.6.jar。
代码:
FTPDemo->的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kpbird.ftpdemo"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"> </uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="com.androidexample.ftpdemo.FtpUpload"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="Upload"android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Button>
</LinearLayout>
FTPDemo.java
package com.androidexample.ftpdemo;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import java.io.File;
import com.kpbird.ftpdemo.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class FtpUpload extends Activity implements OnClickListener {
/********* work only for Dedicated IP ***********/
static final String FTP_HOST= "lumium.com";
/********* FTP USERNAME ***********/
static final String FTP_USER = "xxxxx";
/********* FTP PASSWORD ***********/
static final String FTP_PASS ="xxxxx";
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
}
public void onClick(View v) {
/********** Pick file from sdcard *******/
File f = new File("/mnt/sdcard/phoneData.txt");
// Upload sdcard file
uploadFile(f);
}
public void uploadFile(File fileName){
FTPClient client = new FTPClient();
try {
client.connect(FTP_HOST,21);// here i get error: java.
client.login(FTP_USER,FTP_PASS);
client.setType(FTPClient.TYPE_BINARY);
client.changeDirectory("/upload/");
client.upload(fileName, new MyTransferListener());
} catch (Exception e) {
e.printStackTrace();
try {
client.disconnect(true);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
/******* Used to file upload and show progress **********/
public class MyTransferListener implements FTPDataTransferListener {
public void started() {
btn.setVisibility(View.GONE);
// Transfer started
Toast.makeText(getBaseContext(), " Upload Started ...", Toast.LENGTH_SHORT).show();
//System.out.println(" Upload Started ...");
}
public void transferred(int length) {
// Yet other length bytes has been transferred since the last time this
// method was called
Toast.makeText(getBaseContext(), " transferred ..." + length, Toast.LENGTH_SHORT).show();
//System.out.println(" transferred ..." + length);
}
public void completed() {
btn.setVisibility(View.VISIBLE);
// Transfer completed
Toast.makeText(getBaseContext(), " completed ...", Toast.LENGTH_SHORT).show();
//System.out.println(" completed ..." );
}
public void aborted() {
btn.setVisibility(View.VISIBLE);
// Transfer aborted
Toast.makeText(getBaseContext()," transfer aborted , please try again...", Toast.LENGTH_SHORT).show();
//System.out.println(" aborted ..." );
}
public void failed() {
btn.setVisibility(View.VISIBLE);
// Transfer failed
System.out.println(" failed ..." );
}
}
}
logcat的:
enter code here
04-06 13:35:28.625: W/System.err(1252): java.net.UnknownHostException: lumium.com
04-06 13:35:28.685: W/System.err(1252): at
java.net.InetAddress.lookupHostByName(InetAddress.java:506)
04-06 13:35:28.755: W/System.err(1252): at
java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
04-06 13:35:28.795: W/System.err(1252): at
java.net.InetAddress.getAllByName(InetAddress.java:256)
04-06 13:35:28.839: W/System.err(1252): at
java.net.Socket.tryAllAddresses(Socket.java:130)
04-06 13:35:28.885: W/System.err(1252): at java.net.Socket.<init>(Socket.java:209)
04-06 13:35:28.954: W/System.err(1252): at java.net.Socket.<init>(Socket.java:176)
04-06 13:35:28.994: W/System.err(1252): at
it.sauronsoftware.ftp4j.connectors.DirectConnector.connectForCommunicationChannel
(DirectConnector.java:37)
04-06 13:35:29.015: W/System.err(1252): at
it.sauronsoftware.ftp4j.FTPClient.connect(FTPClient.java:1031)
04-06 13:35:29.065: W/System.err(1252): at
com.androidexample.ftpdemo.FtpUpload.uploadFile(FtpUpload.java:56)
04-06 13:35:29.127: W/System.err(1252): at
com.androidexample.ftpdemo.FtpUpload.onClick(FtpUpload.java:45)
04-06 13:35:29.175: W/System.err(1252): at
android.view.View.performClick(View.java:2485)
04-06 13:35:29.225: W/System.err(1252): at
android.view.View$PerformClick.run(View.java:9080)
04-06 13:35:29.285: W/System.err(1252): at
android.os.Handler.handleCallback(Handler.java:587)
04-06 13:35:29.337: W/System.err(1252): at
android.os.Handler.dispatchMessage(Handler.java:92)
04-06 13:35:29.404: W/System.err(1252): at android.os.Looper.loop(Looper.java:123)
04-06 13:35:29.475: W/System.err(1252): at
android.app.ActivityThread.main(ActivityThread.java:3683)
04-06 13:35:29.515: W/System.err(1252): at
java.lang.reflect.Method.invokeNative(Native Method)
04-06 13:35:29.555: W/System.err(1252): at
java.lang.reflect.Method.invoke(Method.java:507)
04-06 13:35:29.624: W/System.err(1252): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-06 13:35:29.665: W/System.err(1252): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-06 13:35:29.694: W/System.err(1252): at dalvik.system.NativeStart.main(Native
Method)
04-06 13:35:44.795: W/System.err(1252): java.lang.IllegalStateException: Client not
connected
04-06 13:35:44.866: W/System.err(1252): at
it.sauronsoftware.ftp4j.FTPClient.disconnect(FTPClient.java:1107)
04-06 13:35:44.894: W/System.err(1252): at
com.androidexample.ftpdemo.FtpUpload.uploadFile(FtpUpload.java:66)
04-06 13:35:44.904: W/System.err(1252): at
com.androidexample.ftpdemo.FtpUpload.onClick(FtpUpload.java:45)
04-06 13:35:44.925: W/System.err(1252): at
android.view.View.performClick(View.java:2485)
04-06 13:35:44.945: W/System.err(1252): at
android.view.View$PerformClick.run(View.java:9080)
04-06 13:35:44.965: W/System.err(1252): at
android.os.Handler.handleCallback(Handler.java:587)
04-06 13:35:45.005: W/System.err(1252): at
android.os.Handler.dispatchMessage(Handler.java:92)
04-06 13:35:45.036: W/System.err(1252): at android.os.Looper.loop(Looper.java:123)
04-06 13:35:45.064: W/System.err(1252): at
android.app.ActivityThread.main(ActivityThread.java:3683)
04-06 13:35:45.095: W/System.err(1252): at
java.lang.reflect.Method.invokeNative(Native Method)
04-06 13:35:45.125: W/System.err(1252): at
java.lang.reflect.Method.invoke(Method.java:507)
04-06 13:35:45.155: W/System.err(1252): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-06 13:35:45.187: W/System.err(1252): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-06 13:35:45.206: W/System.err(1252): at dalvik.system.NativeStart.main(Native
Method)
04-06 13:39:17.745: D/SntpClient(75): request time failed: java.net.SocketException:
Address family not supported by protocol
04-06 13:43:00.015: I/dalvikvm(75): Jit: resizing JitTable from 1024 to 2048
04-06 13:44:00.114: I/dalvikvm(156): Total arena pages for JIT: 11
04-06 13:44:17.774: D/SntpClient(75): request time failed: java.net.SocketException:
Address family not supported by protocol
04-06 13:49:17.809: D/SntpClient(75): request time failed: java.net.SocketException:
Address family not supported by protocol
04-06 13:54:17.829: D/SntpClient(75): request time failed: java.net.SocketException:
Address family not supported by protocol
04-06 13:59:17.859: D/SntpClient(75): request time failed: java.net.SocketException:
Address family not supported by protocol
04-06 14:04:16.786: W/ProcessStats(75): Skipping unknown process pid 3352
04-06 14:04:16.786: W/ProcessStats(75): Skipping unknown process pid 3353
04-06 14:04:17.865: D/SntpClient(75): request time failed: java.net.SocketException:
Address family not supported by protocol
04-06 14:09:17.949: D/SntpClient(75): request time failed: java.net.SocketException:
Address family not supported by protocol
04-06 14:14:17.974: D/SntpClient(75): request time failed: java.net.SocketException:
Address family not supported by protocol
04-06 14:14:55.121: D/dalvikvm(156): GC_CONCURRENT freed 301K, 52% free 2790K/5767K,
external 2158K/2674K, paused 124ms+215ms
04-06 14:19:17.995: D/SntpClient(75): request time failed: java.net.SocketException:
Address family not supported by protocol
04-06 14:22:05.270: W/ProcessStats(75): Skipping unknown process pid 4569
04-06 14:24:18.055: D/SntpClient(75): request time failed: java.net.SocketException:
Address family not supported by protocol
我该怎么办? 任何建议,链接 提前完成。
答案 0 :(得分:1)
请看下面的例子 我认为这是OnUI问题所以我在下面的代码。 我希望这对任何人都有帮助 让我知道你的结果 谢谢你的时间。
public class MyTransferListener implements FTPDataTransferListener {
public void started() {
runOnUiThread(new Runnable() {
public void run() {
//btn.setVisibility(View.GONE);
// Transfer started
Toast.makeText(getBaseContext(), " Upload Started ...", Toast.LENGTH_SHORT).show();
}
});
// System.out.println(" Upload Started ...");
}
public void transferred(final int length) {
// Yet other length bytes has been transferred since the last time
// this
// method was called
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getBaseContext(), " transferred ..." + length, Toast.LENGTH_SHORT).show();
}
});
// System.out.println(" transferred ..." + length);
}
public void completed() {
runOnUiThread(new Runnable() {
public void run() {
//btn.setVisibility(View.VISIBLE);
// Transfer completed
Toast.makeText(getBaseContext(), " completed ...", Toast.LENGTH_SHORT).show();
}
});
// System.out.println(" completed ..." );
}
public void aborted() {
runOnUiThread(new Runnable() {
public void run() {
//btn.setVisibility(View.VISIBLE);
// Transfer aborted
Toast.makeText(getBaseContext(), " transfer aborted , please try again...",
Toast.LENGTH_SHORT).show();
}
});
// System.out.println(" aborted ..." );
}
public void failed() {
runOnUiThread(new Runnable() {
public void run() {
//btn.setVisibility(View.VISIBLE);
// Transfer failed
System.out.println(" failed ...");
}
});
}
}