依赖架包
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
工具类
package com.chinamobile.util;
import java.io.*;
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;
public class FtpUtils {
private static final Logger logger = Logger.getLogger(FtpUtils.class);
/**
* @param path 上传到ftp服务器哪个路径下
* @param addr 地址
* @param port 端口号
* @param username 用户名
* @param password 密码
* @return
* @throws Exception
*/
public static FTPClient connect(String path, String addr, int port, String username, String password) throws IOException {
FTPClient ftpClient = new FTPClient();
int reply;
try {
ftpClient.connect(addr, port);
ftpClient.login(username, password);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return null;
}
ftpClient.changeWorkingDirectory(path);
} catch (IOException e) {
logger.debug("连接ftp服务出错", e);
throw e;
}
return ftpClient;
}
/**
* @param file 上传的文件或文件夹
* @param path 上传的文件的路径
* @return void
* @throws :(Error note)
* @throws Exception
* @author
* @class ItemFtp
* @title upload
* @Description :
* @time 2013 2013-11-27
*/
public static void upload(File file, String path, FTPClient ftpClient) throws IOException {
logger.debug(" file.isDirectory() : " + file.isDirectory());
InputStream input =null;
try {
ftpClient.enterLocalPassiveMode();
//切换工作目录
ftpClient.changeWorkingDirectory(path);
// ftpClient.makeDirectory(file.getName());
if (file.isDirectory()) {
ftpClient.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath() + "\\" + files[i]);
if (file1.isDirectory()) {
upload(file1, path, ftpClient);
ftpClient.changeToParentDirectory();
} else {
File file2 = new File(file.getPath() + "\\" + files[i]);
input = new FileInputStream(file2);
ftpClient.storeFile(file2.getName(), input);
}
}
} else {
File file2 = new File(file.getPath());
System.out.println(" file.getPath() : " + file.getPath() + " | file2.getName() : " + file2.getName());
input = new FileInputStream(file2);
// ftpClient.changeWorkingDirectory(path);
boolean result=ftpClient.storeFile(file2.getName(), input);
if(result){
logger.debug("ftp上传文件成功:"+file2.getName());
}else{
logger.debug("ftp上传文件失败:"+file2.getName());
}
}
} catch (IOException e) {
logger.debug("ftp上传文件出错", e);
throw e;
}finally{
if(input!=null){
input.close();
}
}
}
/**
* @param reomvepath 下载的文件的路径
* @param fileName 下载的文件名
* @param localPath 下载的文件本地路径
* @return void
* @throws :(Error note)
* @throws Exception
* @author
* @class ItemFtp
* @title download
* @Description : FPT 下载文件方法
* @time 2013 2013-11-27
*/
public static void download(String reomvepath, String fileName, String localPath, FTPClient ftpClient) throws IOException {
OutputStream is =null;
try {
ftpClient.changeWorkingDirectory(reomvepath);
// 列出该目录下所有文件
FTPFile[] fs = ftpClient.listFiles();
// 遍历所有文件,找到指定的文件
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
// 根据绝对路径初始化文件
File localFile = new File(localPath + "/" + ff.getName());
// 输出流
is= new FileOutputStream(localFile);
// 下载文件
ftpClient.retrieveFile(ff.getName(), is);
System.out.println("下载成功!");
}
}
} catch (IOException e) {
logger.debug("ftp下载文件出错", e);
throw e;
}finally{
if(is!=null){
is.close();
}
}
}
/**
* @param srcFname 被修改的文件
* @param targetFname 修改成的文件名
* @param ftpClient ftp连接
* @return
*/
public static void renameFile(String srcFname, String targetFname, FTPClient ftpClient) throws IOException {
if (ftpClient != null) {
try {
ftpClient.rename(srcFname, targetFname);
} catch (IOException e) {
logger.debug("ftp修改文件名出错", e);
throw e;
}
}
}
/**
* @param ftpClient ftp连接
* @return
*/
public static void close(FTPClient ftpClient) throws IOException {
if (ftpClient != null) {
try {
if (ftpClient.isConnected()) {
ftpClient.logout(); //退出连接
ftpClient.disconnect();
}
} catch (IOException e) {
logger.debug("ftp关闭退出出错", e);
throw e;
}
}
}
/**
* 列出目录下的文件
* @param directory 要列出的目录
*/
public static FTPFile[] listFiles(FTPClient ftpClient, String directory) throws IOException {
FTPFile[] fileList = null;
try {
ftpClient.enterLocalPassiveMode();
fileList = ftpClient.listFiles(directory);
} catch (Exception e) {
logger.debug("ftp获取文件列表出错", e);
throw e;
}
return fileList;
}
public static void delete(String path, String fileName, FTPClient ftpClient) throws IOException {
try{
ftpClient.changeWorkingDirectory(path);
ftpClient.deleteFile(fileName);
}catch(Exception e){
logger.debug("ftp删除文件出错", e);
throw e;
}
}
调用
public String getStatementOfAccountFileFromFtp(String time) {
FTPClient ftpClient = FtpUtils.connect(path, host, Integer.parseInt(port), username, password);
String fileName=null;
try {
if (ftpClient != null) {
//要下载的文件名?(事先无法确定)
StringBuffer sb=new StringBuffer("M2M_WCDZ_");
sb.append(time);
//列出文件夹下的所有文件
FTPFile[] fileList=FtpUtils.listFiles(ftpClient,path);
for(int i=0;i<fileList.length;i++){
if(fileList[i].getName().startsWith(sb.toString())){
fileName=fileList[i].getName();
break;
}
}
if(!StringUtils.isEmpty(fileName)){
FtpUtils.download(path,fileName,localPath ,ftpClient);
//删除文件
FtpUtils.delete(path,fileName,ftpClient);
}
}
} catch (Exception e) {
logger.error("cmiot下载文件异常",e);
e.printStackTrace();
} finally {
if(ftpClient != null){
FtpUtils.close(ftpClient);
}
}
return fileName;
}
下一篇 工具类 (二) 操作 sftp