工具类---FTP工具类
1. 导入commons-net
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
2. 创建config.properties文件,配置FTP服务器信息
#ftp服务器地址
ftp.address=182.61.40.184
#ftp默认端口是21
ftp.port=21
#ftp用户名
ftp.username=root
#ftp密码
ftp.password=xxxxxxx
3. 读取config.properties配置文件的工具类ConfigReaderUtil
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
/**
* DESC : 读取配置文件工具类
*
*/
public class ConfigReaderUtil {
public ConfigReaderUtil(){}
private static Properties props = new Properties();
static{
try {
//加载配置文件,需要手动指定配置文件
props.load(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"),"UTF-8"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*根据key获取value*/
public static String getValue(String key){
return props.getProperty(key).trim();
}
}
4. FTP工具类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.log4j.Logger;
/**
* DESC : ftp工具类
*
* @author Lonely
*
*/
public class FTPUtil {
private static Logger logger = Logger.getLogger(FTPUtil.class);
static FTPClient ftp = null;
private static String ftpAddress = ConfigReaderUtil.getValue("ftp.address"); // ftp服务器地址
private static Integer ftpPort = Integer.valueOf(ConfigReaderUtil.getValue("ftp.port")); // ftp端口
private static String ftpUsername = ConfigReaderUtil.getValue("ftp.username"); // ftp用户名
private static String ftpPassword = ConfigReaderUtil.getValue("ftp.password"); // ftp密码
/**
* DESC 登录FTP
* @param ip
* @param port
* @param username
* @param password
* @return
*/
public static boolean login() {
try {
ftp = new FTPClient();
ftp.connect(ftpAddress, ftpPort);
boolean loginResu = ftp.login(ftpUsername, ftpPassword);
if (loginResu) {
logger.info("登录FTP成功");
// 设置UTF-8编码
ftp.setCharset(Charset.forName("UTF-8"));
ftp.setControlEncoding("UTF-8");
return true;
} else {
logger.info("登录FTP失败");
return false;
}
} catch (IOException e) {
logger.info("登录FTP出现异常");
e.printStackTrace();
return false;
}
}
/**
* DESC 登出FTP
*/
public static void disconnect() {
if (null != ftp && ftp.isConnected()) {
try {
ftp.logout();
ftp.disconnect();
logger.info("登出FTP......");
logger.info("断开FTP......");
} catch (IOException ex) {
}
}
}
/**
* DESC 下载文件
*
* @param remoteFileUrl FTP服务器文件路径 例:/index.html
* @param localFileUrl 要保存到本地的路径 例:D:/index.html
* @return
*/
public static boolean downloadFile(String remoteFileUrl, String localFileUrl) {
FileOutputStream fos = null;
InputStream is = null;
boolean loginResu = login(); // 登录
try {
if (loginResu) {
is = ftp.retrieveFileStream(remoteFileUrl);
fos = new FileOutputStream(new File(localFileUrl));
byte[] b = new byte[1024];
int len = 0;
while ((len = is.read(b)) != -1) {
fos.write(b, 0, len);
}
logger.info("下载文件:" + remoteFileUrl + " 成功----------------------");
return true;
} else {
logger.info("下载文件 :" + remoteFileUrl + " 失败----------------------");
return false;
}
} catch (IOException e) {
e.printStackTrace();
logger.info("下载文件失败");
return false;
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* DESC 上传文件
*
* @param remoteFileUrl 要保存文件的路径 例:/index.html
* @param localFileUrl 要上传的文件路径 例:D:/index.html
* @return
*/
public static boolean uploadFile(String remoteFileUrl, String localFileUrl) {
FileInputStream fis = null;
OutputStream os = null;
boolean loginResu = login(); // 登录
try {
if (loginResu) {
os = ftp.storeFileStream(remoteFileUrl);
fis = new FileInputStream(new File(localFileUrl));
byte[] b = new byte[1024];
int len = 0;
while ((len = fis.read(b)) != -1) {
os.write(b, 0, len);
}
logger.info("上传文件:" + localFileUrl + " 成功----------------");
return true;
} else {
logger.info("上传文件:" + localFileUrl + " 失败----------------");
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* DESC 获取ftp某一文件(路径)下的文件名字,用于查看文件列表
* @param remotedir 远程地址目录
* @return
*/
public static boolean getFilesName(String remotedir){
boolean loginResu = login(); // 登录
if (loginResu) {
FTPFile[] files;
try {
files = ftp.listFiles(remotedir);
for (int i = 0; i < files.length; i++) {
logger.info(files[i].getName() + "----");
//其他操作
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
} else {
return false;
}
}
/**
* DESC 删除FTP服务器文件
* @param ftpFileName 要删除的文件路径
*/
public static boolean deleteFile(String ftpFileName){
boolean loginResu = login(); // 登录
if (loginResu) {
boolean delResu;
try {
delResu = ftp.deleteFile(ftpFileName); //删除文件
if(delResu) {
logger.info("删除FTP服务器文件:" + ftpFileName + "成功");
return true;
}else {
logger.info("删除FTP服务器文件:" + ftpFileName + "失败");
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}else {
return false;
}
}
}
调用测试
public static void main(String[] args) {
//打印 /usr 下的文件(文件夹)名
FTPUtil.getFilesName("/usr");
//上传本地 D:/G(other)/temp/index.html 到 FTP服务器 /index.html
FTPUtil.uploadFile("/index.html", "D:/G(other)/temp/index.html");
//下载FTP服务器中的 /index.html 到 本地 D:/G(other)/temp/index.html
FTPUtil.downloadFile("/index.html", "D:/G(other)/temp/index.html");
//删除FTP服务器文件 /index.html
FTPUtil.deleteFile("/index.html");
//登出 断开连接
FTPUtil.disconnect();
}
还没有评论,来说两句吧...