我正在从 FTP 服务器下载文件,但我得到一个NullPointerException
. 我究竟做错了什么?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ftpConnect("domain","username", "password", portnum);
try
{
String s = Environment.getExternalStorageDirectory().toString();
File f = new File(s+"/???/item");
f.mkdirs();
ftpDownload("ftp://user@portalftp.?????.com/rooms.txt", f);
}
catch(Exception e)
{
Toast toast = Toast.makeText(getApplicationContext(), "Download error: "+e.getMessage(), Toast.LENGTH_SHORT);
toast.show();
}
ftpDisconnect();
}
public boolean ftpDownload(String srcFilePath, File desFilePath)
{
boolean status = false;
try {
FileOutputStream desFileStream = new FileOutputStream(desFilePath);
status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
desFileStream.close();
Toast toast = Toast.makeText(getApplicationContext(), "Downloaded", Toast.LENGTH_SHORT);
toast.show();
return status;
} catch (Exception e) {
Toast toast = Toast.makeText(getApplicationContext(), "Download error"+e.getMessage(), Toast.LENGTH_SHORT);
toast.show();
}
return status;
}
public boolean ftpConnect(String host, String username,
String password, int port)
{
try{
mFTPClient = new FTPClient();
// connecting to the host
mFTPClient.connect(host, port);
// now check the reply code, if positive mean connection success
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
// login using username & password
boolean status = mFTPClient.login(username, password);
/* Set File Transfer Mode
*
* To avoid corruption issue you must specified a correct
* transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
* EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE
* for transferring text, image, and compressed files.
*/
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
mFTPClient.enterLocalPassiveMode();
return status;
}
} catch(Exception e) {
Log.e("ERROR", e.getMessage());
}
return false;
}
}