Java FTPClient 遠端檔案上傳下載追加
阿新 • • 發佈:2018-12-04
注意事項:
- 匯入jar包:commons-net-3.6.jar(百度雲分享)
- 用於登陸FTP伺服器的賬戶對檔案操作目錄必須有讀寫許可權
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.junit.Test;
上傳檔案
說明:將本地 D:/test/ 目錄下的 uploadfile.txt 檔案上傳到伺服器 /home/ftp/log 目錄下,檔名為 log_20180913
public void uploadFile() { String ip = "xxx.xxx.xxx.xxx"; //伺服器IP地址 String userName = "yourUserName"; //用於登陸伺服器的使用者名稱 String passWord = "yourPassord"; //登陸密碼 String remoteDirectoryPath = "/home/ftp/log"; //遠端資料夾的絕對路徑 String localFilePath = "D:/test/uploadfile.txt"; //要上傳到伺服器的本地檔案的絕對路徑 String remoteFileName = "log_20180913"; //將本地檔案上傳到伺服器後文件的名字 FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(ip); boolean isLogin = ftpClient.login(userName, passWord); System.out.println("uploadFile 登陸成功? " + isLogin); ftpClient.changeWorkingDirectory(remoteDirectoryPath); InputStream is = new FileInputStream(new File(localFilePath)); boolean isStore = ftpClient.storeFile(remoteFileName, is); System.out.println("上傳成功? " + isStore); } catch (IOException e) { e.printStackTrace(); }finally { try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } }
下載檔案
說明:將伺服器 /home/ftp/log 路徑下的 log_20180913 檔案下載到本地 D:/test 目錄下,檔名為java.txt
public void downloadFile() { String ip = "xxx.xxx.xxx.xxx"; // 伺服器IP地址 String username = "yourUsername"; // 用於登陸伺服器的使用者名稱 String password = "yourPassword"; // 登陸密碼 String remoteDirectoryPath = "/home/ftp/log"; // 遠端資料夾的絕對路徑 String localFilePath = "D:/test/java.txt"; // 要上傳到伺服器的本地檔案的絕對路徑 String remoteFileName = "log_20180913"; // 將本地檔案上傳到伺服器後文件的名字 FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(ip); boolean isLogin = ftpClient.login(username, password); System.out.println("登陸成功? " + isLogin); ftpClient.changeWorkingDirectory(remoteDirectoryPath); FTPFile[] files = ftpClient.listFiles(); for (FTPFile file : files) { if (file.getName().equals(remoteFileName)) { OutputStream os = new FileOutputStream(new File(localFilePath)); ftpClient.retrieveFile(file.getName(), os); } } } catch (IOException e) { e.printStackTrace(); } finally { try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } }
向檔案追加內容
說明:向伺服器 /home/ftp/log 路徑下的 log_20180913 檔案追加內容:\nhello append content
public void appendFile() {
String ip = "xxx.xxx.xxx.xxx"; // 伺服器IP地址
String username = "yourUsername"; // 用於登陸伺服器的使用者名稱
String password = "yourPassword"; // 登陸密碼
String remoteDirectoryPath = "/home/ftp/log"; // 遠端資料夾的絕對路徑
String remoteFileName = "log_20180913"; // 將本地檔案上傳到伺服器後文件的名字
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ip);
boolean isLogin = ftpClient.login(username, password);
System.out.println("登陸成功? " + isLogin);
ftpClient.changeWorkingDirectory(remoteDirectoryPath);
FTPFile[] ftpFiles = ftpClient.listFiles();
for (FTPFile file : ftpFiles) {
if (file.getName().equals(remoteFileName)) {
ftpClient.setRestartOffset(file.getSize());
OutputStream os = ftpClient.appendFileStream(remoteFileName);
os.write("\nhello append content".getBytes("utf-8"));
os.flush();
os.close();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}