FTP上传下载文件
工作中有些项目会有需要链接ftp远程服务器,上传/下载文件,mac安装ftp软件收费,破解也没找到路径。通过问题的关键字(异常名称,卡点日志信息)百度,试一试。网络权限问题一定要检查好,Mac打开防火墙选项,添加允许ftp接入。想到以下两种方式,记录一下,方便以后再次使用。 方式1:mac终端ftp命令操作 方式2:java代码客户端操作
方式一:mac终端ftp命令操作 准备工作
//检查本机是否具有访问ftp远程服务器权限
telnet ftpip port
es:telnet 127.0.0.1 21
//打开终端,通过brew命令安装ftp命令工具
brew install inetutils
//检查ftp版本,是否已安装成功,显示版本号即为成功
ftp -V
//打开终端,输入ftp,回车切换至ftp命令行open 127.0.0.1 21
ftp> open ftpip port
//显示以下信息即已连接,
Connected to xx.xx.xxx.xx
...
//按照提示输入用户名,密码
Name (xxx.xx.xxx.xx:test): test
331 Password required for test
Password:
230 Logged on
//切换ftp被动模式
ftp> passive
Passive mode on.
//查看远程ftp服务器目录
ftp> ls
//切换远程服务器目标路径(例)
ftp> cd /home/.../
//定位本机文件夹路径
ftp> lcd /Users/.../本地文件存放路径
//文件上传:put 文件名.文件类型
ftp> put test.txt
//文件下载:get 文件名.文件类型
ftp> get test.txt
方式二:java代码客户端操作 POM
实操代码
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.*;
public class FTPUtils {
public static void main(String[] args) {
System.out.println("=====start");
//1.登录ftp 用户名,密码,ftp地址,端口
FTPClient client = FTPUtils.ftpLogin("test","syst4638#!0","127.0.0.1",21);
//2.上传文件-远程文件名和本地文件名是一致的
String fileName = "test.txt";
//本地路径名-ftp远程路径名
String localPath = "/Users/localhost/Documents/test/";
String remotePath = "/company/";
//远程路径名,如果已知目标路径则不需要查看
FTPUtils.getFileList(client,"/");
//2.上传文件
FTPUtils.ftpUpload(client,remotePath,localPath,fileName);
//3.下载文件
FTPUtils.ftpDown(client,remotePath,localPath,fileName);
System.out.println("=====end");
}
/**
* ftp远程登录
* @param userName 用户名
* @param password 密码
* @param ip ftp服务器ip
* @param port 端口
* @return
*/
public static FTPClient ftpLogin(String userName,String password,String ip,int port){
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ip, port);
Boolean flag = ftpClient.login(userName, password);
System.out.println("登录标志:"+flag);
} catch (IOException e) {
e.printStackTrace();
}
return ftpClient;
}
/**
* 默认根文件目录是"/"
* @param ftpClient
* @param filePath
*/
public static void getFileList(FTPClient ftpClient,String filePath){
FTPFile[] files = new FTPFile[0];
ftpClient.enterLocalPassiveMode();
try {
files = ftpClient.listFiles(filePath);
for (FTPFile file : files) {
if (file.isFile()) {
System.out.println("FileName:"+file.getName());
} else if (file.isDirectory()) {
System.out.println("FileDirectory"+file.getName());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 上传文件至ftp远程服务器
* @param ftpClient
* @param targetFilePath
* @param filePath
* @param remoteFileName
*/
public static void ftpUpload(FTPClient ftpClient,String targetFilePath,String filePath,String remoteFileName){
try {
FileInputStream in = new FileInputStream(filePath+remoteFileName);
//切换被动模式
ftpClient.enterLocalPassiveMode();
//切换至目标文件夹
boolean isChange = ftpClient.changeWorkingDirectory(targetFilePath);
System.out.println("远程服务器当前目录:"+ftpClient.printWorkingDirectory());
// 第三步:文件上传
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // 设置文件类型
// remoteFileName:远程FTP文件的保存名称,in:本地待上传文件的文件输入流
boolean isStore = ftpClient.storeFile(remoteFileName, in);
System.out.println("文件上传成功标志:" + isStore);
} catch (
FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 断开连接
try {
ftpClient.disconnect();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
ftpClient = null;
}
}
}
/**
* 下载文件
* @param ftpClient
* @param remoteFilePath
* @param localFilePath
* @param remoteFileName
*/
public static void ftpDown(FTPClient ftpClient,String remoteFilePath,String localFilePath,String remoteFileName){
try {
//切换至目标文件夹
boolean isChange = ftpClient.changeWorkingDirectory(remoteFilePath);
System.out.println("远程服务器当前目录:"+ftpClient.printWorkingDirectory());
File file = new File(localFilePath+remoteFileName);
OutputStream outputStream = new FileOutputStream(file);
//切换被动模式
ftpClient.enterLocalPassiveMode();
ftpClient.retrieveFile(remoteFilePath+remoteFileName, outputStream);
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 断开连接
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
精彩链接
发表评论