导入依赖
<!--FtpClient所在的包-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<!--日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
ftp对象创建
/**
* ftp对象
* @param ip 地址
* @param userName 用户名
* @param password 密码
* @return
* @throws IOException
*/
public static FTPClient getFtp(String ip,String userName,String password) throws IOException {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ip,21);
ftpClient.login(userName,password);
return ftpClient;
}
ftp上传文件
/**
* 上传ftp
* @param ftpClient ftp对象
* @param path 定位到的ftp的目录
* @param in 上传的流
* @throws IOException
*/
private static void uploadFile(FTPClient ftpClient, String path, InputStream in) throws IOException {
int replyCode = ftpClient.getReplyCode();
ftpClient.setDataTimeout(120000);
//设置为二进制文件
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
if (!FTPReply.isPositiveCompletion(replyCode)) {
ftpClient.disconnect();
System.out.println("FTP连接失败");
}else {
System.out.println("FTP连接成功");
}
boolean changeWorkingDirectory = ftpClient.changeWorkingDirectory(path);
//文件夹切换失败,则创建文件夹
if(!changeWorkingDirectory){
String[] split = path.split("/");
for (String s : split) {
if (ftpClient.changeWorkingDirectory(s)) {
continue;
}
boolean makeDirectory = ftpClient.makeDirectory(s);
if (makeDirectory) {
boolean b = ftpClient.changeWorkingDirectory(s);
if(!b){
throw new RuntimeException("切换目录失败");
}
}
}
}
String removePath = new String("Servlet_12.png".getBytes("UTF-8"),"iso-8859-1");
//上传
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
if(ftpClient.storeFile(removePath, in)){
System.out.println("文件上传成功");
}else{
System.out.println("文件上传失败");
}
//关闭文件流
in.close();
//关闭连接
if (ftpClient != null) {
ftpClient.logout();
ftpClient.disconnect();
}
}
ftp下载文件
/**
* 下载ftp
* @param ftpClient ftp对象
* @param filePath ftp上的文件夹路径
* @param fileName ftp上文件的路径
* @throws IOException
*/
public static void downFtp(FTPClient ftpClient,String filePath,String fileName) throws IOException {
//定位到ftp目录
ftpClient.changeWorkingDirectory(filePath);
FTPFile[] ftpFiles = ftpClient.listFiles();
for (FTPFile ftpFile : ftpFiles) {
byte[] bytes = ftpFile.getName().getBytes("iso-8859-1");
String str = new String(bytes,"UTF-8");
if(str.equals(fileName)){
OutputStream outputStream = new FileOutputStream("C:\\Users\\泽\\Desktop\\新建文本文档.png");
ftpClient.retrieveFile(ftpFile.getName(),outputStream);
outputStream.close();
}
}
//断开ftp
ftpClient.disconnect();
}
ftp删除文件
/**
* 删除文件
* @param ftpClient ftp对象
* @param path 文件在ftp上的路径
* @param fileName 文件名称
* @throws IOException
*/
public static void deleteFtp(FTPClient ftpClient,String path,String fileName) throws IOException {
ftpClient.changeWorkingDirectory(path);
ftpClient.deleteFile( fileName);
}
测试
public static void main(String[] args) {
try {
FTPClient ftpClient = getFtp("192.168.56.1","anonymous","");
InputStream inputStream = new FileInputStream("D:/Servlet_12.png");
// uploadFile(ftpClient,"/test",inputStream);
// downFtp(ftpClient,"/test","Servlet_12.png");
deleteFtp(ftpClient,"/test","Servlet_12.png");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}