FtpUtil
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;
/**
* @author E0395
*
*/
public class FtpUtil {
private static Logger logger = Logger.getLogger(FtpUtil.class);
private static FTPClient ftp;
/**
* 获取ftp连接
* @param f
* @return
* @throws Exception
*/
public static boolean connectFtp(Ftp f) throws Exception{
ftp=new FTPClient();
boolean flag=false;
int reply;
if (f.getPort()==null) {
ftp.connect(f.getIpAddr(),21);
}else{
ftp.connect(f.getIpAddr(),f.getPort());
}
ftp.login(f.getUserName(), f.getPwd());
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return flag;
}
ftp.changeWorkingDirectory(f.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 f
* @throws Exception
*/
public static void upload(File f) throws Exception{
ftp.enterLocalPassiveMode();
if (f.isDirectory()) {
ftp.makeDirectory(f.getName());
ftp.changeWorkingDirectory(f.getName());
String[] files=f.list();
for(String fstr : files){
File file1=new File(f.getPath()+"/"+fstr);
if (file1.isDirectory()) {
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2=new File(f.getPath()+"/"+fstr);
FileInputStream input=new FileInputStream(file2);
ftp.storeFile(file2.getName(),input);
input.close();
}
}
}else{
File file2=new File(f.getPath());
FileInputStream input=new FileInputStream(file2);
ftp.storeFile(file2.getName(),input);
input.close();
}
}
/**
* 下载链接配置
* @param f
* @param localBaseDir 本地目录
* @param remoteBaseDir 远程目录
* @throws Exception
*/
public static void startDown(Ftp f,String localBaseDir,String remoteBaseDir,String fileName) throws Exception{
if (FtpUtil.connectFtp(f)) {
try {
FTPFile[] files = null;
ftp.enterLocalPassiveMode();//本地模式
boolean changedir = ftp.changeWorkingDirectory(remoteBaseDir);
if (changedir) {
ftp.setControlEncoding("GBK");
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
files = ftp.listFiles();
for (int i = 0; i < files.length; i++) {
try{
if(files[i].getName().equals(fileName)) {
downloadFile(files[i], localBaseDir, remoteBaseDir);
}
}catch(Exception e){
logger.error(e);
logger.error("<"+files[i].getName()+">下载失败");
e.printStackTrace();
}
}
}
} catch (Exception e) {
logger.error(e);
logger.error("下载过程中出现异常");
e.printStackTrace();
}
}else{
logger.error("链接失败!");
System.out.println("链接失败!");
}
}
/**
*
* 下载FTP文件
* 当你需要下载FTP文件的时候,调用此方法
* 根据<b>获取的文件名,本地地址,远程地址</b>进行下载
*
* @param ftpFile
* @param relativeLocalPath
* @param relativeRemotePath
*/
private static void downloadFile(FTPFile ftpFile, String relativeLocalPath,String relativeRemotePath) {
System.out.println("开始下载");
if (ftpFile.isFile()) {
InputStream in = null ;
FileOutputStream bos = null;
try {
in = ftp.retrieveFileStream(ftpFile.getName());
File file = new File(relativeLocalPath + ftpFile.getName());
file.getParentFile().mkdirs();
bos = new FileOutputStream(file);
byte[] len= new byte[1024*2];
int read = 0;
while((read = in.read(len)) != -1) {
bos.write(len,0,read);
}
} catch (FileNotFoundException e) {
logger.error("文件未找到",e);
e.printStackTrace();
} catch (IOException e) {
logger.error("IO异常",e);
e.printStackTrace();
}finally {
try {
if(in!=null) {
in.close();
}
if(bos!=null) {
bos.close();
}
} catch (IOException e) {
logger.error("关闭异常",e);
e.printStackTrace();
}
}
} else {
String newlocalRelatePath = relativeLocalPath + ftpFile.getName();
String newRemote = new String(relativeRemotePath+ ftpFile.getName().toString());
File fl = new File(newlocalRelatePath);
if (!fl.exists()) {
fl.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);
e.printStackTrace();
}
}
System.out.println("下载完毕");
}
// public static void main(String[] args) throws Exception{
// Ftp f=new Ftp();
// f.setIpAddr("10.12.100.95");
// f.setUserName("silu");
// f.setPwd("silu@12345");
// FtpUtil.connectFtp(f);
File file = new File("E:/ftp/tpxw/201902/123.png");
FtpUtil.upload(file);//把文件上传在ftp上 \pub\nwzd\qjlm\tpxw\to-left - 副本.png
// FtpUtil.startDown(f, "e:\\ftp\\test\\", "/nwzd/qjlm/ztzl","rss_40.xml");//下载ftp文件测试
// System.out.println("下载完成");
// }
}
Ftp
public class Ftp {
private String ipAddr;//ip地址
private Integer port;//端口号
private String userName;//用户名
private String pwd;//密码
private String path;//aaa路径
public String getIpAddr() {
return ipAddr;
}
public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}