我对ftp上传有一个奇怪的(对我而言)问题。我正在尝试将zip文件上传到ftp服务器。它的工作正常,除非互联网连接很慢(我们用56kbps测试)。如果我在100%传输后通过FTP上传文件,则需要一些时间才能完成(约2分钟到20分钟,具体取决于文件大小)。
如果您看到最后两个日志项,则需要2分钟才能触发回调,尽管100%的上传已完成。我不确定为什么会触发回调。你能否对此有所了解?
07-02 10:34:38.112:D / FTP(1646):transferred26661time 2014-07-02 10:34:38
07-02 10:42:15.462:D / FTP(1646):完成2014-07-02 10:36:27
提前致谢。
查看我的代码
package com.androidexample.ftpdemo;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import java.io.File;
import java.util.Date;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.kpbird.ftpdemo.R;
public class FtpUpload extends Activity implements OnClickListener {
/********* work only for Dedicated IP ***********/
static final String FTP_HOST = "";
/********* FTP USERNAME ***********/
static final String FTP_USER = "";
/********* FTP PASSWORD ***********/
static final String FTP_PASS = "";
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) {
// Upload sdcard file
File f = new File("/sdcard/test.zip");
uploadFile(f);
}
public void uploadFile(File fileName) {
if (Build.VERSION.SDK_INT >= 10) {
ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);
}
FTPClient client = new FTPClient();
try {
client.connect(FTP_HOST);
client.login(FTP_USER, FTP_PASS);
client.setType(FTPClient.TYPE_BINARY);
client.upload(fileName, new MyTransferListener());
} catch (Exception e) {
btn.setVisibility(View.VISIBLE);
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() {
// Transfer started
Log.d("FTP", " started");
}
public void transferred(int length) {
Date d = new Date();
CharSequence s = DateFormat.format("yyyy-MM-dd hh:mm:ss",
d.getTime());
Log.d("FTP", " transferred" + length + "time" + s);
}
public void completed() {
// Transfer completed
Date d = new Date();
CharSequence s = DateFormat.format("yyyy-MM-dd hh:mm:ss",
d.getTime());
Log.d("FTP", " completed " + s);
}
public void aborted() {
// Transfer aborted
Date d = new Date();
CharSequence s = DateFormat.format("yyyy-MM-dd hh:mm:ss",
d.getTime());
Log.d("FTP", " aborted " + s);
}
public void failed() {
// Transfer failed
Date d = new Date();
CharSequence s = DateFormat.format("yyyy-MM-dd hh:mm:ss",
d.getTime());
Log.d("FTP", " failed " + s);
}
}
}
请看下面给出的日志,完成后需要2分钟的延迟。
07-02 10:27:48.602: D/FTP(1646): started
07-02 10:27:48.637: D/FTP(1646): transferred65536time2014-07-02 10:27:48
07-02 10:27:48.672: D/FTP(1646): transferred65536time2014-07-02 10:27:48
07-02 10:27:48.712: D/FTP(1646): transferred65536time2014-07-02 10:27:48
07-02 10:27:48.747: D/FTP(1646): transferred65536time2014-07-02 10:27:48
07-02 10:27:48.797: D/FTP(1646): transferred65536time2014-07-02 10:27:48
07-02 10:27:48.822: D/FTP(1646): transferred65536time2014-07-02 10:27:48
07-02 10:27:48.842: D/FTP(1646): transferred65536time2014-07-02 10:27:48
07-02 10:27:48.867: D/FTP(1646): transferred65536time2014-07-02 10:27:48
07-02 10:27:48.892: D/FTP(1646): transferred65536time2014-07-02 10:27:48
07-02 10:27:48.927: D/FTP(1646): transferred65536time2014-07-02 10:27:48
07-02 10:27:48.992: D/dalvikvm(1646): GC_FOR_ALLOC freed 216K, 10% free 12224K/13500K, paused 28ms, total 33ms
07-02 10:27:48.997: D/FTP(1646): transferred65536time2014-07-02 10:27:48
07-02 10:27:49.072: D/FTP(1646): transferred65536time2014-07-02 10:27:49
07-02 10:28:29.047: D/FTP(1646): transferred65536time2014-07-02 10:28:29
07-02 10:28:29.112: D/FTP(1646): transferred65536time2014-07-02 10:28:29
07-02 10:28:29.137: D/FTP(1646): transferred65536time2014-07-02 10:28:29
07-02 10:28:29.162: D/FTP(1646): transferred65536time2014-07-02 10:28:29
07-02 10:29:09.992: D/FTP(1646): transferred65536time2014-07-02 10:29:09
07-02 10:29:10.057: D/FTP(1646): transferred65536time2014-07-02 10:29:10
07-02 10:29:10.112: D/FTP(1646): transferred65536time2014-07-02 10:29:10
07-02 10:29:10.172: D/FTP(1646): transferred65536time2014-07-02 10:29:10
07-02 10:30:35.052: D/FTP(1646): transferred65536time2014-07-02 10:30:35
07-02 10:30:35.147: D/FTP(1646): transferred65536time2014-07-02 10:30:35
07-02 10:30:35.207: D/FTP(1646): transferred65536time2014-07-02 10:30:35
07-02 10:30:35.252: D/FTP(1646): transferred65536time2014-07-02 10:30:35
07-02 10:31:05.617: D/FTP(1646): transferred65536time2014-07-02 10:31:05
07-02 10:31:05.682: D/FTP(1646): transferred65536time2014-07-02 10:31:05
07-02 10:31:05.742: D/FTP(1646): transferred65536time2014-07-02 10:31:05
07-02 10:31:05.792: D/FTP(1646): transferred65536time2014-07-02 10:31:05
07-02 10:31:05.822: D/FTP(1646): transferred65536time2014-07-02 10:31:05
07-02 10:31:05.857: D/FTP(1646): transferred65536time2014-07-02 10:31:05
07-02 10:31:05.887: D/FTP(1646): transferred65536time2014-07-02 10:31:05
07-02 10:31:47.082: D/FTP(1646): transferred65536time2014-07-02 10:31:47
07-02 10:31:47.157: D/FTP(1646): transferred65536time2014-07-02 10:31:47
07-02 10:31:47.217: D/FTP(1646): transferred65536time2014-07-02 10:31:47
07-02 10:31:47.267: D/FTP(1646): transferred65536time2014-07-02 10:31:47
07-02 10:32:27.737: D/FTP(1646): transferred65536time2014-07-02 10:32:27
07-02 10:32:27.802: D/FTP(1646): transferred65536time2014-07-02 10:32:27
07-02 10:32:27.862: D/FTP(1646): transferred65536time2014-07-02 10:32:27
07-02 10:32:27.917: D/FTP(1646): transferred65536time2014-07-02 10:32:27
07-02 10:32:27.967: D/FTP(1646): transferred65536time2014-07-02 10:32:27
07-02 10:33:39.422: D/FTP(1646): transferred65536time2014-07-02 10:33:39
07-02 10:33:39.482: D/FTP(1646): transferred65536time2014-07-02 10:33:39
07-02 10:33:39.542: D/FTP(1646): transferred65536time2014-07-02 10:33:39
07-02 10:33:39.587: D/FTP(1646): transferred65536time2014-07-02 10:33:39
07-02 10:33:49.692: D/FTP(1646): transferred65536time2014-07-02 10:33:49
07-02 10:33:49.792: D/FTP(1646): transferred65536time2014-07-02 10:33:49
07-02 10:33:49.842: D/FTP(1646): transferred65536time2014-07-02 10:33:49
07-02 10:33:49.887: D/FTP(1646): transferred65536time2014-07-02 10:33:49
07-02 10:34:37.902: D/FTP(1646): transferred65536time2014-07-02 10:34:37
07-02 10:34:37.967: D/FTP(1646): transferred65536time2014-07-02 10:34:37
07-02 10:34:38.017: D/FTP(1646): transferred65536time2014-07-02 10:34:38
07-02 10:34:38.067: D/FTP(1646): transferred65536time2014-07-02 10:34:38
07-02 10:34:38.112: D/FTP(1646): transferred26661time2014-07-02 10:34:38
07-02 10:42:15.462: D/FTP(1646): completed 2014-07-02 10:36:27