private String hostname = "127.0.0.1";
private Integer port = 21;
private String username = "test";
private String password = "123456";
private FTPClient ftpClient = null;
/**
* 初始化服务器
*
*/
public void initFtpClient() {
ftpClient = new FTPClient();
ftpClient.setControlEncoding("UTF-8");
try {
System.out.println("connect.....server:" + this.hostname + ":" + this.port);
ftpClient.connect(this.hostname, this.port);
ftpClient.login(username, password);
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("connect....ftp server failed:" + this.hostname + ":" + this.port);
}
System.out.println("connect....ftp server success:" + this.hostname + ":" + this.port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 上传文件
*/
public boolean uploadFile(String pathName, String fileName, String originFileName) {
boolean flag = false;
InputStream inputStream = null;
System.out.println("开始上传文件");
try {
inputStream = new FileInputStream(new File(originFileName));
initFtpClient();
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
CreateDirectory(pathName);
ftpClient.makeDirectory(pathName);
ftpClient.changeWorkingDirectory(pathName);
ftpClient.storeFile(fileName, inputStream);
inputStream.close();
ftpClient.logout();
flag = true;
System.out.println("上传文件成功");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("上传文件失败");
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return flag;
}
public boolean uploadFile(String pathName,String fileName,InputStream in) {
boolean flag=false;
System.out.println("开始上传文件");
initFtpClient();
try {
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
CreateDirectory(pathName);
ftpClient.changeWorkingDirectory(pathName);
ftpClient.storeUniqueFile(fileName, in);
in.close();
ftpClient.logout();
flag=true;
System.out.println("上传文件成功");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("上传文件失败");
e.printStackTrace();
} finally {
if(ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(in!=null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return flag;
}
private boolean CreateDirectory(String pathName) throws Exception {
// TODO Auto-generated method stub
boolean success = true;
String directory = pathName + "/";
if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory))) {
int start = 0;
int end = 0;
if (directory.startsWith("/")) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf("/", start);
String path = "";
String paths = "";
while (true) {
String subDirectory = new String(pathName.substring(start, end).getBytes("GBK"), "iso-8859-1");
path = path + "/" + subDirectory;
if (!existFile(path)) {
if (makeDirectory(subDirectory)) {
changeWorkingDirectory(subDirectory);
} else {
System.out.println("创建目录[" + subDirectory + "]失败");
changeWorkingDirectory(subDirectory);
}
} else {
changeWorkingDirectory(subDirectory);
}
paths = paths + "/" + subDirectory;
start = end + 1;
end = directory.indexOf("/", start);
// 检查所有目录是否创建完毕
if (end <= start) {
break;
}
}
}
return success;
}
private boolean makeDirectory(String subDirectory) {
// TODO Auto-generated method stub
boolean flag = true;
try {
flag = ftpClient.makeDirectory(subDirectory);
if (flag) {
System.out.println("创建文件夹" + subDirectory + " 成功!");
} else {
System.out.println("创建文件夹" + subDirectory + " 失败!");
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
private boolean existFile(String path) throws Exception {
// TODO Auto-generated method stub
boolean flag = false;
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
if (ftpFileArr.length > 0) {
flag = true;
}
return flag;
}
private boolean changeWorkingDirectory(String directory) {
// TODO Auto-generated method stub
boolean flag = true;
try {
flag = ftpClient.changeWorkingDirectory(directory);
if (flag) {
System.out.println("进入文件夹" + directory + " 成功!");
} else {
System.out.println("进入文件夹" + directory + " 失败!开始创建文件夹");
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return flag;
}
public boolean downloadFile(String pathName,String fileName,String localPath) {
boolean flag=false;
OutputStream out=null;
System.out.println("开始下载文件");
initFtpClient();
try {
ftpClient.changeWorkingDirectory(pathName);
FTPFile[] ftpFiles=ftpClient.listFiles();
for (FTPFile ftpFile : ftpFiles) {
if(fileName.equalsIgnoreCase(ftpFile.getName())) {
File localFile=new File(localPath+"/"+ftpFile.getName());
out=new FileOutputStream(localFile);
ftpClient.retrieveFile(ftpFile.getName(), out);
out.close();
}
}
ftpClient.logout();
System.out.println("下载文件成功");
flag=true;
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("下载文件失败");
e.printStackTrace();
}finally {
if(ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(out!=null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return flag;
}
public static void main(String[] args) throws Exception {
FtpUtils ftp=new FtpUtils();
//ftp.uploadFile("ftpFile/data", "1.jpg", "E:\\1.jpg");
//InputStream in=new FileInputStream(new File("E:\\3.jpg"));
//ftp.uploadFile("/", "3.jpg", in);
ftp.downloadFile("/", "3.jpg", "F:");
}
}
pom文件如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.taikang.ftp</groupId>
<artifactId>ftp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.ftpserver</groupId>
<artifactId>ftpserver-core</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>1.4.1</version>
</dependency>
</dependencies>
</project>