| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- package com.izouma.awesomeAdmin.utils;
- import org.apache.commons.lang3.StringUtils;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.attribute.PosixFileAttributeView;
- import java.nio.file.attribute.PosixFileAttributes;
- import java.nio.file.attribute.PosixFilePermission;
- import java.nio.file.attribute.PosixFilePermissions;
- import java.util.Set;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- public class FileUtils {
- public static String getExtension(String fileName) {
- if (StringUtils.INDEX_NOT_FOUND == StringUtils.indexOf(fileName, "."))
- return StringUtils.EMPTY;
- String ext = StringUtils.substring(fileName, StringUtils.lastIndexOf(fileName, "."));
- return StringUtils.trimToEmpty(ext);
- }
- public static String getFileName(String header) {
- String[] tempArr1 = header.split(";");
- String[] tempArr2 = tempArr1[2].split("=");
- // 获取文件名,兼容各种浏览器的写法
- return tempArr2[1].substring(tempArr2[1].lastIndexOf("\\") + 1).replaceAll("\"", "");
- }
- /**
- * 获取文件权限
- *
- * <pre>
- * 当前仅支持posix系统,需要修改为windows格式
- * </pre>
- *
- * @param path
- * @return
- * @throws IOException
- */
- public static String getPermissions(Path path) throws IOException {
- PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(path, PosixFileAttributeView.class);
- PosixFileAttributes readAttributes = fileAttributeView.readAttributes();
- Set<PosixFilePermission> permissions = readAttributes.permissions();
- return PosixFilePermissions.toString(permissions);
- }
- /**
- * 修改文件权限
- *
- * @param file
- * @param permsCode
- * @param recursive
- * @return
- * @throws IOException
- */
- public static String setPermissions(File file, String permsCode, boolean recursive) throws IOException {
- PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(file.toPath(),
- PosixFileAttributeView.class);
- fileAttributeView.setPermissions(PosixFilePermissions.fromString(permsCode));
- if (file.isDirectory() && recursive && file.listFiles() != null) {
- for (File f : file.listFiles()) {
- setPermissions(f, permsCode, true);
- }
- }
- return permsCode;
- }
- public static boolean write(InputStream inputStream, File f) {
- boolean ret = false;
- try (OutputStream outputStream = new FileOutputStream(f)) {
- int read;
- byte[] bytes = new byte[1024];
- while ((read = inputStream.read(bytes)) != -1) {
- outputStream.write(bytes, 0, read);
- }
- ret = true;
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (inputStream != null) {
- try {
- inputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return ret;
- }
- public static void mkFolder(String fileName) {
- File f = new File(fileName);
- if (!f.exists()) {
- f.mkdir();
- }
- }
- public static File mkFile(String fileName) {
- File f = new File(fileName);
- try {
- f.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return f;
- }
- public static void fileProber(File dirFile) {
- File parentFile = dirFile.getParentFile();
- if (!parentFile.exists()) {
- // 递归寻找上级目录
- fileProber(parentFile);
- parentFile.mkdir();
- }
- }
- /**
- * 将文本文件中的内容读入到buffer中
- *
- * @param buffer buffer
- * @param filePath 文件路径
- * @throws IOException 异常
- * @date 2013-1-7
- */
- public static void readToBuffer(StringBuffer buffer, String filePath) throws IOException {
- InputStream is = new FileInputStream(filePath);
- String line; // 用来保存每行读取的内容
- BufferedReader reader = new BufferedReader(new InputStreamReader(is));
- line = reader.readLine(); // 读取第一行
- while (line != null) { // 如果 line 为空说明读完了
- buffer.append(line); // 将读到的内容添加到 buffer 中
- buffer.append("\n"); // 添加换行符
- line = reader.readLine(); // 读取下一行
- }
- reader.close();
- is.close();
- }
- /**
- * 读取文本文件内容
- *
- * @param filePath 文件所在路径
- * @return 文本内容
- * @throws IOException 异常
- * @date 2013-1-7
- */
- public static String readFile(String filePath) throws IOException {
- StringBuffer sb = new StringBuffer();
- FileUtils.readToBuffer(sb, filePath);
- return sb.toString();
- }
- /**
- * 功能:Java读取txt文件的内容
- * 步骤:1:先获得文件句柄
- * 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
- * 3:读取到输入流后,需要读取生成字节流
- * 4:一行一行的输出。readline()。
- * 备注:需要考虑的是异常情况
- */
- public static String readFileToString(File file) {
- try {
- if (file.isFile() && file.exists()) { //判断文件是否存在
- Long filelength = file.length(); // 获取文件长度
- byte[] filecontent = new byte[filelength.intValue()];
- try {
- FileInputStream in = new FileInputStream(file);
- in.read(filecontent);
- in.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- String fileContentArr = new String(filecontent);
- return fileContentArr;// 返回文件内容,默认编码
- } else {
- System.out.println("找不到指定的文件");
- }
- } catch (Exception e) {
- System.out.println("读取文件内容出错");
- e.printStackTrace();
- }
- return null;
- }
- public static void unzip(InputStream in, File destDir) throws IOException {
- byte[] buffer = new byte[1024];
- ZipInputStream zis = new ZipInputStream(in);
- ZipEntry zipEntry = zis.getNextEntry();
- while (zipEntry != null) {
- File newFile = newFile(destDir, zipEntry);
- if (zipEntry.isDirectory()) {
- if (!newFile.isDirectory() && !newFile.mkdirs()) {
- throw new IOException("Failed to create directory " + newFile);
- }
- } else {
- // fix for Windows-created archives
- File parent = newFile.getParentFile();
- if (!parent.isDirectory() && !parent.mkdirs()) {
- throw new IOException("Failed to create directory " + parent);
- }
- // write file content
- FileOutputStream fos = new FileOutputStream(newFile);
- int len;
- while ((len = zis.read(buffer)) > 0) {
- fos.write(buffer, 0, len);
- }
- fos.close();
- }
- zipEntry = zis.getNextEntry();
- }
- zis.closeEntry();
- zis.close();
- }
- public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
- File destFile = new File(destinationDir, zipEntry.getName());
- String destDirPath = destinationDir.getCanonicalPath();
- String destFilePath = destFile.getCanonicalPath();
- if (!destFilePath.startsWith(destDirPath + File.separator)) {
- throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
- }
- return destFile;
- }
- public static File findExcel(File dir) {
- if (!(dir.exists() && dir.isDirectory())) return null;
- for (File file : dir.listFiles()) {
- String name = file.getName().toLowerCase();
- if ((name.endsWith(".xlsx") || name.endsWith(".xls")) && !name.startsWith(".") && !file.isHidden()) {
- return file;
- }
- }
- return null;
- }
- }
|