package ouc.dataworker.bundle.filesystem;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.ResourceBundle;
public class FileListService {
public List<FileListModel> FileList;
public String setUserHome(String userid,String InputPath) {
ResourceBundle resource = ResourceBundle.getBundle("ouc/dataworker/bundle/filesystem/config");
String path = resource.getString("MainPath");
if(userid==null||userid.isEmpty()) {
userid = "tmpuser";
path=path+"//"+userid;
}else {
path=path+"//"+userid;
}
if(InputPath==null||InputPath.isEmpty()) {
}else {
path=path+"//"+InputPath;
}
System.out.println(path);
return path;
}
public List<FileListModel> listfile(String pathName,int depth) throws IOException{
//获取pathName的File对象
File dirFile = new File(pathName);
//判断该文件或目录是否存在,不存在时在控制台输出提醒
if (!dirFile.exists()) {
System.out.println("do not exit");
return null;
}
System.out.println(dirFile.getName());
//获取此目录下的所有文件名与目录名
String[] fileList = dirFile.list();
List<FileListModel> FileList=new ArrayList<FileListModel>();
for (int i = 0; i < fileList.length; i++) {
//遍历文件目录
FileListModel filelist=new FileListModel();
String string = fileList[i];
File file = new File(dirFile.getPath(),string);
String name = file.getName();
filelist.setname(name);
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cal.setTimeInMillis(file.lastModified());
String modtime=formatter.format(cal.getTime());
filelist.setmodificationdate(modtime);
String newpath=pathName+"//"+name;
Path source=Paths.get(newpath);
filelist.settype(Files.probeContentType(source));
filelist.setsize(file.length());
filelist.setpath(file.getPath());
filelist.isdir(file.isDirectory());
FileList.add(filelist);
}
return FileList;
}
public List<FileListModel> creatfile(String path,String name,String content)throws IOException {
String Newpath=path+"//"+name;
File filedir = new File(path);
if (!filedir.exists()) {
File userPath = new File(Newpath );
if (!userPath.exists()) {
} System.out.println("do not exit");
return null;
}
File file = new File(Newpath);
if(file.exists()){
return null;
}else if(name==null||name.isEmpty()) {
return null;
}
file.createNewFile();
FileListModel filelist=new FileListModel();
List<FileListModel> FileList=new ArrayList<FileListModel>();
filelist.setname(name);
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cal.setTimeInMillis(file.lastModified());
String modtime=formatter.format(cal.getTime());
filelist.setmodificationdate(modtime);
Path source=Paths.get(Newpath);
filelist.settype(Files.probeContentType(source));
filelist.setsize(file.length());
filelist.setpath(file.getPath());
filelist.isdir(file.isDirectory());
FileList.add(filelist);
// write
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
bw.close();
fw.close();
return FileList;
}
public List<FileListModel> creatdir(String path,String subpath)throws IOException {
String Newpath=path+"//"+subpath;
File filedir = new File(path);
if (!filedir.exists()) {
System.out.println("do not exit");
return null;
}
File file = new File(Newpath);
if(file.exists()){
return null;
}else if(subpath==null||subpath.isEmpty()) {
return null;
}else if(!file.exists()){
file.mkdirs();
}
FileListModel filelist=new FileListModel();
List<FileListModel> FileList=new ArrayList<FileListModel>();
filelist.setname(subpath);
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cal.setTimeInMillis(file.lastModified());
String modtime=formatter.format(cal.getTime());
filelist.setmodificationdate(modtime);
Path source=Paths.get(Newpath);
filelist.settype(Files.probeContentType(source));
filelist.setsize(file.length());
filelist.setpath(file.getPath());
filelist.isdir(file.isDirectory());
FileList.add(filelist);
return FileList;
}
public OutPutJsonModel statecodeset(String pathName)throws IOException {
StateCodeService statecode=new StateCodeService();
//获取pathName的File对象
List<FileListModel> filelist=new ArrayList<FileListModel>();
filelist=listfile(pathName,0);
OutPutJsonModel json=new OutPutJsonModel();
File dirFile = new File(pathName);
//判断该文件或目录是否存在,不存在时在控制台输出提醒
if (!dirFile.exists()) {
System.out.println("do not exit");
json=statecode.stateset('b',filelist);
} else {
json=statecode.stateset('a',filelist);
}
return json;
}
public OutPutJsonModel statecodeset(String path,String subpath)throws IOException {
StateCodeService statecode=new StateCodeService();
//获取pathName的File对象
List<FileListModel> filelist=new ArrayList<FileListModel>();
filelist=creatdir(path,subpath);
OutPutJsonModel json=new OutPutJsonModel();
String Newpath=path+"//"+subpath;
File dirFile = new File(path);
File file=new File(Newpath);
//判断该文件或目录是否存在,不存在时在控制台输出提醒
if (!dirFile.exists()) {
System.out.println("do not exit");
json=statecode.stateset('b',filelist);
}else if(file.exists()&&(null == filelist ||filelist.size() ==0)&&!subpath.isEmpty()){
System.out.println(file.getName()+subpath);
json=statecode.stateset('e',filelist);
}else if(subpath==null||subpath.isEmpty()) {
json=statecode.stateset('f',filelist);
}else if(file.exists()){
System.out.println(file.getName()+subpath);
json=statecode.stateset('a',filelist);
}
return json;
}
public OutPutJsonModel statecodeset(String path,String name,String content)throws IOException {
StateCodeService statecode=new StateCodeService();
//获取pathName的File对象
List<FileListModel> filelist=new ArrayList<FileListModel>();
filelist=creatfile(path,name,content);
OutPutJsonModel json=new OutPutJsonModel();
String Newpath=path+"//"+name;
File dirFile = new File(path);
File file=new File(Newpath);
//判断该文件或目录是否存在,�