0
我想通過我的應用程序從FTP下載文件。但我的敬酒都沒有表現出來。我如何檢查我是否已連接並且文件是否已下載?如何檢查我的應用程序是否正在從FTP下載文件
代碼:
package no.kraftpriser.oversikt;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.SocketException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTPClient;
public class KraftpriserActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FTPClient ftp;
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
int duration2 = Toast.LENGTH_LONG;
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
try {
int reply;
String server = "62.97.235.213";
int port = 21;
ftp.connect(server, port);
ftp.login("anonymous","nobody");
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory("/Fastpris");
InputStream inStream = ftp.retrieveFileStream("2011.txt");
CharSequence tilkoblet = "Connected!";
Toast toastTilkoblet = Toast.makeText(context, tilkoblet, duration2);
toastTilkoblet.show();
reply = ftp.getReplyCode();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
if (ftp.isConnected()) {
try {
ftp.disconnect();
}
catch (IOException f) {
//Do nothing
}
}
CharSequence ikkeTikoblet = "Kunne ikke koble til server";
Toast toastIkkeTilkoblet = Toast.makeText(context, ikkeTikoblet, duration);
toastIkkeTilkoblet.show();
e.printStackTrace();
}
}
}