FileUtil.java 5.92 KB
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.tiptimes.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class FileUtil {
    public FileUtil() {
    }

    public static String checkImgContentType(String string) {
        String result = "";
        String[] imgContentTypes = new String[]{"image/gif", "image/png", "image/jpeg", "image/jpg"};
        String[] var3 = imgContentTypes;
        int var4 = imgContentTypes.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            String type = var3[var5];
            if (type.equals(string)) {
                result = "." + type.substring(type.lastIndexOf("/") + 1);
            }
        }

        return result;
    }

    public static String checkVideoContentType(String string) {
        String result = "";
        String[] imgContentTypes = new String[]{"video/mp4"};
        String[] var3 = imgContentTypes;
        int var4 = imgContentTypes.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            String type = var3[var5];
            if (type.equals(string)) {
                result = "." + type.substring(type.lastIndexOf("/") + 1);
            }
        }

        return result;
    }

    public static boolean mkdirFolders(String strFilePath) {
        boolean bFlag = false;

        try {
            File file = new File(strFilePath);
            if (!file.exists()) {
                bFlag = file.mkdirs();
            } else {
                bFlag = true;
            }

            if (bFlag) {
                return true;
            } else {
                System.out.println("创建目录" + strFilePath + "失败!");
                return false;
            }
        } catch (Exception var3) {
            System.out.println("新建目录操作出错" + var3.getLocalizedMessage());
            var3.printStackTrace();
            return bFlag;
        }
    }

    public static boolean deleteFile(String fileName) {
        if (fileName != null && !"".equals(fileName)) {
            File file = new File(fileName);
            if (file.exists() && file.isFile()) {
                if (file.delete()) {
                    System.out.println("删除单个文件" + fileName + "成功!");
                    return true;
                } else {
                    System.out.println("删除单个文件" + fileName + "失败!");
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean deleteDirectory(String dir) {
        if (dir != null && !"".equals(dir)) {
            if (!dir.endsWith(File.separator)) {
                dir = dir + File.separator;
            }

            File dirFile = new File(dir);
            if (dirFile.exists() && dirFile.isDirectory()) {
                boolean flag = true;
                File[] files = dirFile.listFiles();

                for(int i = 0; i < files.length; ++i) {
                    if (files[i].isFile()) {
                        flag = deleteFile(files[i].getAbsolutePath());
                        if (!flag) {
                            break;
                        }
                    } else if (files[i].isDirectory()) {
                        flag = deleteDirectory(files[i].getAbsolutePath());
                        if (!flag) {
                            break;
                        }
                    }
                }

                if (!flag) {
                    System.out.println("删除目录失败!");
                    return false;
                } else if (dirFile.delete()) {
                    System.out.println("删除目录" + dir + "成功!");
                    return true;
                } else {
                    return false;
                }
            } else {
                System.out.println("删除目录失败:" + dir + "不存在!");
                return false;
            }
        } else {
            return false;
        }
    }

    public static File saveFileByInputStream(InputStream stream, String path, String filename) throws IOException {
        File savaFile = new File(path + File.separator + filename);
        FileOutputStream fps = new FileOutputStream(savaFile);
        byte[] buffer = new byte[1048576];
        int bytesum = 0;
        boolean var7 = false;

        int byteread;
        while((byteread = stream.read(buffer)) != -1) {
            bytesum += byteread;
            fps.write(buffer, 0, byteread);
            fps.flush();
        }

        fps.close();
        stream.close();
        return savaFile;
    }

    public static File copyFileByPath(String originPath, String targetPath, String targetPathForRepeat) throws IOException {
        File targetFile = new File(targetPath);

        try {
            BufferedInputStream input = null;
            if (originPath.startsWith("http")) {
                input = new BufferedInputStream(new URL(originPath).openStream());
            } else {
                input = new BufferedInputStream(new FileInputStream(originPath));
            }
            FileOutputStream fps;
            if (!targetFile.exists()) {
                fps = new FileOutputStream(targetFile);
            } else {
                fps = new FileOutputStream(targetPathForRepeat);
            }

            byte[] buffer = new byte[1048576];
            int bytesum = 0;
            boolean var8 = false;

            int byteread;
            while((byteread = input.read(buffer)) != -1) {
                bytesum += byteread;
                fps.write(buffer, 0, byteread);
                fps.flush();
            }

            fps.close();
            input.close();
        } catch (Exception var9) {
        }

        return targetFile;
    }
}