我知道如何使用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