1.坦率地说,如果您使用的是开源,那么请利用外部库。
2. 不要重新发明轮子,您可以使用可信赖的库,所以请使用它。
我还将我用来上传和下载歌曲的程序附加到ftp server
使用apache's common lib
上传:
public void goforIt(){
FTPClient con = null;
try
{
con = new FTPClient();
con.connect("192.168.2.57");
if (con.login("Administrator", "KUjWbk"))
{
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
String data = "/sdcard/vivekm4a.m4a";
FileInputStream in = new FileInputStream(new File(data));
boolean result = con.storeFile("/vivekm4a.m4a", in);
in.close();
if (result) Log.v("upload result", "succeeded");
con.logout();
con.disconnect();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
下载:
public void goforIt(){
FTPClient con = null;
try
{
con = new FTPClient();
con.connect("192.168.2.57");
if (con.login("Administrator", "KUjWbk"))
{
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
String data = "/sdcard/vivekm4a.m4a";
OutputStream out = new FileOutputStream(new File(data));
boolean result = con.retrieveFile("vivekm4a.m4a", out);
out.close();
if (result) Log.v("download result", "succeeded");
con.logout();
con.disconnect();
}
}
catch (Exception e)
{
Log.v("download result","failed");
e.printStackTrace();
}
}