import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import com.wrt.zjg.webservices.model.FtpModel;
public class FtpUtil {
private static Logger logger = Logger.getLogger(FtpUtil.class);
private static FTPClient ftp;
/**
* 获取ftp连接
* @param fm
* @return
* @throws Exception
*/
public static boolean connectFtp(FtpModel fm) throws Exception{
ftp = new FTPClient();
boolean flag = false;
int reply;
if(fm.getPort() == null) {
ftp.connect(fm.getIpAddr(), 21);
} else {
ftp.connect(fm.getIpAddr(), fm.getPort());
}
ftp.login(fm.getUserName(), fm.getPwd());
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return flag;
}
ftp.changeWorkingDirectory(fm.getPath());
flag = true;
return flag;
}
/**
* 关闭ftp连接
*/
public static void closeFtp() {
if(ftp != null && ftp.isConnected()) {
try {
ftp.logout();
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* ftp上传文件
* @param file
* @throws Exception
*/
public static void upload(File file) throws Exception {
if(file.isDirectory()) {
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for(String fstr : files) {
File file1 = new File(file.getPath() + "/" + fstr);
if(file1.isDirectory()) {
upload(file1);
ftp.changeToParentDirectory();
} else {
File file2 = new File(file.getPath() + "/" + fstr);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
} else {
File file2= new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
/**
*
* @param fm 将要上传的文件
* @param localBaseDir 本地目录
* @param remoteBaseDir 远程目录
* @throws Exception
*/
public static void startDown(FtpModel fm, String localBaseDir, String remoteBaseDir) throws Exception {
if(FtpUtil.connectFtp(fm)) {
try {
FTPFile[] files = null;
boolean changedir = ftp.changeWorkingDirectory(remoteBaseDir);
if(changedir) {
ftp.setControlEncoding("GBK");
files = ftp.listFiles();
for(int i = 0; i < files.length; i++) {
try {
downloadFile(files[i], localBaseDir, remoteBaseDir);
} catch (Exception e) {
logger.error(e);
logger.error("<"+files[i].getName()+">下载失败");
}
}
}
} catch (Exception e) {
logger.error(e);
logger.error("下载过程中出现异常!");
}
} else {
logger.error("链接失败!");
}
}
/**
* 下载FTP文件
* 当你需要下载FTP文件的时候,调用此方法
* 根据<b>获取的文件名,本地地址,远程地址</b>进行下载
* @param ftpFile
* @param relativeLocalPath
* @param relativeRemotePath
*/
public static void downloadFile(FTPFile ftpFile, String relativeLocalPath, String relativeRemotePath) {
if(ftpFile.isFile()) {
if(ftpFile.getName().indexOf("?") == -1) {
OutputStream outputStream = null;
try {
File locaFile = new File(relativeLocalPath + ftpFile.getName());
if(locaFile.exists()) {
return;
} else {
outputStream = new FileOutputStream(relativeLocalPath + ftpFile.getName());
ftp.retrieveFile(ftpFile.getName(), outputStream);
outputStream.flush();
outputStream.close();
}
} catch (Exception e) {
logger.error(e);
} finally {
try {
if(outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
logger.error("输出文件流异常");
}
}
}
} else {
String newLocalRelatePath = relativeLocalPath + ftpFile.getName();
String newRemote = new String (relativeLocalPath + ftpFile.getName().toString());
File file = new File(newLocalRelatePath);
if(!file.exists()) {
file.mkdirs();
}
try {
newLocalRelatePath = newLocalRelatePath + "/";
newRemote = newRemote + "/";
String currentWorkDir = ftpFile.getName().toString();
boolean changedir = ftp.changeWorkingDirectory(currentWorkDir);
if(changedir) {
FTPFile[] files = null;
files = ftp.listFiles();
for(int i = 0; i < files.length; i++) {
downloadFile(files[i], newLocalRelatePath, newRemote);
}
}
if(changedir) {
ftp.changeToParentDirectory();
}
} catch (Exception e) {
logger.error(e);
}
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FtpModel fm = new FtpModel();
fm.setIpAddr("192.168.1.252");
fm.setUserName("ftp");
fm.setPwd("ftp12345");
fm.setPath("/temp/zjg/messageXML/");
FtpUtil.connectFtp(fm);
File file = new File("E:/test/959663081-20170606154839294.xml");
FtpUtil.upload(file);
}
}