我通过FileZilla将.db文件上传到我的FTP服务器。然后我写了一些代码,使用Apache ftp commons将文件下载到我的设备。在我下载并尝试查询后,它会一直显示错误。然后我发现下载文件的文件大小只是0.我不知道发生了什么,因为这是我第一次处理FTP问题。
下载代码如下所示。
public void run() {
//Download the database here
String networkName="abc.net";
//User name and password. CONFIDENTIAL!
String user="a1234";
String pw="123455";
String dir="/public_html/word_enhancement_db.db";
FTPClient client=new FTPClient();
try{
//Connect the server and log in.
client.connect(networkName);
if (client.login(user,pw))
Log.w("InternetThread","Logged in");
else Log.w("InternetThread","Cannot log in!");
//Set the location for the database.
String DB_PATH="/data/data/com.android.luftschlafer.wordenhancement/databases";
String DB_NAME="/word_enhancement.db";
Log.w("DB location",DB_PATH+DB_NAME);
Log.w("InternetThread", "Old DB not found");
File newDB = new File(DB_PATH, DB_NAME);
if (newDB.mkdirs()) {
Log.w("InternetThread", "New DB dir has been produced");
}
Log.w("InternetThread", "New DB has been built");
FileOutputStream fos = new FileOutputStream(DB_PATH + DB_NAME);
client.setBufferSize(1024);
client.setFileType(FTP.BINARY_FILE_TYPE);
client.retrieveFile(dir, fos);
fos.close();
Log.w("InternetThread", "New database has been written");
Log.w("InternetThread"," New database file size: "+Long.toString(newDB.length()));
}
} catch (Exception e){
e.printStackTrace();
Log.e("InternetThread","Client connection error");
} finally {
try{
client.disconnect();
Log.w("InternetThread","Logged out");
} catch (Exception e){
e.printStackTrace();
Log.w("InternetThread","Cannot disconnect");
}
}