向ftp服务端上传文件和文件夹-20151201

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import com.sitech.ismp.coll.busi.e2e.rule.AnalysisRule;

/**
* @author smj
* */
public class upToFtp extends AnalysisRule {

public upToFtp(Map<String, String> params) {
super(params);
// TODO Auto-generated constructor stub
}

private FTPClient ftp;
/**
* Description: 向FTP服务器上传文件夹
* @param path 上传到ftp服务器哪个路径下
* @param url FTP服务器地址
* @param port 端口号
* @param username 用户名
* @param password 密码
* @author smj
* @throws Exception
*/
private boolean connect(String path,String url,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
ftp.connect(url,port);
ftp.login(username,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
boolean flag4=ftp.changeWorkingDirectory(path);
logger.info("切换工作目录:"+flag4+"所要切换的路径:"+path);
result = true;
return result;
}
/**
*
* @param file 上传的文件或文件夹
* @throws Exception
*/
private boolean upload(File file) throws Exception{
if(file.isDirectory()){
//ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getPath());
String[] files = file.list();
logger.info("文件列表有:"+files.toString() );
for (int i = 0; i < files.length; i++) {
File file1 = new File(files[i]);
logger.info("我端的文件名:"+file1.getName() + "\t" );
if(file1.isDirectory()){
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2 = new File(files[i]);
logger.info("文件名:"+file2.getName() + "\t" );
FileInputStream input = new FileInputStream(file1);
boolean flag3= ftp.storeFile(file2.getName(), input);
logger.info("文件传输状态"+flag3);
input.close();
}
}
}else{
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
return true;
}

/**
* 移动指定文件或文件夹(包括所有文件和子文件夹)
*
* @param fromDir
* 要移动的文件或文件夹
* @param toDir
* 目标文件夹
* @throws Exception
*/
public void MoveFolderAndFileWithSelf(String from, String to) throws Exception {
try {
File dir = new File(from);

to += File.separator + dir.getName();
File moveDir = new File(to);
if(dir.isDirectory()){
if (!moveDir.exists()) {
moveDir.mkdirs();
}
}else{
File tofile = new File(to);
dir.renameTo(tofile);
return;
}

//System.out.println("dir.isDirectory()"+dir.isDirectory());
//System.out.println("dir.isFile():"+dir.isFile());

// 文件一览
File[] files = dir.listFiles();
if (files == null)
return;

// 文件移动
for (int i = 0; i < files.length; i++) {
System.out.println("文件名:"+files[i].getName());
if (files[i].isDirectory()) {
MoveFolderAndFileWithSelf(files[i].getPath(), to);
// 成功,删除原文件
files[i].delete();
}
File moveFile = new File(moveDir.getPath() + File.separator + files[i].getName());
// 目标文件夹下存在的话,删除
if (moveFile.exists()) {
moveFile.delete();
}
files[i].renameTo(moveFile);
}
//dir.delete();
} catch (Exception e) {
throw e;
}
}

protected void excute() {
logger.info("upToFtp start........");
try {
boolean flag1=connect("/ibnmsftp", "10.145.206.133", 21, "ibnmsftp", "ibnmsftp#2012");
logger.info("连接对端ftp状态为::"+flag1 + "\t" );
File file = new File("/iBNMSConsole/ismp/CRM_DATA/");
logger.info("文件路径为:"+file.getPath() + "文件名字为:" +file.getName()+ "\t" );
boolean flag= upload(file);
logger.info("文件上传状态为:"+flag + "\t" );
if(flag==true){
MoveFolderAndFileWithSelf("/iBNMSConsole/ismp/CRM_DATA/","/iBNMSConsole/ismp/CRM_HIS/");
}
logger.info("upToFtp end........");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.info("上传文件出错");

}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值