【发布时间】:2014-12-24 00:42:17
【问题描述】:
我有一个简单的 FTP 上传应用程序,通常可以正常工作。
有时在上传过程中我可能会失去 INTERNET(在 wifi 点或其他东西之间移动)。
当发生这种情况时,Upload 会继续尝试上传但没有任何进展,不会超时或导致任何错误,并且由于服务永远不会完成,除非我强制停止应用程序,否则我无法重新启动它。
我如何设置超时选项和任何其他有用的工具来阻止我的应用程序这样做?
我如何实施以下或任何其他措施以及产生什么影响:
client.setConnectTimeout(60); //Are this milliseconds, seconds or what?
client.setDefaultTimeout(300); //Are this milliseconds, seconds or what?
client.setControlKeepAliveTimeout(120); //Are this milliseconds, seconds or what?
这是我的代码的精简版供您查看。
myFTP.class
public class myFTP{
String ip;
String user;
String pass;
FTPClient client = new FTPClient();
public myFTP() {
ip = "someip";
user = "´someuser";
pass = "somepass";
client = new FTPClient();
}
public boolean connect() {
//Define vars
try{
//Connect and login
client.setConnectTimeout(60);
client.setDefaultTimeout(300);
client.setControlKeepAliveTimeout(120);
client.connect(ip);
client.login(user,pass);
if(FTPReply.isPositiveCompletion(client.getReplyCode())){
client.setFileType(FTP.BINARY_FILE_TYPE);
}else{
Log.e ("Coneccion FTP", "Fallo");
}
} catch (UnknownHostException e) {
e.printStackTrace();
return false;
} catch (SocketException e){
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean upload (String fileName, File file, String dir){
//Cargo el archivo
try{
FileInputStream fis = new FileInputStream(file);
client.enterLocalPassiveMode();
Log.e("Subiendo", fileName);
boolean status = client.storeFile(dir+fileName,fis);
if (!status){
fis.close();
return false;
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
try {
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}
【问题讨论】:
标签: android timeout ftp-client