我知道如何使用ftp4j但是我想用同样的方法下载从FTP文件上传文件,我希望看到:下载文件从FTP使用ftp4j
- 服务器目录(httpdocs资料/文件夹/到/下载/从/ file.extension)
- 本地目录(/sdcard/folder/to/download/to/file.extension)
下面是我用上传的代码:
file_destination = new File(filDir +"/"+fileName); // where fileDir + filename = sdcard/document/file.txt
private void uploadImage() {
// TODO Auto-generated method stub
file_destination_string = file_destination.toString();
upload_file = new File(file_destination_string);
uploadFile(upload_file);
}
private void uploadFile(File f) {
// TODO Auto-generated method stub
client = new FTPClient();
try {
client.connect(FTP_HOST,21); //where HOST is ip address of server
client.login(FTP_USER, FTP_PASS); // FTP user/password
client.setType(FTPClient.TYPE_BINARY);
client.changeDirectory(FTP_DIR); //where FTP_DIR = /httpdocs/folder/
client.upload(f, new MyTransferListener());
} catch (Exception e) {
Toast.makeText(getBaseContext(), "FTP Failed: "+e, Toast.LENGTH_SHORT).show();
System.out.println("e1..."+e);
e.printStackTrace();
try {
client.disconnect(true);
Log.e("MYAPP", "exception", e);
System.out.println("e2...");
} catch (Exception e2) {
System.out.println("e3...");
e2.printStackTrace();
}
}
}
public class MyTransferListener implements FTPDataTransferListener {
public void started() {
Toast.makeText(getBaseContext(), " Upload Started ...", Toast.LENGTH_SHORT).show();
}
public void transferred(int length) {
Toast.makeText(getBaseContext(), " transferred ..." + length, Toast.LENGTH_SHORT).show();
}
public void completed() {
Toast.makeText(getBaseContext(), " completed ...", Toast.LENGTH_SHORT).show();
}
public void aborted() {
Toast.makeText(getBaseContext()," transfer aborted, please try again...", Toast.LENGTH_SHORT).show();
}
public void failed() {
Toast.makeText(getBaseContext()," failed..", Toast.LENGTH_SHORT).show();
}
}
如何下载文件?
你可以使用'client.download(“myfile.txt的”,新的Java。 io.File(“d:/myfile.txt”));'这会将“myfile.txt”下载到“D:\ myfile.txt”。 – Bobby
我试图上传和下载到安卓设备上,而不是PC – TwoStarII
容易,只需用“/sdcard/myfile.txt”替换“D:\ myfile.txt” – Bobby