位于Apache的Project里,Commons下的net
一些示例,更多使用可訪問官方示例和API文檔
//初始化
FTPClient ftp = new FTPClient();
//連接
ftp.connect(ftpURL);
//判斷是否連接成功
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)){
//連接失敗
ftp.disconnect();
return;
}
//登錄
if(!ftp.login(username, password)){
//登錄失敗
ftp.logout();
return;
}
//上傳文件
ftp.storeFile(path_file_name, ioStream);
ioStream.close();
//訪問指定文件夾,路徑都是相對(duì)路徑
ftp.changeWorkingDirectory(oPath);
//瀏覽當(dāng)前文件夾文件
FTPFile[] files = ftp.listFiles();
for(FTPFile f : files){
if(f.isFile()){
//判斷是否是文件
}
//改變?cè)L問模式
ftp.enterLocalPassiveMode();
//下載文件到指定輸出流
OutputStream sb = new ByteArrayOutputStream();
ftp.retrieveFile(f.getName(), sb);
sb.close();
//讀取文件
InputStream is = ftp.retrieveFileStream(f.getName());
is.close();
ftp.completePendingCommand();//保證某些操作事務(wù)完成
}
}
//關(guān)閉連接
if (ftp.isConnected()) {
try {
ftp.logout();
ftp.disconnect();
} catch (IOException ioe) { }
}