package com.ftpfile;
|
|
import android.util.Log;
|
|
import java.io.IOException;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import org.apache.commons.net.ftp.FTPClient;
|
import org.apache.commons.net.ftp.FTPFile;
|
import org.apache.commons.net.ftp.FTPReply;
|
|
|
public class FTPFileUtil {
|
public static final String TAG = "FTPFileUtil";
|
|
private static String FileRoot = "/pub/USER_DATA/"; //数据根目录
|
public String battName = "";
|
public FTPClient ftp;
|
public ArrayList<String> arFiles;
|
|
public String server_ip = "192.168.7.130"; //设备ip地址
|
public String userNmae = "ftp"; //默认ftp用户名
|
public String passWord = ""; //默认用户密码
|
public int port = 21; //ftp默认端口
|
|
|
/**
|
* 读取所有的电池组
|
* @return
|
*/
|
public List<MyFTPFile> readAllBattt(String server_ip) {
|
List<MyFTPFile> files = new ArrayList<>();
|
Integer parentId=1;//根目录
|
Integer id=2;//变量需要
|
try {
|
if(login(server_ip, port, userNmae, passWord)) {
|
Log.e(TAG, "readAllBattt: &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" );
|
ListFileRoot(FileRoot, files, 1, 1);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
if(ftp != null) {
|
try {
|
ftp.disconnect();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
Log.e(TAG, "readAllBattt: #####################"+files.size() );
|
return files;
|
}
|
|
/**
|
* 获取当前电池组所有的历史放电数据
|
* @return
|
*/
|
public List<MyFTPFile> readAllTestData(String battName) {
|
List<MyFTPFile> files = new ArrayList<>();
|
Integer parentId=1;//根目录
|
Integer id=2;//变量需要
|
try {
|
|
System.out.println(FileRoot+battName+"//");
|
if(login(server_ip, port, userNmae, passWord)) {
|
ListBattTestData(FileRoot+battName+"/", files, 1, 1);
|
}
|
disConnection();
|
} catch (IOException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
return files;
|
|
}
|
|
/**
|
* 解析指定的放电数据
|
*/
|
public void readBattTestData(String battName,String fileName) {
|
try {
|
if(login(server_ip, port, userNmae, passWord)) {
|
//ReadBattTestData(FileRoot+battName+"/",fileName);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
/**
|
* 重载构造函数
|
*
|
*
|
*/
|
public FTPFileUtil() {
|
ftp = new FTPClient();
|
arFiles = new ArrayList<String>();
|
// if (isPrintCommmand) {
|
// ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
|
// }
|
}
|
|
/**
|
* 登陆FTP服务器
|
*
|
* @param host FTPServer IP地址
|
* @param port FTPServer 端口
|
* @param username FTPServer 登陆用户名
|
* @param password FTPServer 登陆密码
|
* @return 是否登录成功
|
* @throws IOException
|
*/
|
public boolean login(String host, int port, String username, String password) throws IOException {
|
this.ftp.connect(host, port);
|
if (FTPReply.isPositiveCompletion(this.ftp.getReplyCode())) {
|
if (this.ftp.login(username, password)) {
|
/**
|
需要注意这句代码,如果调用List()方法出现,文件的无线递归,与真实目录结构不一致的时候,可能就是因为转码后,读出来的文件夹与正式文件夹字符编码不一致所导致。
|
则需去掉转码,尽管递归是出现乱码,但读出的文件就是真实的文件,不会死掉。等读完之后再根据情况进行转码。
|
如果ftp部署在windows下,则:
|
for (String arFile : f.arFiles) {
|
arFile = new String(arFile.getBytes("iso-8859-1"), "GBK");
|
logger.info(arFile);
|
}
|
*/
|
ftp.enterLocalPassiveMode(); //设置成被动模式
|
this.ftp.setControlEncoding("utf-8");
|
return true;
|
}
|
}
|
if (this.ftp.isConnected()) {
|
this.ftp.disconnect();
|
}
|
return false;
|
}
|
|
/**
|
* 关闭数据链接
|
*
|
* @throws IOException
|
*/
|
public void disConnection() throws IOException {
|
if (this.ftp.isConnected()) {
|
this.ftp.disconnect();
|
}
|
}
|
|
public List<MyFTPFile> setFileAttributeList(List<MyFTPFile> fileAttributeList,MyFTPFile fileAttribute,FTPFile file,String filePath){
|
fileAttribute.setFileName(file.getName());
|
if (file.getName().indexOf(".")>0){
|
fileAttribute.setFileType(file.getName().substring(file.getName().lastIndexOf(".")+1));
|
}
|
fileAttribute.setFileSize(file.getSize());
|
fileAttribute.setFileCreateTime(file.getTimestamp().getTime());
|
fileAttribute.setFilePath(filePath+file.getName());
|
fileAttribute.setCreateTime(new Date());
|
fileAttributeList.add(fileAttribute);
|
return fileAttributeList;
|
}
|
|
|
|
/**
|
* 递归遍历出目录下面所有文件
|
*
|
* @param pathName 需要遍历的目录,必须以"/"开始和结束
|
* @throws IOException
|
*/
|
public void ListFileRoot(String pathName, List<MyFTPFile> fileAttributeList,Integer parentId,Integer id) throws IOException {
|
if (pathName.startsWith("/") && pathName.endsWith("/")) {
|
//更换目录到当前目录
|
this.ftp.changeWorkingDirectory(pathName);
|
FTPFile[] files = this.ftp.listFiles();
|
List<FileAttribute> dirfileAttributeList =new ArrayList<>();
|
for (FTPFile file : files) {
|
//System.out.println("12");
|
MyFTPFile fileAttribute=new MyFTPFile();
|
fileAttribute.setParentId(parentId);
|
if (file.isFile()) {
|
|
fileAttribute.setIsfile(1);
|
//组装文件类
|
// setFileAttributeList(fileAttributeList,fileAttribute,file,pathName);
|
//System.out.println(file.getRawListing());
|
// arFiles.add(pathName + file.getName());
|
} else if (file.isDirectory()) {
|
id++;
|
//System.out.println(file.getRawListing());
|
fileAttribute.setFileId(id);
|
fileAttribute.setFileName(file.getName());
|
fileAttribute.setIsfile(0);
|
fileAttribute.setFileCreateTime(file.getTimestamp().getTime());
|
fileAttribute.setFilePath(pathName+file.getName());
|
fileAttribute.setCreateTime(new Date());
|
fileAttributeList.add(fileAttribute);
|
//dirfileAttributeList.add(fileAttribute);
|
|
}
|
}
|
|
if (dirfileAttributeList!=null&&dirfileAttributeList.size()>0){
|
for ( FileAttribute fileAttribute :dirfileAttributeList){
|
//save(fileAttribute);保存数据库返回主键id
|
parentId=fileAttribute.getFileId();
|
// 需要加此判断。否则,ftp默认将‘项目文件所在目录之下的目录(./)’与‘项目文件所在目录向上一级目录下的目录(../)’都纳入递归,这样下去就陷入一个死循环了。需将其过滤掉。
|
if (!".".equals(fileAttribute.getFileName()) && !"..".equals(fileAttribute.getFileName())) {
|
List(pathName + fileAttribute.getFileName() + "/",fileAttributeList,parentId,id);
|
}
|
}
|
}
|
|
}
|
}
|
|
/**
|
* 递归遍历出目录下面所有测试文件
|
*
|
* @param pathName 需要遍历的目录,必须以"/"开始和结束
|
* @throws IOException
|
*/
|
public void ListBattTestData(String pathName, List<MyFTPFile> fileAttributeList,Integer parentId,Integer id) throws IOException {
|
if (pathName.startsWith("/") && pathName.endsWith("/")) {
|
//更换目录到当前目录
|
this.ftp.changeWorkingDirectory(pathName);
|
FTPFile[] files = this.ftp.listFiles();
|
for (FTPFile file : files) {
|
MyFTPFile fileAttribute=new MyFTPFile();
|
fileAttribute.setParentId(parentId);
|
if (file.isFile() && file.getName().endsWith(".FBO")) {
|
fileAttribute.setIsfile(1);
|
System.out.println(file.getName());
|
|
//组装文件类
|
setFileAttributeList(fileAttributeList,fileAttribute,file,pathName);
|
//System.out.println(file.getRawListing());
|
arFiles.add(pathName + file.getName());
|
}
|
}
|
System.out.println(arFiles.size());
|
}
|
}
|
|
/**
|
* 解析指定的历史放电数据
|
*
|
* @param pathName 需要遍历的目录,必须以"/"开始和结束
|
* @throws IOException
|
*/
|
// public void ReadBattTestData(String filePath,String fileName) throws IOException {
|
// System.out.println(filePath);
|
//
|
// if (filePath.startsWith("/") && filePath.endsWith("/")) {
|
// //更换目录到当前目录
|
// this.ftp.changeWorkingDirectory(filePath);
|
//
|
// InputStream ins = null;
|
// try {
|
//
|
// // 从服务器上读取指定的文件
|
// ins = this.ftp.retrieveFileStream(fileName);
|
// //System.out.println(ins);
|
// FboDataInf data_inf = new FboDataInf();
|
// ArrayList<FboData> al_fbo_data = new ArrayList<FboData>();
|
// FboData.checkFboFile(ins, data_inf, al_fbo_data);
|
//
|
// System.out.println(data_inf);
|
// System.out.println("88888===="+al_fbo_data.size());
|
// // 主动调用一次getReply()把接下来的226消费掉. 这样做是可以解决这个返回null问题
|
// this.ftp.getReply();
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// }
|
// }
|
// }
|
|
|
/**
|
* 递归遍历出目录下面所有文件
|
*
|
* @param pathName 需要遍历的目录,必须以"/"开始和结束
|
* @throws IOException
|
*/
|
public void List(String pathName, List<MyFTPFile> fileAttributeList,Integer parentId,Integer id) throws IOException {
|
if (pathName.startsWith("/") && pathName.endsWith("/")) {
|
//更换目录到当前目录
|
this.ftp.changeWorkingDirectory(pathName);
|
FTPFile[] files = this.ftp.listFiles();
|
List<MyFTPFile> dirfileAttributeList =new ArrayList<>();
|
for (FTPFile file : files) {
|
MyFTPFile fileAttribute=new MyFTPFile();
|
fileAttribute.setParentId(parentId);
|
if (file.isFile()) {
|
fileAttribute.setIsfile(1);
|
//组装文件类
|
setFileAttributeList(fileAttributeList,fileAttribute,file,pathName);
|
System.out.println(file.getRawListing());
|
// arFiles.add(pathName + file.getName());
|
} else if (file.isDirectory()) {
|
id++;
|
System.out.println(file.getRawListing());
|
fileAttribute.setFileId(id);
|
fileAttribute.setFileName(file.getName());
|
fileAttribute.setIsfile(0);
|
fileAttribute.setFileCreateTime(file.getTimestamp().getTime());
|
fileAttribute.setFilePath(pathName+file.getName());
|
fileAttribute.setCreateTime(new Date());
|
fileAttributeList.add(fileAttribute);
|
dirfileAttributeList.add(fileAttribute);
|
|
}
|
}
|
|
if (dirfileAttributeList!=null&&dirfileAttributeList.size()>0){
|
for ( MyFTPFile fileAttribute :dirfileAttributeList){
|
//save(fileAttribute);保存数据库返回主键id
|
parentId=fileAttribute.getFileId();
|
// 需要加此判断。否则,ftp默认将‘项目文件所在目录之下的目录(./)’与‘项目文件所在目录向上一级目录下的目录(../)’都纳入递归,这样下去就陷入一个死循环了。需将其过滤掉。
|
if (!".".equals(fileAttribute.getFileName()) && !"..".equals(fileAttribute.getFileName())) {
|
List(pathName + fileAttribute.getFileName() + "/",fileAttributeList,parentId,id);
|
}
|
}
|
}
|
|
}
|
}
|
|
/**
|
* 递归遍历目录下面指定的文件名
|
*
|
* @param pathName 需要遍历的目录,必须以"/"开始和结束
|
* @param ext 文件的扩展名
|
* @throws IOException
|
*/
|
public void List(String pathName, String ext) throws IOException {
|
if (pathName.startsWith("/") && pathName.endsWith("/")) {
|
//更换目录到当前目录
|
this.ftp.changeWorkingDirectory(pathName);
|
FTPFile[] files = this.ftp.listFiles();
|
for (FTPFile file : files) {
|
if (file.isFile()) {
|
if (file.getName().endsWith(ext)) {
|
arFiles.add(pathName + file.getName());
|
}
|
} else if (file.isDirectory()) {
|
if (!".".equals(file.getName()) && !"..".equals(file.getName())) {
|
List(pathName + file.getName() + "/", ext);
|
}
|
}
|
}
|
}
|
}
|
|
class MyFTPFile{
|
public Integer fileId;
|
public String fileName;
|
public String fileType;
|
public Long fileSize;
|
public String filePath;
|
public Integer isfile=1;
|
public Integer parentId;
|
public Date fileCreateTime;
|
public Date createTime;
|
public Integer getFileId() {
|
return fileId;
|
}
|
public String getFileName() {
|
return fileName;
|
}
|
public String getFileType() {
|
return fileType;
|
}
|
public Long getFileSize() {
|
return fileSize;
|
}
|
public String getFilePath() {
|
return filePath;
|
}
|
public Integer getIsfile() {
|
return isfile;
|
}
|
public Integer getParentId() {
|
return parentId;
|
}
|
public Date getFileCreateTime() {
|
return fileCreateTime;
|
}
|
public Date getCreateTime() {
|
return createTime;
|
}
|
public void setFileId(Integer fileId) {
|
this.fileId = fileId;
|
}
|
public void setFileName(String fileName) {
|
this.fileName = fileName;
|
}
|
public void setFileType(String fileType) {
|
this.fileType = fileType;
|
}
|
public void setFileSize(Long fileSize) {
|
this.fileSize = fileSize;
|
}
|
public void setFilePath(String filePath) {
|
this.filePath = filePath;
|
}
|
public void setIsfile(Integer isfile) {
|
this.isfile = isfile;
|
}
|
public void setParentId(Integer parentId) {
|
this.parentId = parentId;
|
}
|
public void setFileCreateTime(Date fileCreateTime) {
|
this.fileCreateTime = fileCreateTime;
|
}
|
public void setCreateTime(Date createTime) {
|
this.createTime = createTime;
|
}
|
@Override
|
public String toString() {
|
return "MyFTPFile [fileId=" + fileId + ", fileName=" + fileName + ", fileType=" + fileType + ", fileSize="
|
+ fileSize + ", filePath=" + filePath + ", isfile=" + isfile + ", parentId=" + parentId
|
+ ", fileCreateTime=" + fileCreateTime + ", createTime=" + createTime + "]";
|
}
|
|
}
|
|
|
public static void main(String[] args) {
|
FTPFileUtil util = new FTPFileUtil();
|
//读取所有的电池组列表
|
//List<MyFTPFile> myFiles = util.readAllBattt("192.168.7.130");
|
|
//读取所有的历史文件列表
|
//List<MyFTPFile> myFiles = util.readAllTestData("Default_Battery");
|
|
util.readBattTestData("Default_Battery","F2017-01-15 02.16.18.FBO");
|
|
// for(MyFTPFile file:myFiles) {
|
// System.err.println(file);
|
// }
|
|
}
|
}
|