Explorar el Código

修复linux下读取md5不正常的bug,由于jdk的bug(无法在linux读取精准的lastModified)

NorthLan hace 6 años
padre
commit
a22a562363
Se han modificado 1 ficheros con 4 adiciones y 2 borrados
  1. 4 2
      zen-core/src/main/kotlin/com/gxzc/zen/common/util/FileUtil.kt

+ 4 - 2
zen-core/src/main/kotlin/com/gxzc/zen/common/util/FileUtil.kt

@@ -5,6 +5,7 @@ import org.apache.commons.io.FilenameUtils
 import java.io.File
 import java.io.FileInputStream
 import java.nio.ByteBuffer
+import java.nio.file.Files
 
 /**
  * 文件工具类
@@ -45,6 +46,7 @@ object FileUtil {
         val fc = FileInputStream(file).channel
         val buffer = ByteBuffer.allocate(chunkSize)
         val fileSize = fc.size()
+        val lastModified = file.lastModified() / 1000 * 1000 // 由于jdk8的bug导致在linux上读取修改时间丢失精度
         val chunks = Math.ceil(1.0 * fileSize / chunkSize).toInt() // 分块总数
 
         var currentPos = 0 * chunkSize * 1L
@@ -69,7 +71,7 @@ object FileUtil {
                     retBuffer.put(byte)
                     retBuffer.put(byte2)
 
-                    val timeByteArray = ByteBuffer.allocate(8).putLong(file.lastModified()).array()
+                    val timeByteArray = ByteBuffer.allocate(8).putLong(lastModified).array()
                     timeByteArray.reverse()
 
                     retBuffer.put(timeByteArray)
@@ -82,7 +84,7 @@ object FileUtil {
                 val retBuffer = ByteBuffer.allocate(byte.size + 8) // 8 为long占用字节数
                 retBuffer.put(byte)
 
-                val timeByteArray = ByteBuffer.allocate(8).putLong(file.lastModified()).array()
+                val timeByteArray = ByteBuffer.allocate(8).putLong(lastModified).array()
                 timeByteArray.reverse()
 
                 retBuffer.put(timeByteArray)