import org.apache.log4j.Logger; import java.io.*; import java.util.ArrayList; import com.ljqc.license.entity.pg.FilePathNode; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.io.ZipInputStream; import net.lingala.zip4j.model.FileHeader; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import lombok.extern.slf4j.Slf4j; import org.apache.tomcat.util.http.fileupload.FileUtils; import org.springframework.stereotype.Component; import java.io.File; import java.io.IOException; /** * description:文件工具类 **/ @Slf4j @Component public class FileUtil { private static Logger logger = Logger.getLogger(FileUtil.class);
/** * 获取所有路径 */ public static ArrayList<String> getAllPath(String path) { ArrayList<String> paths = new ArrayList<String>(); File fpath = new File(path); getAllPath(fpath, paths); return paths; } /** * 递归获取一个目录下的所有文件 */ public static void getAllPath(File path, ArrayList<String> paths) { File[] fs = path.listFiles(); if (fs != null) { for (int i = 0; i < fs.length; i++) { if (java.nio.file.Files.isSymbolicLink(fs[i].toPath())) { continue; } if (fs[i].isDirectory()) { getAllPath(fs[i], paths); } if (fs[i].isFile()) { paths.add(fs[i].toString()); } } } } /** * @Description //TODO 递归删除一个目录下的所有子目录(适合linux、windows) **/ public static boolean deleteDir(String path) { File file = new File(path); if(file.exists()){ //空目录可以直接删除 boolean success = (new File(path)).delete(); //非空目录,递归删除 if (!success) { if (file.isDirectory()) { String[] children = file.list(); //递归删除目录中的子目录下 for (int i=0; i<children.length; i++) { boolean succ =deleteDir(file.toPath()+File.separator+children[i]); if (!succ) { return false; } } } // 目录此时为空,可以删除 return file.delete(); } } return true; } /** * @Description //TODO linux命令删除目录/文件 **/ public static boolean delDirByShell(String dir_path) { InputStream in = null; BufferedReader read = null; try { File file = new File(dir_path.replace("*", "")); if (!file.exists()) { return true; } String[] cmds = {"/bin/sh", "-c", "rm -rf " + dir_path}; Process pro = Runtime.getRuntime().exec(cmds); pro.waitFor(); in = pro.getInputStream(); read = new BufferedReader(new InputStreamReader(in)); String line = null; while ((line = read.readLine()) != null) { logger.info(line); } logger.info("成功删除:" + dir_path); } catch (Exception ex) { logger.error("删除失败:" + dir_path, ex); return false; } finally { try { if (read != null) { read.close(); } if (in != null) { in.close(); } } catch (Exception ex) { logger.error("killPids关闭流异常:", ex); } } return true; }
/** * 根据文件路径,获取文件名称 */ public static String getFilename(String filepath) { try{ File file = new File(filepath);
return file.getName(); }catch (Exception ex){ return filepath; } } /** * java读取文本 */ public static String readFile(String fileName) { String encoding = "UTF-8"; File file = new File(fileName); if (!file.exists()) { return null; } Long filelength = file.length(); byte[] filecontent = new byte[filelength.intValue()]; try { FileInputStream in = new FileInputStream(file); in.read(filecontent); in.close(); } catch (FileNotFoundException e) { logger.error("readFile FileNotFoundException:", e); } catch (IOException e) { logger.error("readFile IOException:", e); } try { return new String(filecontent, encoding); } catch (UnsupportedEncodingException e) { logger.error("readFile UnsupportedEncodingException:", e); return null; } } /** * 按行读取文件 */ public static ArrayList<String> readFileline(String filepath) { ArrayList<String> lines = new ArrayList<>(); InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; try { File file = new File(filepath); if (file.exists()) { inputStreamReader = new InputStreamReader(new FileInputStream(file)); bufferedReader = new BufferedReader(inputStreamReader); String str; while ((str = bufferedReader.readLine()) != null) { lines.add(str); } } } catch (IOException e) { logger.error("readline exception:" + filepath); } finally { try { if (bufferedReader != null) { bufferedReader.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } } catch (Exception ex) { ex.printStackTrace(); } } return lines; } public static String readFileContent(File file) { StringBuilder result = new StringBuilder(); try { BufferedReader br = new BufferedReader(new FileReader(file)); //构造一个BufferedReader类来读取文件 String s = null; while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行 result.append(System.lineSeparator() + s); } br.close(); } catch (Exception e) { logger.error("read txt file {} error", file.getAbsolutePath()); logger.error(e.toString()); } return result.toString(); }
public static String getZipFileContent(String zipPath, String filePath) { BufferedReader br = null; InputStreamReader isr = null; ZipInputStream zi = null; try { ZipFile zip_file = new ZipFile(zipPath);
FileHeader fh = zip_file.getFileHeader(filePath); zi = zip_file.getInputStream(fh); char[] b = new char[(int) fh.getUncompressedSize()]; isr = new InputStreamReader(zi, "utf-8"); br = new BufferedReader(isr); br.read(b); zi.close(); isr.close(); return String.valueOf(b); } catch (Exception e) { logger.error("get zip file content error! zip path:{}, file " + "path:{}", zipPath, filePath); } finally { try { if (zi != null) { zi.close(); } if (isr != null) { isr.close(); } if (br != null) { br.close(); } } catch (Exception ex) { } } try {
} catch (Exception e) {
} return null; } }
