设为首页 收藏本站
查看: 960|回复: 0

[经验分享] FTP工具类

[复制链接]

尚未签到

发表于 2016-6-7 11:53:42 | 显示全部楼层 |阅读模式
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import com.aspire.prm.app.iodd.common.remoteclient.RemoteClient;
import com.aspire.prm.dmplt.basic.domain.FtpConfig;
/**
* name:FtpClient
* <p>
* </p>
*
* @author:lipeng
* @data:2014-9-20 下午05:20:26
* @version 1.0
*/
public class FtpClient implements RemoteClient {
private FTPClient client = null;
private FtpConfig config = null;
private static final Logger logger = Logger.getLogger(FtpClient.class);;
/** 当前工作目录,每次关闭连接要回复到null,因为当前类是单例类 */
private String workDirectory = null;
/** 是否手工控制连接 */
private boolean handSwitch = false;
/** true表示已经登录到ftp服务器 */
private boolean ready = false;
/**
* 初始化参数配置及创建commons.net.ftp的客户端
*/
public FtpClient(FtpConfig config) {
client = new FTPClient();
this.config = config;
client.setControlEncoding(config.getRemoteEncoding());
// 设置当前工作目录
workDirectory = config.getRootPath();
}
/**
* 连接ftp
*
* @return
* @throws SocketException
* @throws IOException
*/
private boolean connect() throws SocketException, IOException {
client.connect(config.getServer(), Integer.valueOf(config.getPort()));
int reply;
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
logger.info("FTP server refused connection.");
return false;
}
return true;
}
/**
* 登入ftp
*
* @return
* @throws IOException
*/
private boolean login() throws IOException {
if (!client.login(config.getUsername(), config.getPassword())) {
client.logout();
logger.info("FTP server login fail.");
return false;
}
return true;
}
/**
* 连接然后登入统一入口
*
* @return
* @throws SocketException
* @throws IOException
*/
public boolean ready() throws SocketException, IOException {
if (connect() && login()) {
setConfig();
ready = true;
return true;
}
return false;
}
/**
* ftp运行环境参数配置
*
* @throws IOException
*/
private void setConfig() throws IOException {
FTPClientConfig conf = new FTPClientConfig(config.getFTPStyle());
client.configure(conf);
// 被动传输模式
if (config.getPassiveMode())
client.enterLocalPassiveMode();
// 二进制传输模式
if (config.getBinaryFileType())
client.setFileType(FTP.BINARY_FILE_TYPE);
// 设置当前工作目录
client.changeWorkingDirectory(getWorkDirectory());
}
/**
* 关闭连接
*
* @throws IOException
*/
public void close() throws IOException {
if (client.isConnected()) {
client.logout();
client.disconnect();
// 也可设置为null
workDirectory = config.getRootPath();
}
ready = false;
}
/**
* 获取等前工作目录的文件列表
*
* @return
* @throws IOException
*/
public String[] listFiles() throws IOException {
if (!setReady()) {
return null;
}
FTPFile[] files = client.listFiles();
int filesLength = files.length;
String[] fileNameArr = new String[filesLength];
for (int i = 0; i < filesLength; i++) {
fileNameArr = files.getName();
}
setClose();
return fileNameArr;
}
/**
* 上传文件,文件名方式
*
* @param path
* @param name
* @return
* @throws Exception
* @throws IOException
*/
public boolean uploadFile(String uploadFile, String remoteName) throws Exception {
FileInputStream fis = null;
try {
if (!setReady()) {
return false;
}
fis = new FileInputStream(uploadFile);
if (remoteName.contains("/")) {
String remotePath = remoteName.substring(0,remoteName.lastIndexOf("/"));
client.makeDirectory(remotePath);
remoteName=remoteName.substring(remoteName.lastIndexOf("/") + 1,remoteName.length());
client.changeWorkingDirectory(workDirectory+"/"+remotePath);
}
if (client.storeFile(remoteName, fis)) {
logger.info(" upload success !!! ");
return true;
}
client.changeWorkingDirectory(workDirectory);
} catch (Exception e) {
logger.error(" upload fail !!! ");
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e2) {
logger.error(e2.getMessage(), e2);
}
}
setClose();
}
return false;
}
/**
* 上传文件,流方式
*
* @param path
* @param name
* @return
* @throws IOException
*/
public boolean uploadFile(InputStream stream, String name, String remoteName) {
try {
if (!setReady()) {
return false;
}
if (client.storeFile(getWorkDirectory() + remoteName, stream)) {
logger.info(" upload success !!! ");
return true;
}
} catch (Exception e) {
logger.error(" upload fail !!! ");
return false;
} finally {
if (stream != null) {
try {
stream.close();
} catch (Exception e2) {
logger.error(e2.getMessage(), e2);
}
}
}
return false;
}
/**
* 下载文件
*
* @param ftpFileName
* @param localName
* @return
* @throws Exception
*/
public boolean downloadFile(String ftpFileName, String localName) throws Exception {
FileOutputStream fos = null;
try {
File localFile=new File(localName);
if(localFile!=null&&!localFile.exists()){
if(localFile.getParentFile()!=null&&!localFile.getParentFile().exists()){
localFile.getParentFile().mkdirs();
}
localFile.createNewFile();
}
fos = new FileOutputStream(localFile);
if (!setReady()) {
return false;
}
if (client.retrieveFile(
new String(ftpFileName.getBytes(config.getLocalEncoding()), config.getRemoteEncoding()), fos)) {
logger.info("download success !!! ");
return true;
}
logger.info(" download fail !!! ");
return false;
} catch (Exception e) {
logger.error("ftp下载文件失败ftpFileName:" + ftpFileName + ",localName" + localName, e);
throw e;
} finally {
if (fos != null) {
try {
fos.close();
} catch (Exception e2) {
}
}
}
}
/**
* 删除文件
*
* @param path
* @param name
* @return
* @throws IOException
*/
public boolean removeFile(String name) throws Exception {
if (!setReady()) {
return false;
}
client.changeWorkingDirectory(config.getRootPath());
if (client.deleteFile(name)) {
logger.info("remove file success !!! ");
return true;
}
logger.info(" remove file fail !!! ");
return false;
}
/**
* 改变工作目录
*
* @param path
* @throws IOException
*/
public void setWorkDirectory(String path) throws IOException {
workDirectory = (config.getRootPath() + path);
// 如果是手动控制可以设置改变工作目录
if (handSwitch) {
client.changeWorkingDirectory(workDirectory);
}
}
/**
*
* 复制文件
* @param @param src
* @param @param dst
* @param @return
* @param @throws Exception     
* @return boolean
*/
public boolean copyFile(String src,String dst) throws Exception {
try {
setReady();
if (dst.contains("/")) {
String remotePath = dst.substring(0,dst.lastIndexOf("/"));
client.makeDirectory(remotePath);
}
InputStream inputStream=client.retrieveFileStream(src);
if (!client.completePendingCommand()) {
return false;
}
// 如果读取的文件流不为空则复制文件
if (inputStream != null) {
boolean tt= client.storeFile(dst, inputStream);
// 关闭文件流
inputStream.close();
return tt;
}
return false;
} catch (Exception e) {
logger.error("ftp复制文件出错,[src:" + src+",dst:"+dst+"]", e);
throw e;
}
}
/**
* 创建目录
*
* @param pathname
* @return
* @throws IOException
*/
public boolean createDirectory(String pathname) throws IOException {
if (!setReady()) {
return false;
}
boolean okFlag = client.makeDirectory(pathname);
setClose();
return okFlag;
}
/**
* 获取当前工作目录
*
* @return
*/
public String getWorkDirectory() {
return workDirectory;
}
/**
* 准备FTP连接环境
*
* @return
* @throws SocketException
* @throws IOException
*/
private boolean setReady() throws SocketException, IOException {
if (!ready) {
if (!ready()) {
logger.error("Ftp ready fail.");
if (client.isConnected())
client.disconnect();
return false;
}
}
ready = true;
return true;
}
/**
* 设置是否ftp连接
*
* @throws IOException
*/
private void setClose() {
try {
if (!handSwitch)
close();
} catch (Exception e) {
logger.error("关闭ftp连接出错", e);
}
}
/**
* 打开手动连接
*/
public void openHandSwitch() {
handSwitch = true;
}
/**
* 关闭手动连接
*/
public void closeHandSwitch() {
handSwitch = false;
}
}

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-227432-1-1.html 上篇帖子: FTP source code 下篇帖子: python写的FTP工具
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表