|
@@ -3,8 +3,6 @@ package com.jiayue.ipfcst.common.data.util;
|
|
|
import cn.hutool.core.util.CharsetUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.io.FileUtils;
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
import org.springframework.util.ResourceUtils;
|
|
|
|
|
|
import java.io.*;
|
|
@@ -13,7 +11,6 @@ import java.nio.charset.Charset;
|
|
|
import java.nio.file.*;
|
|
|
import java.nio.file.attribute.BasicFileAttributeView;
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
|
|
-import java.security.MessageDigest;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
@@ -44,90 +41,18 @@ public class CommonDataUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 移动本地文件
|
|
|
+ * 获取文件创建时间
|
|
|
*
|
|
|
- * @param remoteAbsoluteFile 远程文件名(包括完整路径)
|
|
|
- * @param localAbsoluteFile 本地文件名(包括完整路径)
|
|
|
- * @return 成功时,返回true,失败返回false
|
|
|
- * @throws Exception
|
|
|
+ * @param fullFileName
|
|
|
+ * @return
|
|
|
*/
|
|
|
- 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);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static InputStream toInputStream(File file, Charset source, Charset target) {
|
|
|
- InputStream inputStream = null;
|
|
|
- try {
|
|
|
- if (file.exists() && file.canRead()) {
|
|
|
- String context = FileUtils.readFileToString(file, source);
|
|
|
- inputStream = null == context ? null : IOUtils.toInputStream(context, target);
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- return inputStream;
|
|
|
- }
|
|
|
-
|
|
|
- public static File convert(File file, Charset source, Charset target) {
|
|
|
- if (file.exists() && file.canRead() && file.canWrite()) {
|
|
|
- return CharsetUtil.convert(file, source, target);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- //文件内容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();
|
|
|
+ public static Long getFileCreateTime(String fullFileName) throws Exception{
|
|
|
+ Path path = Paths.get(fullFileName);
|
|
|
+ BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
|
|
|
+ BasicFileAttributes attr = basicview.readAttributes();
|
|
|
+ Date createDate = new Date(attr.creationTime().toMillis());
|
|
|
+ return createDate.getTime();
|
|
|
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return md5;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -136,18 +61,10 @@ public class CommonDataUtil {
|
|
|
* @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 System.currentTimeMillis();
|
|
|
+ public static Long getFileModifyTime(String fullFileName) throws Exception{
|
|
|
+ File file = new File(fullFileName);
|
|
|
+ long modifiedTime = file.lastModified();
|
|
|
+ return modifiedTime;
|
|
|
}
|
|
|
|
|
|
/**
|