找尋可在Android上使用的FTP client library,用於傳輸檔案到FTP server中
- edtFTPJ
- License: LGPL
- 付費版具Security
- FTPClient
- License: ASF
決定使用FTPClient
- 將上面二種library參考到Android程式當中,皆會發生NoClassDefFoundError
- 解法:將library直接匯入到程式的libs資料夾中
- 由於上面的原因,故採用較小容量的FTPClient
Usage
public class MainActivity extends Activity { private static final String TAG = "MainActivity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int port = 21; String ftpAddress = "127.0.0.1"; String username = "potato"; String password = "123456"; String root = "/sdcard"; String remoteRoot = "/test"; FileInputStream inputStream = null; FTPClient client = null; try { client = new FTPClient(); client.setConnectTimeout(1000); client.connect(ftpAddress, port); client.login(username, password); client.setFileType(FTP.BINARY_FILE_TYPE); client.enterLocalPassiveMode(); for(int i = 0;i<3;i++) { try { inputStream = new FileInputStream(root + "/" + i + ".jpg"); } catch (FileNotFoundException e) { e.printStackTrace(); } client.appendFile(remotRoot + "/" + i + ".jpg", inputStream); inputStream.close(); } client.disconnect(); } catch (IOException ioe) { ioe.printStackTrace(); } } }