lyh
10 天以前 30c9299fa9ffc8d3cd51d4a18b4c0b33c3ef8141
src/main/java/com/lxzn/framework/utils/file/FileUtil.java
@@ -5,7 +5,6 @@
import com.lxzn.framework.exception.ExceptionCast;
import com.lxzn.framework.utils.SHA256Util;
import com.lxzn.framework.utils.date.DateUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
@@ -16,6 +15,10 @@
import java.io.*;
import java.net.URLEncoder;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -25,6 +28,7 @@
public class FileUtil {
    private static String fileUploadFolder;
    @Value("${fileHomePath}")
    public void setFileUploadFolder(String fileUploadFolder) {
@@ -59,6 +63,91 @@
        dto.setFileSize(fileSize);
        dto.setFileSuffix(suffix);
        return dto;
    }
    /**
     * 方法一:使用 FileWriter 写文件
     * @param filepath 文件目录
     * @param content  待写入内容
     * @throws IOException
     */
    public static void fileWriterSql(String filepath, String content) throws IOException {
        OutputStreamWriter outputStreamWriter = null;
        try {
            File file = new File(filepath);
            if (!file.exists()){
                file.createNewFile();
            }
            FileOutputStream outputStream = new FileOutputStream(file);
            if (outputStream != null){
                outputStreamWriter = new OutputStreamWriter(outputStream, "utf-8");
                outputStreamWriter.write(content);
                outputStreamWriter.flush();
            }
            try {
                if (outputStreamWriter != null){
                    outputStreamWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        catch (IOException e) {
            e.getMessage();
        }
        finally {
            try {
                if (outputStreamWriter != null){
                    outputStreamWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 删除文件
     *
     * @param filePath
     * @return
     */
    public static boolean deleteNcFile(String filePath) {
        String lastFileReName =  fileUploadFolder + "/" + filePath ;
        boolean flag = false;
        File file = new File(lastFileReName);
        if (!file.exists()) {
            return flag;
        }
        boolean b = file.delete();
        if (!b) {
            System.out.println("文件删除失败: " + filePath);
        }
        return true;
    }
    public static boolean copyFileUpName(String lastFile ,String newFile,String ncFix) {
        String lastFileReName =  fileUploadFolder + "/" + lastFile ;
        if (StringUtils.isNotBlank(ncFix)) {
            newFile =  newFile + "." + ncFix;
        }
        File toFile = new File(newFile);
        if (!toFile.getParentFile().exists()) {
            toFile.mkdirs();
        }
        try {
            long begintime = System.currentTimeMillis(); // 获取拷贝文件前的系统时间
            Files.copy(Paths.get(lastFileReName),
                    Paths.get(newFile),
                    StandardCopyOption.REPLACE_EXISTING);
            long endtime = System.currentTimeMillis(); // 获取文件拷贝结束时的系统时间
            System.out.println("拷贝文件所消耗的时间是:" + (endtime - begintime) + "毫秒");
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    public static FileUploadResult uploadFile(String fileName, InputStream fis) {
@@ -176,6 +265,54 @@
            log.error(e.getMessage());
        }
        return -1;
    }
    /**
     * 删除文件
     *
     * @param filePath
     * @return
     */
    public static boolean deleteFile(String filePath) {
        boolean flag = false;
        File file = new File(filePath);
        if (!file.exists()) {
            return flag;
        }
        boolean b = file.delete();
        if (!b) {
            try {
                System.gc();
                Path path = Paths.get(filePath);
                Files.deleteIfExists(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
    /**
     *
     * @param file
     * @return
     */
    public static long selectFileSize(File file) {
        try (FileInputStream fis = new FileInputStream(file)) {
            long size = 0;
            int read;
            byte[] buffer = new byte[1024];
            while ((read = fis.read(buffer)) != -1) {
                size += read;
            }
            return size;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }
    /**
@@ -352,5 +489,12 @@
        return null;
    }
    public static String getFileAbsPathTxt(String path){
        File file = new File(path);
        if(file.exists()){
            return file.getAbsolutePath();
        }
        return null;
    }
}