123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package com.jiayue.ipfcst.spare2.util;
- import cn.hutool.core.io.FileUtil;
- import com.jiayue.ipfcst.common.data.util.CommonDataUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.io.FileUtils;
- import java.io.File;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.LinkOption;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.nio.file.attribute.BasicFileAttributeView;
- import java.nio.file.attribute.BasicFileAttributes;
- import java.security.MessageDigest;
- import java.util.Date;
- /**
- * 文件工具类
- *
- * @author xsl
- * @version 3.0
- */
- @Slf4j
- public class FilesUtil extends CommonDataUtil {
- private static final String imageFileSuffix = "txt";
- public static <T> String imageFileName(Class<T> clazz) {
- return clazz.getSimpleName() + "." + imageFileSuffix;
- }
- /**
- * 移动本地文件
- *
- * @param remoteAbsoluteFile 远程文件名(包括完整路径)
- * @param localAbsoluteFile 本地文件名(包括完整路径)
- * @return 成功时,返回true,失败返回false
- * @throws Exception
- */
- public static void move(String localAbsoluteFile, String remoteAbsoluteFile) throws Exception {
- move(localAbsoluteFile, remoteAbsoluteFile, true);
- }
- public static void move(String localAbsoluteFile, String remoteAbsoluteFile, boolean overwrite) throws Exception {
- File srcFile = new File(localAbsoluteFile);
- File destDir = new File(remoteAbsoluteFile);
- StringBuilder opts = new StringBuilder();
- try {
- if (!destDir.exists()) {// 如果目录不存在则创建目录
- boolean b = destDir.mkdirs();
- if (!b) // 如果创建失败则抛出异常
- throw new RuntimeException(destDir + " 目录创建失败");
- }
- File desFile = new File(remoteAbsoluteFile + File.separator + srcFile.getName());
- if (overwrite && desFile.exists()) {
- FileUtils.deleteQuietly(desFile);
- opts.append(" >> 覆盖 >> ");
- } else {
- opts.append(" >> 移动 >> ");
- }
- FileUtils.moveFile(srcFile, desFile);
- log.info("文件:" + srcFile.getName() + opts + destDir.getPath());
- } catch (IOException e) {
- throw new Exception("文件:" + srcFile.getName() + opts + destDir.getPath() + "失败。", e);
- }
- }
- //文件内容md5加密
- public static String getMD5(byte[] fileByte) {
- String md5 = new String();
- try {
- MessageDigest md = MessageDigest.getInstance("MD5");
- md.update(fileByte);
- byte b[] = md.digest();
- int i;
- StringBuffer buf = new StringBuffer("");
- for (int offset = 0; offset < b.length; offset++) {
- i = b[offset];
- if (i < 0)
- i += 256;
- if (i < 16)
- buf.append("0");
- buf.append(Integer.toHexString(i));
- }
- md5 = buf.toString();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return md5;
- }
- /**
- * 获取文件创建时间
- *
- * @param fullFileName
- * @return
- */
- public static Long getFileCreateTime(String fullFileName) {
- Path path = Paths.get(fullFileName);
- BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
- BasicFileAttributes attr;
- try {
- attr = basicview.readAttributes();
- Date createDate = new Date(attr.creationTime().toMillis());
- return createDate.getTime();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return new Date().getTime();
- }
- /**
- * 检查uploadfile目录下是否生成过文件{name}
- * @param name
- * @return
- */
- public static boolean checkUpGened(String name) {
- return FileUtil.exist(getFileUploadPath(), name);
- }
- }
|