瀏覽代碼

实现解析压缩包功能

qitz 2 年之前
父節點
當前提交
437b672446

+ 17
- 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/wisdomarbitrate/CaseApplicationController.java 查看文件

@@ -96,6 +96,8 @@ public class CaseApplicationController extends BaseController {
96 96
         return toAjax(caseApplicationService.submitCaseApplication(batchCaseApplication.getIds()));
97 97
     }
98 98
 
99
+
100
+
99 101
     /**
100 102
      * 删除立案数据
101 103
      */
@@ -158,6 +160,21 @@ public class CaseApplicationController extends BaseController {
158 160
         return success(sealUrlRecordselect);
159 161
     }
160 162
 
163
+
164
+    /**
165
+     * 案件证据材料压缩包上传
166
+     *
167
+     * @param file      附件
168
+     * @param id        案件申请id
169
+     * @return
170
+     */
171
+    @PostMapping("/upload")
172
+    public AjaxResult uploadZipFile(@RequestParam("file") MultipartFile file, Long id) {
173
+        String username = this.getUsername();
174
+        Long userId = this.getUserId();
175
+        return caseApplicationService.uploadZipFile(file, id, username, userId);
176
+    }
177
+
161 178
     /**
162 179
      * 立案申请导入模板下载
163 180
      */

+ 3
- 0
ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/ICaseApplicationService.java 查看文件

@@ -8,6 +8,7 @@ import com.ruoyi.wisdomarbitrate.domain.CaseApplication;
8 8
 import com.ruoyi.wisdomarbitrate.domain.SealSignRecord;
9 9
 import com.ruoyi.wisdomarbitrate.domain.vo.SendRoomNoMessageVO;
10 10
 import com.ruoyi.wisdomarbitrate.domain.vo.ToDoCount;
11
+import org.springframework.web.multipart.MultipartFile;
11 12
 
12 13
 import java.util.List;
13 14
 
@@ -72,4 +73,6 @@ public interface ICaseApplicationService {
72 73
      * @return
73 74
      */
74 75
     int updateCaseLockStatus(CaseApplication caseApplication);
76
+
77
+    AjaxResult uploadZipFile(MultipartFile file, Long id, String username, Long userId);
75 78
 }

+ 123
- 2
ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/CaseApplicationServiceImpl.java 查看文件

@@ -35,13 +35,14 @@ import com.ruoyi.wisdomarbitrate.domain.vo.SendRoomNoMessageVO;
35 35
 import com.ruoyi.wisdomarbitrate.mapper.*;
36 36
 import com.ruoyi.wisdomarbitrate.service.ICaseApplicationService;
37 37
 import com.ruoyi.wisdomarbitrate.utils.SignAward;
38
+import com.ruoyi.wisdomarbitrate.utils.UnZipFileUtils;
38 39
 import org.springframework.beans.factory.annotation.Autowired;
39 40
 import org.springframework.stereotype.Service;
40 41
 import org.springframework.transaction.annotation.Transactional;
41 42
 import org.springframework.web.bind.annotation.RequestParam;
43
+import org.springframework.web.multipart.MultipartFile;
42 44
 
43
-import java.io.File;
44
-import java.io.IOException;
45
+import java.io.*;
45 46
 import java.math.BigDecimal;
46 47
 import java.nio.file.Files;
47 48
 import java.nio.file.Path;
@@ -84,6 +85,8 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
84 85
     private SealSignRecordMapper sealSignRecordMapper;
85 86
     @Autowired
86 87
     private CaseLogRecordMapper caseLogRecordMapper;
88
+    @Autowired
89
+    private   CaseEvidenceDirectoryMapper caseEvidenceDirectoryMapper;
87 90
     // 手机号正则
88 91
     private static final Pattern TELEPHONE_REGX =  Pattern.compile("^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$");
89 92
 
@@ -247,6 +250,124 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
247 250
         return caseApplicationMapper.updateCaseLockStatus(caseApplication.getId(),caseApplication.getLockStatus());
248 251
     }
249 252
 
253
+    public  void inputChangeToFile(InputStream instream, File file) {
254
+        try {
255
+            OutputStream outStr = new FileOutputStream(file);
256
+            int bytesRead = 0;
257
+            byte[] buffer = new byte[8192];
258
+            while ((bytesRead = instream.read(buffer, 0, 8192)) != -1) {
259
+                outStr.write(buffer, 0, bytesRead);
260
+            }
261
+            outStr.flush();
262
+            outStr.close();
263
+            instream.close();
264
+        } catch (Exception e) {
265
+            e.printStackTrace();
266
+        }
267
+    }
268
+
269
+    @Override
270
+    @Transactional
271
+    public AjaxResult uploadZipFile(MultipartFile file, Long id, String username, Long userId) {
272
+        if (file.isEmpty()) {
273
+            return AjaxResult.error("请选择要上传的文件");
274
+        }
275
+//        String targetPath =  "/home/ruoyi/uploadPath/upload/unzipFile";
276
+        String targetPath =  "F:\\testZip\\unzipFile";
277
+        File zipFile = null;
278
+        InputStream ins = null;
279
+        try {
280
+            ins = file.getInputStream();
281
+            zipFile = new File(file.getOriginalFilename());
282
+            inputChangeToFile(ins, zipFile);
283
+        } catch (IOException e) {
284
+            e.printStackTrace();
285
+        }
286
+        //解压缩上传的压缩包
287
+        UnZipFileUtils.unZipFile(zipFile,targetPath);
288
+
289
+        //得到解压缩的所有文件
290
+        String zipName = file.getOriginalFilename();
291
+        String zipPath = "F:\\testZip\\unzipFile\\"+zipName;
292
+//        String zipPath =  "/home/ruoyi/uploadPath/upload/unzipFile"+zipName;
293
+        File dirUnzipPath = new File(zipPath);
294
+        List<File> allFiles = new ArrayList<>();
295
+        List<String> allFilestr = new ArrayList<>();
296
+        UnZipFileUtils.getFiles(dirUnzipPath,allFiles);
297
+        if(allFiles!=null&&allFiles.size()>0){
298
+            for (File fileIter : allFiles) {
299
+                System.out.println(fileIter.getAbsolutePath());
300
+                allFilestr.add(fileIter.getAbsolutePath());
301
+            }
302
+        }
303
+
304
+        if(allFilestr!=null&&allFilestr.size()>0){
305
+            for (String filestr : allFilestr) {
306
+                List<Integer> allindex = new ArrayList<>();
307
+                int indexKey = filestr.indexOf("\\");
308
+                allindex.add(indexKey);
309
+                while (indexKey != -1) {
310
+                    indexKey = filestr.indexOf("\\", indexKey + 1);
311
+                    allindex.add(indexKey);
312
+                }
313
+                if(allindex!=null&&allindex.size()>0){
314
+                    for (int i = 0; i < allindex.size()-2; i++) {
315
+                        //得到第2个 \ 和 第3个 \ 之间的目录 为一级(i+1)目录,
316
+                        String substr = filestr.substring(allindex.get(i+1)+1,allindex.get(i+2));
317
+                        Integer series = i + 1;
318
+                        Integer idcaseEvidenceDirectory = null;
319
+                        if(series==1){
320
+                            CaseEvidenceDirectory caseEvidenceDirectory = new CaseEvidenceDirectory();
321
+                            caseEvidenceDirectory.setEvidenceName(substr);
322
+                            caseEvidenceDirectory.setSeries(series);
323
+
324
+                            //caseEvidenceDirectory.setCaseid(id);
325
+                            caseEvidenceDirectory.setCreateBy(username);
326
+                            caseEvidenceDirectory.setCreateTime(new Date());
327
+
328
+                            caseEvidenceDirectoryMapper.save(caseEvidenceDirectory);
329
+                            idcaseEvidenceDirectory = caseEvidenceDirectory.getId();
330
+
331
+                        }else {
332
+                            CaseEvidenceDirectory caseEvidenceDirectory = new CaseEvidenceDirectory();
333
+                            caseEvidenceDirectory.setEvidenceName(substr);
334
+                            caseEvidenceDirectory.setSeries(series);
335
+
336
+                            caseEvidenceDirectory.setParentId(idcaseEvidenceDirectory);
337
+                            
338
+                            caseEvidenceDirectoryMapper.save(caseEvidenceDirectory);
339
+                            
340
+
341
+
342
+                        }
343
+                        //通过 案件id 和 目录表 的各个层级的 级数 和 目录名称 查询目录表
344
+                        //判断这个级数的目录是否已经在目录表中存在,
345
+                        //若存在,不用新建这个目录,
346
+                        //若不存在,则新建这个目录
347
+                        //保存到目录表
348
+
349
+
350
+
351
+                    }
352
+                    int lastIndex = allindex.size();
353
+                    //得到 最后1个 \ 之后的目录 为文件名,
354
+                    //保存到目录表
355
+                    //保存到附件表
356
+
357
+                }
358
+
359
+
360
+
361
+            }
362
+
363
+
364
+
365
+        }
366
+
367
+        return AjaxResult.success();
368
+
369
+    }
370
+
250 371
     private List<CaseLogRecord> getNofinishCasenode(Integer caseStatus) {
251 372
         CaseLogRecord caseLogRecord1 = new CaseLogRecord();
252 373
         caseLogRecord1.setCaseNodeName("立案审查");

+ 126
- 0
ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/UnZipFileUtils.java 查看文件

@@ -0,0 +1,126 @@
1
+package com.ruoyi.wisdomarbitrate.utils;
2
+
3
+import lombok.extern.slf4j.Slf4j;
4
+
5
+import java.io.File;
6
+import java.io.FileOutputStream;
7
+import java.io.IOException;
8
+import java.io.InputStream;
9
+import java.nio.charset.Charset;
10
+import java.util.ArrayList;
11
+import java.util.Enumeration;
12
+import java.util.List;
13
+import java.util.zip.ZipEntry;
14
+import java.util.zip.ZipFile;
15
+
16
+@Slf4j
17
+public class UnZipFileUtils {
18
+
19
+    public static void main(String[] args) {
20
+        // 压缩包路径
21
+//        String zipPath = "F:\\testZip\\zipFile\\上传申请书证据材料.zip";
22
+        // 解压后存放路径
23
+//        String targetPath =  "F:\\testZip\\unzipFile";
24
+//        unZipFile(new File(zipPath),targetPath);
25
+
26
+
27
+//        String zipPath = "F:\\testZip\\unzipFile\\上传申请书证据材料";
28
+//        File dirUnzipPath = new File(zipPath);
29
+//        List<File> allFiles = new ArrayList<>();
30
+//        getFiles(dirUnzipPath,allFiles);
31
+//        if(allFiles!=null&&allFiles.size()>0){
32
+//            for (File fileIter : allFiles) {
33
+//                System.out.println(fileIter.getName());
34
+//                System.out.println(fileIter.getAbsolutePath());
35
+//            }
36
+//        }
37
+
38
+         String key = "\\";
39
+         String str = "F:\\testZip\\上传申请书证据材料\\证据材料\\仲裁裁决书模板.docx";
40
+        List<Integer> allindex = new ArrayList<>();
41
+        int a = str.indexOf(key);//*第一个出现的索引位置
42
+        allindex.add(a);
43
+        while (a != -1) {
44
+            System.out.print(a + "\t");
45
+            a = str.indexOf(key, a + 1);//*从这个索引往后开始第一个出现的位置
46
+            allindex.add(a);
47
+        }
48
+        String substr = str.substring(allindex.get(1)+1,allindex.get(2));
49
+        System.out.println("substr---"+substr);
50
+
51
+
52
+    }
53
+
54
+    public static boolean unZipFile(File aboriginalFile, String targetPath) {
55
+        if (!aboriginalFile.exists()) {
56
+            log.error("此文件不存在:",aboriginalFile.getPath());
57
+            return false;
58
+        }
59
+        try {
60
+            ZipFile srcZipFile = new ZipFile(aboriginalFile, Charset.forName("GBK"));
61
+            for (Enumeration enumeration = srcZipFile.entries(); enumeration.hasMoreElements(); ) {
62
+                ZipEntry entryZip = (ZipEntry) enumeration.nextElement();
63
+                //若是文件夹,就创建文件夹
64
+                if (entryZip.isDirectory()) {
65
+                    String directPath = targetPath + File.separator + entryZip.getName();
66
+                    File directFile = new File(directPath);
67
+                    boolean makeDirs = directFile.mkdirs();
68
+                    System.out.println("创建文件夹是否成功:" +makeDirs);
69
+                } else {
70
+                    //若是文件,就创建文件
71
+                    File sourceFile = new File(targetPath + File.separator + entryZip.getName());
72
+                    if(!sourceFile.getParentFile().exists()){
73
+                        boolean mDirect = sourceFile.getParentFile().mkdirs();
74
+                        System.out.println("创建压缩文件时,创建父文件夹是否成功:" +mDirect);
75
+                    }
76
+                    boolean newUnZipFile = sourceFile.createNewFile();
77
+                    if (newUnZipFile){
78
+                        // 将压缩文件内容写入到这个文件中
79
+                        try{
80
+                            InputStream is = srcZipFile.getInputStream(entryZip);
81
+                            FileOutputStream fos = new FileOutputStream(sourceFile);
82
+                            int len;
83
+                            byte[] buf = new byte[2048];
84
+                            while ((len = is.read(buf)) != -1) {
85
+                                fos.write(buf, 0, len);
86
+                            }
87
+                        } catch (Exception e){
88
+                            log.error("解压失败",e);
89
+                        }
90
+                    }
91
+                }
92
+            }
93
+            return true;
94
+
95
+        } catch (IOException e) {
96
+            log.error("解压失败",e);
97
+            return false;
98
+        }
99
+
100
+    }
101
+
102
+
103
+    public static void getFiles(File fileSource, List<File> allFiles) {
104
+        if (!fileSource.exists()) {
105
+            System.out.println("目录不存在");
106
+            return;
107
+        }
108
+        File[] fileArray = fileSource.listFiles();
109
+        if(fileArray!=null&&fileArray.length>0){
110
+            for (File fileIter : fileArray) {
111
+                if (fileIter.isDirectory()) {
112
+                    getFiles(fileIter,allFiles);
113
+                }else {
114
+                    allFiles.add(fileIter);
115
+                }
116
+
117
+            }
118
+
119
+        }
120
+    }
121
+
122
+
123
+
124
+
125
+
126
+}