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();
}
}
謝謝!你的詳細答案真的幫了我很大的忙,我想我會用第一種方式去處理100-200M文件,並且失去連接可能會非常昂貴......因爲即使我需要重新出演,問題出在FTP服務器上。 – Noam 2012-07-23 19:30:17
當您使用2個打開的流時,只能對小數據量(buf,buffer)進行重試。下載連接將長時間打開(您將等待上傳),因此您的連接可能會被拒絕。如果您無法從偏移量繼續下載,則必須重新開始整個過程。 – alexsmail 2012-07-24 08:22:37