import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class FTPTool {
private FTPClient ftp;
/**
*
* @param path
* 上传到ftp服务器哪个路径下
* @param addr
* 地址
* @param port
* 端口号
* @param username
* 用户名
* @param password
* 密码
* @return
* @throws Exception
*/
public synchronized boolean connect(String addr, int port, String path, String username, String password) {
boolean result = false;
try {
ftp = new FTPClient();
int reply;
// ftp.setDataTimeout(30000); //设置传输超时时间为60秒
ftp.setConnectTimeout(30000);
ftp.connect(addr, port);
ftp.login(username, password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(path);
result = true;
} catch (Exception e) {
return false;
}
return result;
}
/**
* 被动模式
* @param path
* 上传到ftp服务器哪个路径下
* @param addr
* 地址
* @param port
* 端口号
* @param username
* 用户名
* @param password
* 密码
* @return
* @throws Exception
*/
public synchronized boolean connectLPM(String addr, int port, String path, String username, String password) {
boolean result = false;
try {
ftp = new FTPClient();
int reply;
// ftp.setDataTimeout(30000); //设置传输超时时间为60秒
ftp.setConnectTimeout(30000);
ftp.connect(addr, port);
ftp.login(username, password);
ftp.enterLocalPassiveMode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(path);
result = true;
} catch (Exception e) {
return false;
}
return result;
}
/**
*
* @param file
* 上传的文件或文件夹
* @throws Exception
*/
public boolean upload(File file, String fileName) throws Exception {
try {
if (file.isDirectory()) {
ftp.makeDirectory(file.getName());
ftp.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, fileName);
ftp.changeToParentDirectory();
} else {
File file2 = new File(file.getPath() + "\\" + files[i]);
FileInputStream input = new FileInputStream(file2);
ftp.enterLocalPassiveMode();
boolean isStoreFlag = ftp.storeFile(file2.getName(), input);
input.close();
return isStoreFlag;
}
}
} else {
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
ftp.enterLocalPassiveMode();
boolean isStoreFlag = ftp.storeFile(fileName, input);
input.close();
return isStoreFlag;
}
ftp.logout();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
return false;
}
public StringBuilder readFile(String fileName) {
StringBuilder sb = null;
if (null != fileName) {
try {
ftp.enterLocalPassiveMode();
InputStream in = ftp.retrieveFileStream(fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line= "";
sb = new StringBuilder(150);
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb;
}
public boolean deleteFile(String path, String fileName) {
boolean result = false;
try {
ftp.deleteFile(path + "/" + fileName);
result = true;
} catch (Exception e) {
return false;
}
return result;
}
public boolean renameFile(String srcFname, String targetFname){
boolean flag = false;
if( ftp!=null ){
try {
flag = ftp.rename(srcFname,targetFname);
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
/**
* 递归遍历出目录下面所有文件
* @param path 需要遍历的目录,必须以"/"开始和结束
* @throws IOException
*/
public List<String> listFiles(String path){
List<String> fileNameList = new ArrayList<String>();
try{
if(path.startsWith("/") && path.endsWith("/")){
String directory = path;
//更换目录到当前目录
boolean flag = ftp.changeWorkingDirectory(directory);
System.out.println(ftp.getStatus());
FTPFile[] files = ftp.listFiles();
for(FTPFile file : files){
if(file.isFile()){
fileNameList.add(directory+file.getName());
}
}
}
}catch (Exception e){
e.printStackTrace();
}
return fileNameList;
}
/**
* 获得某一目录下文件的输入流
* @param channel
* @param dateStr
* @return
*/
public InputStream retrieveFile(String channel, String dateStr){
FTPFile[] ftpFiles = null;
try {
ftpFiles = ftp.listFiles();
InputStream inputStream = null;
for (FTPFile filePath : ftpFiles) {
String fileNameStr = channel + "_" + dateStr + ".xls";
if(filePath.getName().endsWith(fileNameStr)){
inputStream = ftp.retrieveFileStream(filePath.getName());
return inputStream;
}
//遍历ftp目录获取excel文件
// String fileType = filePath.getName().substring(filePath.getName().lastIndexOf("."), filePath.getName().length());
// List<List<String>> lists = new ArrayList<>();
// if (".xls".equals(fileType)) {
// } else if (".xlsx".equals(fileType)) {
// }
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws Exception {
FTPTool t = new FTPTool();
System.out.println(t.connect("172.25.xx.xx", 21, "/", "user", "pwd"));
File file = new File("/Users/jun/Downloads/test/media.ts");
t.upload(file, "media.ts");
// System.out.println(t.readFile("MEDIA1493124001038.xml").toString());
}
}