package com.chinalife.cbdp.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FTPUtil {
/**
* 获取FTPClient对象
* @param ftpHost FTP主机服务器
* @param ftpPassword FTP 登录密码
* @param ftpUserName FTP登录用户名
* @param ftpPort FTP端口 默认为21
* @return
*/
public FTPClient getFTPClient(String ftpHost,int ftpPort,String ftpUserName,String ftpPassword) {
FTPClient ftpClient = null;
try {
ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
System.out.println("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
}
} catch (SocketException e) {
e.printStackTrace();
System.out.println("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
System.out.println("FTP的端口错误,请正确配置。");
}
return ftpClient;
}
/**
* 本地上传文件到FTP服务器
*
* @param fileName 要上传文件的文件名
*
* @throws IOException
*/
public boolean upload(FTPClient ftpClient,String ftpPath,String localFilePath,String fileName) {
try {
// 设置PassiveMode传输
ftpClient.enterLocalPassiveMode();
// 设置以二进制流的方式传输
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 中文支持
ftpClient.setControlEncoding("UTF-8");
//先创建文件夹
boolean dir = mkdir(ftpClient,ftpPath);
if(!dir){
return false;
}
// 切换目录
ftpClient.changeWorkingDirectory(ftpPath);
// 查找是否存在文件,存在删除
FTPFile[] files = ftpClient.listFiles(fileName);
System.out.println(files.length);
if(files.length!=0){
for(FTPFile f:files){
ftpClient.deleteFile(f.getName());
}
}
//如果本地文件不存在直接返回false
File f = new File(localFilePath+File.separator+fileName);
//File f = new File("D:\\home\\logs\\info.log");
if(!f.exists()){
return false;
}
//本地文件地址
InputStream in = new FileInputStream(f);
//new String((ftpPath+File.separator+fileName).getBytes("utf-8"),"iso-8859-1")
System.out.println(ftpPath+"/"+fileName );
boolean store = ftpClient.storeFile(new String((ftpPath+"/"+fileName).getBytes("utf-8"),"iso-8859-1") , in);
if(store){
return true;
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean deleteFile(FTPClient ftpClient,String ftpPath,String fileName){
try {
ftpClient.changeWorkingDirectory(ftpPath);
FTPFile[] files = ftpClient.listFiles(fileName);
for(FTPFile f:files){
ftpClient.deleteFile(f.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
String[] paths = ftpPath.split("/");
String path="/";
for(int i=1;i<paths.length;i++){
path+= paths[i]+"/";
try {
boolean b = ftpClient.makeDirectory(path);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FTPUtil {
/**
* 获取FTPClient对象
* @param ftpHost FTP主机服务器
* @param ftpPassword FTP 登录密码
* @param ftpUserName FTP登录用户名
* @param ftpPort FTP端口 默认为21
* @return
*/
public FTPClient getFTPClient(String ftpHost,int ftpPort,String ftpUserName,String ftpPassword) {
FTPClient ftpClient = null;
try {
ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
System.out.println("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
}
} catch (SocketException e) {
e.printStackTrace();
System.out.println("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
System.out.println("FTP的端口错误,请正确配置。");
}
return ftpClient;
}
/**
* 本地上传文件到FTP服务器
*
* @param fileName 要上传文件的文件名
*
* @throws IOException
*/
public boolean upload(FTPClient ftpClient,String ftpPath,String localFilePath,String fileName) {
try {
// 设置PassiveMode传输
ftpClient.enterLocalPassiveMode();
// 设置以二进制流的方式传输
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 中文支持
ftpClient.setControlEncoding("UTF-8");
//先创建文件夹
boolean dir = mkdir(ftpClient,ftpPath);
if(!dir){
return false;
}
// 切换目录
ftpClient.changeWorkingDirectory(ftpPath);
// 查找是否存在文件,存在删除
FTPFile[] files = ftpClient.listFiles(fileName);
System.out.println(files.length);
if(files.length!=0){
for(FTPFile f:files){
ftpClient.deleteFile(f.getName());
}
}
//如果本地文件不存在直接返回false
File f = new File(localFilePath+File.separator+fileName);
//File f = new File("D:\\home\\logs\\info.log");
if(!f.exists()){
return false;
}
//本地文件地址
InputStream in = new FileInputStream(f);
//new String((ftpPath+File.separator+fileName).getBytes("utf-8"),"iso-8859-1")
System.out.println(ftpPath+"/"+fileName );
boolean store = ftpClient.storeFile(new String((ftpPath+"/"+fileName).getBytes("utf-8"),"iso-8859-1") , in);
if(store){
return true;
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean deleteFile(FTPClient ftpClient,String ftpPath,String fileName){
try {
ftpClient.changeWorkingDirectory(ftpPath);
FTPFile[] files = ftpClient.listFiles(fileName);
for(FTPFile f:files){
ftpClient.deleteFile(f.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
//动态创建目录
public boolean mkdir(FTPClient ftpClient,String ftpPath){String[] paths = ftpPath.split("/");
String path="/";
for(int i=1;i<paths.length;i++){
path+= paths[i]+"/";
try {
boolean b = ftpClient.makeDirectory(path);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
}