ftp 上传 文件 下载文件

来源:互联网 发布:海康网络摄像头价位 编辑:程序博客网 时间:2024/05/29 12:05

private static Log log = LogFactory.getLog(FTPClientHelper.class);


private FTPClient ftpClient = null; // FTP 客户端代理


/**
* 连接制定的ftp服务器
*
* @param ip
*            远程服务器的ip地址
* @param port
*            ftp端口号
* @param userName
*            登陆用户名
* @param password
*            登陆密码
* @return 登陆成功或失败
* @throws AppException
*/
public boolean connectServer(String ip, int port, String userName, String password) throws Exception {
boolean flag = true;
if (ftpClient == null) {
int reply;
try {
ftpClient = new FTPClient();
ftpClient.setControlEncoding("GBK"); // 文件名乱码,默认ISO8859-1,不支持中文
ftpClient.setDefaultPort(port);
ftpClient.connect(ip);
ftpClient.login(userName, password);
if (log.isDebugEnabled())
log.debug(ftpClient.getReplyString());


reply = ftpClient.getReplyCode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setDataTimeout(600000);


if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
if (log.isDebugEnabled())
log.debug("FTP server refused connection.");


flag = false;
}


} catch (SocketException e) {
flag = false;
if (log.isDebugEnabled())
log.debug("登录ftp服务器 " + ip + " 失败,连接超时!");


throw new Exception("登录ftp服务器失败", e);
} catch (IOException e) {
flag = false;
if (log.isDebugEnabled())
log.debug("登录ftp服务器 " + ip + " 失败,FTP服务器无法打开!", e);


throw new Exception("登录ftp服务器失败", e);
}
}
if (log.isDebugEnabled())
log.debug("登陆ftp服务器成功" + ip);
return flag;

}



      //下载文件

public void downloadFileNew(String remotePath, String localPath, String fileName) throws Exception {
if (log.isDebugEnabled())
log.debug("开始下载文件,remotePath: " + remotePath + " localPath: " + localPath + " fileName: " + fileName);


FileOutputStream fos = null;
try {
ftpClient.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录


File localFile = new File(localPath + File.separator + fileName);
fos = new FileOutputStream(localFile);
ftpClient.retrieveFile(fileName, fos);


if (log.isDebugEnabled())
log.debug("下载文件成功,remotePath: " + remotePath + " localPath: " + localPath + " fileName: " + fileName);


} catch (IOException e) {
log.error("下载文件出错,remotePath: " + remotePath + " localPath: " + localPath + " fileName: " + fileName, e);
throw new Exception("从FTP服务器下载文件出错", e);
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
log.error(e);
}
}


//上传文件

public boolean uploadFile(String localPath, String remotePath) throws Exception {
if (log.isDebugEnabled())
log.debug("开始上传文件,remotePath: " + remotePath + " localPath: " + localPath);

boolean upload = false;
InputStream is = null;
try {
ftpClient.changeWorkingDirectory(remotePath);
is = new FileInputStream(localPath);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
upload = ftpClient.storeFile(localPath, is);

if (log.isDebugEnabled())
log.debug("上传文件成功,remotePath: " + remotePath + " localPath: " + localPath );
} catch (Exception e) {
log.error("上传文件失败,remotePath: " + remotePath + " localPath: " + localPath );
log.error(e);
throw new Exception("上传文件到FTP上出错", e);
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
log.error(e);
}
}
return upload;
}

//删除文件

public void deleteFile(String path) {
try {
ftpClient.deleteFile(path);
} catch (IOException e) {
log.error("删除文件错误:"+path,e);
}
}
//获得所有的文件

public String[] getFileList(String path) {
try {
return ftpClient.listNames(path);
} catch (IOException e) {
log.error(e);
return null;
}
}

//重命名

public void rename(String remotePath, String fromname, String toname) throws Exception {
try {
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.rename(fromname, toname);
if (log.isDebugEnabled())
log.debug("移动文件成功,from: " + fromname + " to: " + toname);
} catch (IOException e) {
log.error("从FTP服务器移动文件出错", e);
throw new Exception("从FTP服务器移动文件出错", e);
}
}


//关闭连接

public void closeConnect() {
if (log.isDebugEnabled())
log.debug("关闭tfp服务器链接……");
try {
if (ftpClient != null) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (Exception e) {
log.error("关闭ftp服务器链接失败", e);
}
if (log.isDebugEnabled())
log.debug("成功关闭tfp服务器链接");
}


 

0 0
原创粉丝点击