Browse Source

优化文件读取逻辑,关闭文件流。

NorthLan 6 years ago
parent
commit
e238d78f51
1 changed files with 28 additions and 25 deletions
  1. 28 25
      zen-core/src/main/kotlin/com/gxzc/zen/common/util/FileUtil.kt

+ 28 - 25
zen-core/src/main/kotlin/com/gxzc/zen/common/util/FileUtil.kt

@@ -1,11 +1,10 @@
 package com.gxzc.zen.common.util
 
 import com.gxzc.zen.common.properties.UploadProperties
-import org.apache.commons.io.FilenameUtils
 import java.io.File
 import java.io.FileInputStream
+import java.io.IOException
 import java.nio.ByteBuffer
-import java.nio.file.Files
 
 /**
  * 文件工具类
@@ -44,15 +43,21 @@ object FileUtil {
             chunkSize = uploadProperties!!.chunkSize!!.toInt()
         }
         val fc = FileInputStream(file).channel
-        val buffer = ByteBuffer.allocate(chunkSize)
         val fileSize = fc.size()
+
+        val buffer = ByteBuffer.allocate(chunkSize)
         val lastModified = file.lastModified() / 1000 * 1000 // 由于jdk8的bug导致在linux上读取修改时间丢失精度
         val chunks = Math.ceil(1.0 * fileSize / chunkSize).toInt() // 分块总数
 
         var currentPos = 0 * chunkSize * 1L
 
-        var readLength: Int
-        readLength = fc.read(buffer, currentPos)
+        var retBuffer: ByteBuffer? = null
+
+        var readLength = try {
+            fc.read(buffer, currentPos)
+        } catch (e: IOException) {
+            -1
+        }
         if (readLength != -1) {
             val byte = ByteArray(readLength)
             buffer.flip()
@@ -61,38 +66,36 @@ object FileUtil {
             // 第一块读完
             currentPos = (chunks - 1) * chunkSize * 1L
             if (currentPos > 0) {
-                readLength = fc.read(buffer, currentPos)
-                return if (readLength != -1) {
+                readLength = try {
+                    fc.read(buffer, currentPos)
+                } catch (e: IOException) {
+                    -1
+                }
+                if (readLength != -1) {
                     val byte2 = ByteArray(readLength)
                     buffer.flip()
                     buffer.get(byte2)
                     buffer.clear()
-                    val retBuffer = ByteBuffer.allocate(byte.size + byte2.size + 8) // 8 为long占用字节数
+                    // 第二块读完
+                    retBuffer = ByteBuffer.allocate(byte.size + byte2.size + 8) // 8 为long占用字节数
                     retBuffer.put(byte)
                     retBuffer.put(byte2)
-
-                    val timeByteArray = ByteBuffer.allocate(8).putLong(lastModified).array()
-                    timeByteArray.reverse()
-
-                    retBuffer.put(timeByteArray)
-                    //
-                    MD5Util.encodeMd5(retBuffer.array())
-                } else {
-                    ""
                 }
             } else {
-                val retBuffer = ByteBuffer.allocate(byte.size + 8) // 8 为long占用字节数
+                retBuffer = ByteBuffer.allocate(byte.size + 8) // 8 为long占用字节数
                 retBuffer.put(byte)
+            }
+        }
+        return if (retBuffer != null) {
+            val timeByteArray = ByteBuffer.allocate(8).putLong(lastModified).array()
+            timeByteArray.reverse()
 
-                val timeByteArray = ByteBuffer.allocate(8).putLong(lastModified).array()
-                timeByteArray.reverse()
+            retBuffer.put(timeByteArray)
 
-                retBuffer.put(timeByteArray)
-                //
-                return MD5Util.encodeMd5(retBuffer.array())
-            }
+            MD5Util.encodeMd5(retBuffer.array())
         } else {
-            return ""
+            ""
         }
+
     }
 }