import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
/**
*
* 目的:FTP文件类
* <br/>
*
* FTP上传文件、下载文件
*
* *
*/
public class FTPUtil {
private static final Log log = LogFactory.getLog(FTPUtil.class);
public static int download(String ip,int port,String userName,String password,FileFilter filter,String localFilePath) throws SocketException, IOException{
return download(ip, port, userName, password, null,filter, localFilePath);
}
public static int download(String ip,int port,String userName,String password,String ftpPath,FileFilter filter,String localFilePath) throws SocketException, IOException{
FTPClient ftp = new FTPClient();
int i=0;
try {
// 不为空连接 通过IP 和端口
ftp.connect(ip, port);
// 登陆
if (ftp.login(userName, password)) {
ftp.enterLocalPassiveMode();
// 获取FTP登陆目录下的所有文件
if(ftpPath!=null){
if(!ftp.changeWorkingDirectory(ftpPath)){
throw new RuntimeException("找不到该目录:"+ftpPath);
}
}
FTPFile[] files = ftp.listFiles();
for (FTPFile file : files) {
String fileName = file.getName();
if (filter.accept(fileName)) {
BufferedOutputStream out = null;
try {
// IO流下载文件到本地
out = new BufferedOutputStream(
new FileOutputStream(new File(localFilePath
,fileName)));
// 开始下载
ftp.retrieveFile(file.getName(), out);
log.info("下载文件:" + file.getName() + "到本地路径:"
+ localFilePath);
i++;
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
log.error("", e);
}
}
}
}
} else {
i = -1;
}
} finally {
if(ftp != null&&ftp.isConnected()){
try {
log.info("关闭ftp连接");
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return i;
}
public static int download(String ip,int port,String userName,String password,String ftpPath,final String fileName,String localFilePath)throws SocketException, IOException{
return download(ip, port, userName, password,ftpPath,new FileFilter(){
public boolean accept(String fileName1) {
return fileName.equalsIgnoreCase(fileName1);
}
}, localFilePath);
}
public static int download(String ip,int port,String userName,String password,final String fileName,String localFilePath)throws SocketException, IOException{
return download(ip, port, userName, password,null,fileName, localFilePath);
}
/**
* ftp上传文件至服务器
* @throws SocketException
* @throws IOException
*/
public static void uploadFile(String ip, int port, String userName, String password, String localFile)
throws SocketException, IOException{
log.info("上传本地文件: " + localFile);
File file = new File(localFile);
InputStream in = null;
FTPClient ftpClient = null;
if(file != null){
try {
ftpClient = new FTPClient();
in = new FileInputStream(file);
ftpClient.connect(ip,port);
if(ftpClient.login(userName, password)){
ftpClient.enterLocalPassiveMode();
boolean flag= ftpClient.appendFile(file.getName(), in);
log.info("上传文件成功:" + flag);
}
}catch (SocketException e) {
log.error("ftp上传文件失败:", e);
throw e;
}catch (IOException e) {
log.error("ftp上传文件失败:", e);
throw e;
} finally{
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ftpClient != null){
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public static void upload(String ip, int port, String userName, String password, String path,String remotePath)
throws SocketException, IOException{
upload(ip, port, userName, password, new File(path), remotePath);
}
/**
* ftp上传文件至服务器
* @throws SocketException
* @throws IOException
*/
public static void upload(String ip, int port, String userName, String password, File localFile,String remotePath)
throws SocketException, IOException{
log.info("上传本地文件: " + localFile.getName());
InputStream in = null;
FTPClient ftpClient = null;
if(localFile != null){
try {
ftpClient = new FTPClient();
in = new FileInputStream(localFile);
ftpClient.connect(ip,port);
if(ftpClient.login(userName, password)){
ftpClient.enterLocalPassiveMode();
if(remotePath!=null){
if(!ftpClient.changeWorkingDirectory(remotePath)){
ftpClient.makeDirectory(remotePath);
ftpClient.changeWorkingDirectory(remotePath);
}
}
boolean flag= ftpClient.storeFile(localFile.getName(), in);
log.info("上传文件成功:" + flag);
}
}catch (SocketException e) {
log.error("ftp上传文件失败:", e);
throw e;
}catch (IOException e) {
log.error("ftp上传文件失败:", e);
throw e;
} finally{
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ftpClient != null){
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
posted on 2011-01-21 12:01
David1228 阅读(2683)
评论(0) 编辑 收藏 所属分类:
J2EE