xiexiang 2 年之前
父節點
當前提交
ce3c9954df

+ 92 - 0
src/main/java/com/example/fms/common/utils/FileUtils.java

@@ -15,6 +15,7 @@ import org.springframework.web.multipart.commons.CommonsMultipartFile;
 
 import java.io.*;
 import java.net.URL;
+import java.nio.file.Files;
 
 @Service
 public class FileUtils {
@@ -316,6 +317,97 @@ public class FileUtils {
         return DateUtils.getNowTimeFormat("yyyyMMdd");
     }
 
+    public static boolean deleteQuietly(final File file) {
+        if (file == null) {
+            return false;
+        }
+        try {
+            if (file.isDirectory()) {
+                cleanDirectory(file);
+            }
+        } catch (final Exception ignored) {
+        }
+
+        try {
+            return file.delete();
+        } catch (final Exception ignored) {
+            return false;
+        }
+    }
+
+    public static void cleanDirectory(final File directory) throws IOException {
+        final File[] files = verifiedListFiles(directory);
+
+        IOException exception = null;
+        for (final File file : files) {
+            try {
+                forceDelete(file);
+            } catch (final IOException ioe) {
+                exception = ioe;
+            }
+        }
+
+        if (null != exception) {
+            throw exception;
+        }
+    }
+
+    private static File[] verifiedListFiles(final File directory) throws IOException {
+        if (!directory.exists()) {
+            final String message = directory + " does not exist";
+            throw new IllegalArgumentException(message);
+        }
+
+        if (!directory.isDirectory()) {
+            final String message = directory + " is not a directory";
+            throw new IllegalArgumentException(message);
+        }
+
+        final File[] files = directory.listFiles();
+        if (files == null) {  // null if security restricted
+            throw new IOException("Failed to list contents of " + directory);
+        }
+        return files;
+    }
+
+    public static void forceDelete(final File file) throws IOException {
+        if (file.isDirectory()) {
+            deleteDirectory(file);
+        } else {
+            final boolean filePresent = file.exists();
+            if (!file.delete()) {
+                if (!filePresent) {
+                    throw new FileNotFoundException("File does not exist: " + file);
+                }
+                final String message =
+                        "Unable to delete file: " + file;
+                throw new IOException(message);
+            }
+        }
+    }
+
+    public static void deleteDirectory(final File directory) throws IOException {
+        if (!directory.exists()) {
+            return;
+        }
+
+        if (!isSymlink(directory)) {
+            cleanDirectory(directory);
+        }
+
+        if (!directory.delete()) {
+            final String message =
+                    "Unable to delete directory " + directory + ".";
+            throw new IOException(message);
+        }
+    }
+
+    public static boolean isSymlink(final File file) throws IOException {
+        if (file == null) {
+            throw new NullPointerException("File must not be null");
+        }
+        return Files.isSymbolicLink(file.toPath());
+    }
 
 }
 

+ 10 - 0
src/main/java/com/example/fms/controller/FileMangerController.java

@@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.ByteArrayInputStream;
+import java.io.File;
 import java.util.List;
 
 /**
@@ -39,6 +40,15 @@ public class FileMangerController {
         List<Integer> insertIds = fileManagerService.add(files, sourceId);
         return Response.success(insertIds);
     }
+//
+//    @PostMapping("/uploadSystemFile")
+//    @Operation(summary = "上传文件")
+//    public String upload(@RequestBody List<File> files, Integer sourceId ){
+//        for(File file : files) {
+//            System.out.println(file.getName());
+//        }
+//        return Response.success();
+//    }
 
     @GetMapping("/downloadSystemFile")
     @Operation(summary = "取出文件")

+ 6 - 0
src/main/java/com/example/fms/service/FileFactoryService.java

@@ -52,6 +52,12 @@ public class FileFactoryService {
     }
 
 
+//    IExcutePatentData excutePatentDataObject = createObject(task);
+//                    if (excutePatentDataObject != null) {
+//        //执行方法
+//        excutePatentDataObject.startExcute(task);
+//    }
+
     /**
      * 上传到服务器
      *

+ 26 - 0
src/main/java/com/example/fms/service/FileMangerService.java

@@ -2,7 +2,9 @@ package com.example.fms.service;
 
 import com.example.fms.common.model.dto.DownloadSysFileDTO;
 import com.example.fms.common.model.dto.SystemFileDTO;
+import com.example.fms.common.model.vo.ConfigSettingVO;
 import com.example.fms.common.model.vo.SystemFileVO;
+import com.example.fms.common.utils.ExcuteConfigUtils;
 import com.example.fms.domain.SystemFile;
 import com.example.fms.exception.XiaoShiException;
 import com.example.fms.mapper.SystemFileMapper;
@@ -117,4 +119,28 @@ public class FileMangerService {
             }
         }
     }
+
+
+//    /**
+//     * 工厂方法
+//     *
+//     * @param task 任务
+//     * @return 返回获取专利数据的对象
+//     */
+//    private IFileFactory createObject(Integer id) {
+//        //调用解析配置方法,获取配置信息
+//        List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
+//        //根据配置id获得配置
+//        ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(id)).findFirst().orElse(null);
+//        //根据任务的类型创建并返回对应的解析获取专利数据的对象
+//        switch (id) {
+//            case 1:  //FSS
+//                return fileFactoryService;
+//            case 2:  //欧专局网站导入
+//                return excutePatentDataEpo;
+//            default:
+//                return null;
+//        }
+//
+//    }
 }

+ 1 - 0
src/main/java/com/example/fms/service/SftpService.java

@@ -154,6 +154,7 @@ public class SftpService {
             InputStream in = new FileInputStream(file);
             sftp.put(in, file.getName());
             in.close();
+            FileUtils.deleteQuietly(file);
             //获取文件的后缀,不带“.”,必须是multipartFile类型
             String extName = FileUtil.extName(multipartFile.getOriginalFilename());
             //拼接文件完整名存入数据库表