package com.limi.ftp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
public class FtpUtil {
private String ip = "";
private String username = "";
private String password = "";
private int port = -1;
private String directory = "";
private String srcUrl = "";
private String localUrl = "";
FTPClient ftpClient = null;
private OutputStream fos = null;
private FileInputStream fis = null;
public FtpUtil(String serverIP, int port, String username, String password,
String srcUrl,String directory,String localUrl) {
this.ip = serverIP;
this.username = username;
this.password = password;
this.port = port;
this.srcUrl = srcUrl;
this.directory = directory;
this.localUrl = localUrl;
}
public boolean connectServer() {
if (ftpClient == null) {
ftpClient = new FTPClient();
}
try {
if (port != -1) {
ftpClient.connect(this.ip, this.port);
}
boolean result = ftpClient.login(this.username, this.password);
ftpClient.changeWorkingDirectory(this.directory);
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.setBufferSize(1024);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean upload() {
try {
File srcFile = new File(this.srcUrl);
fis = new FileInputStream(srcFile);
ftpClient.setControlEncoding("gbk");
//存储文件==上传文件
boolean result = ftpClient.storeFile(srcFile.getName(), fis);
return result;
} catch (Exception e) {
e.printStackTrace();
System.out.println("上传是失败!");
}finally{
try {
fis.close();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public void download(){
File downFile = null;
boolean result = true;
try{
//每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据。
ftpClient.enterLocalPassiveMode();
String[] strArr = getFile();
for(int i=0;strArr!=null&i<strArr.length;i++){
downFile = new File(this.localUrl+"\\"+strArr[i]);
fos = new FileOutputStream(downFile);
//检索文件==下载文件
result = ftpClient.retrieveFile(strArr[i], fos);
if(!result){
System.out.println(strArr[i]+"下载失败!");
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
fos.close();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public boolean remove(){
try {
return ftpClient.deleteFile("a.png");
} catch (IOException e) {
e.printStackTrace();
}//删除远程文件
return false;
}
public boolean rename(){
//每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据。
ftpClient.enterLocalPassiveMode();
try {
ftpClient.rename("3111.jpg", "3.png");
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public boolean makeDir(){
//每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据。
ftpClient.enterLocalPassiveMode();
try {
return ftpClient.makeDirectory("ems");
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public boolean renameDir(){
ftpClient.enterLocalPassiveMode();
try {
return ftpClient.rename("ems", "ems1");
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public boolean removeDir(){
ftpClient.enterLocalPassiveMode();
try {
return ftpClient.removeDirectory("ems1");
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public String[] getFile(){
try {
ftpClient.enterLocalPassiveMode();
String[] strArr = ftpClient.listNames();
return strArr;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
养成做笔记的习惯吧,用到了就来看看。