`
wuxshhappy
  • 浏览: 2826 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

org.apache.commons.net.ftp的使用

阅读更多
1、设置FTP 属性:用户名、密码、IP、端口、字符集等,利用properties文件实现动态配置
2、创建FTP连接:
private boolean connectServer() throws Exception {
boolean flag = true;
int reply;
if (this.ftpClient == null) {
try {
this.ftpClient = new FTPClient();
// 设置文件传输格式
this.ftpClient.setControlEncoding(this.charset);
// 设置FTP服务器端口
this.ftpClient.setDefaultPort(this.port);
// 设置FTP服务器IP
this.ftpClient.connect(this.ip);

log.info("Connected to " + this.ip);
// 登陆FTP服务器
boolean isLogin=this.ftpClient.login(this.userName, this.password);
if(isLogin){
log.info(this.userName+" is login FTP "+this.ip);
}else{
log.error(this.userName+" login FTP server refused");
}

// 设置FTP传输二进制文件
boolean setFtpType=this.ftpClient.setFileType(this.ftpClient.BINARY_FILE_TYPE);
reply = this.ftpClient.getReplyCode();
this.ftpClient.setDefaultTimeout(180000);
if (!FTPReply.isPositiveCompletion(reply)) {
this.ftpClient.disconnect();
log.info("FTP server refused connection.");
flag = false;
}
} catch (SocketException e) {
// TODO Auto-generated catch block
log.error("创建FTP连接失败\n" + e.toString());
throw e;
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("创建FTP连接失败\n" + e.toString());
throw e;
}
}
return flag;
}
3、远程创建文件夹,(需递归创建)
/**
* 创建远程文件夹
*
* @param remotePath:FTP服务器的根目录
* @param dicName:文件存储目录(多级)
* @return
* @throws Exception
*/
private boolean createDics(String remotePath, String dicName)
throws Exception {
FTPFile[] files;
String[] root = remotePath.split("/");
String[] dics = dicName.split("/");
List dicList = new LinkedList();
if (dics != null) {
for (int i = 0; i < dics.length; i++) {
dicList.add(dics[i]);
}
}
if (root != null) {
for (int i = 0; i < root.length; i++) {
String rootName = root[i];
log.info("FTP 根目录:"+rootName);
ftpClient.changeWorkingDirectory(rootName);
}
}
return this.createDic(dicList);
}

/**
* 递归创建远程文件夹
*
* @param dics:存储文件夹名(一级)
* @return
*/
private boolean createDic(List dics) {
boolean isCreated = false;
try {
if (dics == null || dics.size() == 0) {
return true;
} else {
FTPFile[] files = this.ftpClient.listFiles();
String dicName = dics.get(0).toString();
log.info("文件夹:"+dicName);
if (files == null || files.length == 0) {
isCreated = this.ftpClient.makeDirectory(dicName);
} else {
for (int i = 0; i < files.length; i++) {

FTPFile ftpFile = files[i];
String ftpName = ftpFile.getName();
if (ftpName.equals(dicName)) {
isCreated = true;
break;
}
}
if (!isCreated) {
isCreated = this.ftpClient.makeDirectory(dicName);
}
log.info("FTP dic iscreated:"+isCreated);
}
if (isCreated) {
boolean isChanged = this.ftpClient
.changeWorkingDirectory(dicName);
log.info("FTP dic isChanged:"+isChanged);
dics.remove(0);
if (isChanged) {
haveDic = createDic(dics);
} else {
return false;
}
} else {
return false;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
log.error(e.toString());
return false;
}
return isCreated;
}
4、上传文件到指定目录
private boolean uploadFile(String fileLocalPath, String fileRemotePath) {
FileInputStream fis = null;
boolean isUpload = false;
try {
String dicName = this.getDicName(fileRemotePath);
log.info("FTP 文件夹:"+dicName);
String fileName = fileRemotePath.replace(dicName, "");
fileName = fileName.replace("/", "");
log.info("FTP 文件名:"+fileName);
createDics(this.rootPath, dicName);
if (this.haveDic) {
fis = new FileInputStream(fileLocalPath);
isUpload = this.ftpClient.storeFile(fileName, fis);
} else {
throw new Exception("没有相对应的文件夹");
}
} catch (IOException e) {
// TODO Auto-generated catch block
if (fis != null) {
try {
fis.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
log.error("上传文件失败\n" + e.toString());
isUpload = false;
} catch (Exception e) {
// TODO Auto-generated catch block
log.error("上传文件失败\n" + e.toString());
isUpload = false;
}
return isUpload;
}
5、关闭FTP连接
private void closeConnect() {
if (this.ftpClient != null) {
try {
this.ftpClient.logout();
this.ftpClient.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("关闭Ftp连接失败!\n" + e.toString());
}

}
}
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics