写在开头: 平常我们在开发中对ftp的操作是比较频繁的,因此总结一个ftp使用工具类,算是改个轮子吧!
依赖的一个jar:commons-net-3.6.jar
FtpUtils.java
public class FtpUtils {
public FTPClient ftp;
public String ip;
public int port;
public String name;
public String pwd;
public boolean isLogin=false;
public FtpUtils(String ip,int port,String name ,String pwd){
this.ip=ip;
this.port=port;
this.name=name;
this.pwd=pwd;
if (this.ftp==null) {
this.ftp=new FTPClient();
try {
this.ftp.connect(ip, port);
this.isLogin=ftp.login(name, pwd);
this.ftp.setCharset(Charset.forName("UTF-8"));
this.ftp.setControlEncoding("UTF-8");
// ftp传递zip二进制时需要设置 默认ascll编码 操作txt等没问题
this.ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @Description 下载文件
* @param fileName
* @param localPath
* @return
*/
public boolean getFile(String fileName,String localPath){
if (this.ftp==null) {
System.out.println("ftp is null,return ...");
return false;
}
if (!this.ftp.isConnected()) {
System.out.println("ftp is DisConnected,return ...");
return false;
}
if (!this.isLogin) {
System.out.println("ftp is notLogin,return ...");
return false;
}
FileOutputStream fos=null;
try {
fos=new FileOutputStream(new File(localPath));
return this.ftp.retrieveFile(fileName, fos);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally {
if (fos!=null) {
try {
fos.close();
fos=null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* @Description 上传文件
*/
public boolean putFile(String filePath,String fileName){
if (this.ftp==null) {
System.out.println("ftp is null,return ...");
return false;
}
if (!this.ftp.isConnected()) {
System.out.println("ftp is DisConnected,return ...");
return false;
}
if (!this.isLogin) {
System.out.println("ftp is notLogin,return ...");
return false;
}
FileInputStream fis=null;
try {
fis=new FileInputStream(new File(filePath));
return this.ftp.storeFile(fileName, fis);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally {
if (fis!=null) {
try {
fis.close();
fis=null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* @Description 关闭登录及连接
* @throws Exception
*/
public void close() throws Exception{
if (this.isLogin) {
this.ftp.logout();
}
if (this.ftp.isConnected()) {
this.ftp.disconnect();
}
}
/**
* @Description 列出一个路径下的所有ftp文件及目录
* @param path
* @return
* @throws IOException
*/
public List<String> listAllFiles(String path) throws IOException{
ftp.enterLocalPassiveMode();
FTPFile[] files = ftp.listFiles(path, new FTPFileFilter() {
@Override
public boolean accept(FTPFile f) {
//if (f.isFile()) return true;
/*if (f.isDirectory()) {
ftp.listFiles(pathname)
}*/
return true;
}
});
List<String> list =new ArrayList<>();
for (FTPFile ftpFile : files) {
list.add(ftpFile.getName());
}
return list;
}
}
测试
public class MainRoot {
public static String ip="24.22.10.11";
public static int port=21;
public static String name="ftpdd";
public static String passwd="ftpdd123";
public static void main(String[] args) throws IOException, InterruptedException {
FtpUtils ftpUtils=new FtpUtils(ip, port,name,passwd);
boolean putFile = ftpUtils.putFile("F://test/d1.zip");
System.out.println(putFile);
}
}
最后附上一份常用服务器ftp的简单操作
1.确认linux开启ftp服务
检测ftp状态:service vsftpd status;
2.修改配置
cd /etc/vsftpd
修改配置文件 vsftpd.conf
关闭匿名登录(必须要用户密码登录):anonymous_enable=NO
设置映射目录:local_root=/home/ffd/data
给映射目录设置高级权限(否则无法上传下载):chmod 777 /home/ffd/data
添加用户和密码:
useradd ftpdd
passwd ftpdd123
重启服务:service vsftpd restart
3.浏览器登录
清浏览器缓存后:
ftp:ip
输入用户名/密码
4.服务端shell操作
ftp ip
查看服务端文件 :ls
转换本地路径:lcd /home
上传本地文件到ftp:put 文件名