wuheng преди 1 година
родител
ревизия
5340318d4f

+ 34 - 6
common/src/main/java/com/koobietech/eas/common/utils/FileManager.java

@@ -23,8 +23,9 @@ public class FileManager {
     public Double getFolderSize(String sizePath) {
         Double size = 0.0;
         Path folderPath = Paths.get(sizePath);
+        DirectoryStream<Path> stream = null;
         try {
-            DirectoryStream<Path> stream = Files.newDirectoryStream(folderPath);
+            stream = Files.newDirectoryStream(folderPath);
             for (Path path : stream) {
                 if (Files.isRegularFile(path)) {
                     size += Files.size(path);
@@ -32,7 +33,11 @@ public class FileManager {
                     size += getFolderSize(path.toFile().getAbsolutePath());
                 }
             }
-        } catch (IOException e) {}
+        } catch (IOException e) {}finally {
+            try {
+                stream.close();
+            } catch (IOException e) {}
+        }
         if ( size > 0  ) {
             size = size / 1024 / 1024 ;
             DecimalFormat df = new DecimalFormat("#.####");
@@ -63,14 +68,19 @@ public class FileManager {
             if (files != null) {
                 for (File file : files) {
                     if ( file.isFile() ) {
+                        FileChannel sourceChannel = null;
+                        FileChannel destChannel = null;
                         try {
-                            FileChannel sourceChannel = new FileInputStream(file).getChannel();
-                            FileChannel destChannel = new FileOutputStream(new File(targetFolder.getPath(), file.getName())).getChannel();
+                            sourceChannel = new FileInputStream(file).getChannel();
+                            destChannel = new FileOutputStream(new File(targetFolder.getPath(), file.getName())).getChannel();
                             destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
                             sourceChannel.close();
                             file.delete();
-                        } catch (IOException e) {
-                            e.printStackTrace();
+                        } catch (IOException e) {}finally {
+                            try {
+                                sourceChannel.close();
+                                destChannel.close();
+                            } catch (IOException e) {}
                         }
                     } else {
                         moveFolder(file.getPath(), new File(targetFolder.getPath(), file.getName()).getPath());
@@ -342,4 +352,22 @@ public class FileManager {
             return  0.0;
         }
     }
+
+    /**
+     * 获取文件流
+     * @param filePath
+     * @return
+     */
+    public FileInputStream getArchiveFile(String filePath) {
+        FileInputStream fileOutputStream = null;
+        try {
+            fileOutputStream = new FileInputStream(filePath);
+        } catch (FileNotFoundException e) {}
+        finally {
+            try {
+                fileOutputStream.close();
+            } catch (IOException e) {}
+        }
+        return fileOutputStream;
+    }
 }

+ 9 - 1
service/src/main/java/com/koobietech/eas/service/EasArchivesFilesService.java

@@ -3,6 +3,7 @@ package com.koobietech.eas.service;
 import com.koobietech.eas.dao.dto.ArchivesDto;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 
+import java.io.FileInputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 
@@ -51,7 +52,14 @@ public interface EasArchivesFilesService {
      * @param filePath
      * @return
      */
-    OutputStream getArchiveFile(String filePath);
+    FileInputStream getArchiveFile(String filePath);
+
+    /**
+     * 获取Resource文件
+     * @param filePath
+     * @return
+     */
+    InputStream getResourceStream(String filePath);
 
     /**
      * 删除文件

+ 17 - 6
service/src/main/java/com/koobietech/eas/service/impl/EasArchivesFilesServiceImpl.java

@@ -11,9 +11,7 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.io.File;
-import java.io.InputStream;
-import java.io.OutputStream;
+import java.io.*;
 import java.util.Date;
 
 @Service
@@ -67,12 +65,25 @@ public class EasArchivesFilesServiceImpl implements EasArchivesFilesService {
     }
 
     @Override
-    public OutputStream getArchiveFile(String filePath) {
-        return null;
+    public FileInputStream getArchiveFile(String filePath) {
+        return  fileManager.getArchiveFile(filePath);
+    }
+
+    @Override
+    public InputStream getResourceStream(String filePath) {
+        InputStream resourceAsStream = null;
+        try {
+            resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filePath);
+        } catch (Exception e) {}finally {
+            try {
+                resourceAsStream.close();
+            } catch (IOException e) {}
+        }
+        return resourceAsStream;
     }
 
     @Override
     public boolean deleteArchiveFile(String filePath) {
-        return false;
+        return fileManager.deleteFile(filePath);
     }
 }