package com.example.test_api.util;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import sun.net.ftp.FtpProtocolException;
import java.io.*;
/**
* ftp工具类
* auth hzj 2019-01-10
*/
public class FtpUtils {
private static FTPClient ftp;
/**
* 获取ftp连接
*
* @param url 服务地址
* @param port 服务端口
* @param username 用户名
* @param password 密码
* @param path 上传到ftp哪呢目录
* @return
* @throws IOException
*/
public static FTPClient initFtpClient(String url, int port, String username, String password, String path) throws IOException {
ftp=new FTPClient();
int reply;
ftp.connect(url, port);//连接FTP服务器
ftp.login(username, password);//登录
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println("连接ftp服务器失败...ftp服务:" + url + ":" + port);
}
ftp.changeWorkingDirectory(path);
System.out.println("connect successfu...ftp服务:" + url + ":" + port);
return ftp;
}
/**
* 上传文件夹或上传单个文件
* @param file 上传的文件
* @throws IOException
* @throws FtpProtocolException
*/
public static void uploadFolder(File file) throws IOException {
boolean rsult = false;
if (file.isDirectory()) {
ftp.makeDirectory(file.getName());//在ftp服务上创建一个和要上传文件同名的文件夹
ftp.changeWorkingDirectory(file.getName());//改变工作目录到刚创建的文件夹下
File[] files = file.listFiles();
for (File f : files) {
if (f.isDirectory()) { //子文件夹
uploadFolder(f);
ftp.changeToParentDirectory(); //上传完子文件夹切换回父目录
} else {
File file1 = new File(file.getPath() + File.separator + f.getName());
InputStream input = new FileInputStream(file1);
boolean b = ftp.storeFile(file1.getName(), input);
input.close();
}
}
} else {
File file1 = new File(file.getPath());
FileInputStream input = new FileInputStream(file1);
ftp.storeFile(file1.getName(), input);
input.close();
}
}
/**
* 上传单个文件, 判断是否上传成功
* @param file 要上传的文件
* @return
* @throws IOException
*/
public static boolean uploadFile(File file) throws IOException {
InputStream input=new FileInputStream(file);
boolean b = ftp.storeFile(file.getName(), input);
return b;
}
/**
* 下载ftp上文件, 支持指定文件下载, 目录下载
* @param remotePath ftp文件目录
* @param localPath 本地保存文件目录
* @param fileName 要下载的文件名
* @throws IOException
*/
public static void downFiles(String remotePath,String localPath,String fileName) throws IOException {
ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (fileName!=null&&!ff.getName().equals(fileName)){
continue;
}
if (ff.isDirectory()){
File file=new File(localPath+File.separator+ff.getName());
if (!file.exists())file.mkdirs();
downFiles(remotePath+File.separator+ff.getName(),localPath+File.separator+ff.getName(),"");
ftp.changeToParentDirectory(); //上传完子文件夹切换回父目录
}else {
File localFile = new File(localPath + File.separator + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
}
}
}
/**
* 断开ftp连接
* @throws IOException
*/
public static void shutDownFtp() throws IOException {
ftp.logout();
ftp.disconnect();
}
public static void main(String[] args) {
//ftp连接
try {
FtpUtils.initFtpClient("124.239.129.67",2121,"idc","idc12345","");
} catch (IOException e) {
System.out.println("ftp服务连接异常!");
}
//上传单个文件
try {
File file=new File("F:\\123\\123.zip");
FtpUtils.uploadFile(file);
} catch (IOException e) {
System.out.println("ftp上传文件异常!");
}
//上传多个文件
try {
File file=new File("F:\\123");
FtpUtils.uploadFolder(file);
} catch (IOException e) {
System.out.println("ftp上传文件异常!");
}
//下载文件
try {
FtpUtils.downFiles("","F:\\down","data-2887392133154708628400001.zip");
} catch (IOException e) {
System.out.println("ftp下载文件异常!");
}
}
}