我刚刚成功使用 Apache Commons Net FTP 将一些文件上传到 FTP 服务器。当我按应有的方式使用所有内容时,它工作正常,我的意思是,文件上传正在工作,但是当我使用 FTP 帐户的错误密码时,显然文件不会上传到服务器,但是我也没有收到错误消息。换句话说:我不知道如何显示错误。我试图用吐司来显示
e.getMessage()
中的 Exception
但没有显示任何内容。有任何文档可以帮助我吗?或者我错过了你可以帮助我的事情吗?谢谢!
这是我的代码:
@Override
protected Void doInBackground(Void... voids) {
FTPClient ftpClient=new FTPClient();
try {
ftpClient.connect(fserver);
ftpClient.login(fusername,fpassword);
ftpClient.enterLocalPassiveMode();
dirArch = new ArrayList<>();
for (int k=0;k<adapter.getSelected().size();k++){
dirArch.add(adapter.getSelected().get(k).getArch());
}
for (String archTXT : dirArch){
File fileX =new File(stringFolder,archTXT);
InputStream inputStream=new FileInputStream(fileX);
ftpClient.storeFile(archTXT,inputStream);
inputStream.close();
pbPorc=pbPorc+pbSum;
pb1.setProgress(pbPorc);
pb2.setProgress(pbPorc);
}
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(ExportTracks.this,e.getMessage(),Toast.LENGTH_LONG).show();
}
return null;
}
你的错误处理是错误的。
FTPClient.login
不会抛出异常。它返回一个false
。你不需要对此进行测试。您的代码继续,尝试使用未经授权的会话,所有这些显然也会失败。
FTPClient.storeFile
也是如此,它也只返回false
,你的代码基本上不会注意到任何错误。