Java FTPClient 远程文件上传下载追加

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36420790/article/details/82689931

注意事项:

  1. 导入jar包commons-net-3.6.jar(百度云分享)
  2. 用于登陆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();
			}
		}
	}

贴上GitHub源文件:

https://github.com/tanmujin/FTPClient

猜你喜欢

转载自blog.csdn.net/qq_36420790/article/details/82689931