java中zip导入


1.MultipartFile对象接收前端传入文件 

2.对zip文件进行校验

 1      @AutoLog(value = "设计文档-zip文件批量导入(需提前导入树结构)")
 2      @ApiOperation(value = "设计文档-zip文件批量导入(需提前导入树结构)", notes = "设计文档-zip文件批量导入(需提前导入树结构)")
 3      @RequestMapping(value = "/addZip", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
 4      @Transactional
 5      public ResponseEntity addZip(@RequestParam("file") MultipartFile file) {
 6          Result<JSONObject> result = new Result<>();
 7          JSONObject jsonObject = new JSONObject();
 8          if (file == null) {
 9              log.info("function=add, file is null!");
10              result.error500("上传的文件为null!");
11              return new ResponseEntity<Result<JSONObject>>(result, HttpStatus.BAD_REQUEST);
12          }
13          //校验文件大小,超过2GB不允许上传
14          long fileSize = file.getSize();
15          if(fileSize > CmpConstant.TWO_GB) {
16              result.error500("文件大小超过2GB");
17              return new ResponseEntity<Result<JSONObject>>(result, HttpStatus.BAD_REQUEST);
18          }
19          //MD5Util.getMD5(file);
20          String md5 = "0";
21          String fileName = file.getOriginalFilename();
22          log.info("function=add, md5={} , fileName={}", md5, fileName);
23          jsonObject.put("fileTokens", null);
24          if (StringUtils.isEmpty(md5) || StringUtils.isEmpty(fileName)) {
25              log.info("function=add,md5 or fileName is empty! md5={} , fileName={}", md5, fileName);
26              result.error500("md5或fileName为空!");
27              return new ResponseEntity<Result<JSONObject>>(result, HttpStatus.BAD_REQUEST);
28          }
29          int fileNameLength = fileName.length();
30          if(fileNameLength > 50) {
31              log.info("function=add, fileNameLength greater than 50!");
32              result.error500("文件名长度超过50字节!");
33              return new ResponseEntity<Result<JSONObject>>(result, HttpStatus.BAD_REQUEST);
34          }
35 
36          String multipartFileName = file.getOriginalFilename();
37          if (StringUtils.isEmpty(multipartFileName)) {
38              log.info("function=add, multipartFileName is empty!");
39              result.setResult(jsonObject);
40              result.error500("上传的文件名为空!");
41              return new ResponseEntity<Result<JSONObject>>(result, HttpStatus.BAD_REQUEST);
42          }
43          try{
44              Future<designDoc> uploadResult = designDocService.fileUploadAndSave(file, md5, null,"0");
45              designDoc docsignDocInstance = uploadResult.get();
46              jsonObject.put("fileTokens", docsignDocInstance.getId());
47              jsonObject.put("docsignDoc", docsignDocInstance);
48              log.info("function=add, result JSONObject={}", jsonObject);
49              result.setResult(jsonObject);
50              result.success("上传成功!");
51              return new ResponseEntity<Result<JSONObject>>(result, HttpStatus.CREATED);
52          } catch (Exception e){
53              result.error500(e.getMessage());
54              result.setSuccess(false);
55              return new ResponseEntity<Result<JSONObject>>(result, HttpStatus.BAD_REQUEST);
56          }
57      }

 

3.根据传入file,保存zip文件

 1 File file = new File(fileSavePath);
 2         createFile(file);
 3         try {
 4             try {
 5                 InputStream stream = multipartFile.getInputStream();
 6                 FileOutputStream out = new FileOutputStream(file);
 7                 int read;
 8                 final byte[] bytes = new byte[1024];
 9                 while ((read = stream.read(bytes)) != -1) {
10                     out.write(bytes, 0, read);
11                 }
12 
13                 out.flush();
14                 stream.close();
15             } catch (Exception e) {
16 
17             }
18 
19             log.info("function=fileUploadAndSave, upload succeed!");
20         } catch (Throwable e) {
21             e.printStackTrace();
22             log.info("function=fileUploadAndSave, upload fail!");
23             return null;
24         }
25 
26 private void createFile(File file) {
27         if (file.exists()) {
28             System.out.println("exists");
29         } else {
30             try {
31                 File pf = file.getParentFile();
32                 if (pf == null) {
33                     file.createNewFile();
34                 } else {
35                     if (!pf.exists()) {
36                         pf.mkdirs();
37                     }
38                     file.createNewFile();
39                 }
40                 System.out.println("mkdirs");
41             } catch (Exception e) {
42                 System.out.println("IOException");
43                 e.printStackTrace();
44             }
45 
46         }
47     }

 

 

4.解压缩文件到指定目录

1  // 解压缩文件到指定目录
2         String unzipPath = fileSavePath.substring(0,fileSavePath.lastIndexOf("."));
3         FileUnZip.zipToFile(fileSavePath, unzipPath);
4         File modelPropPath = new File(unzipPath);
5         File[] modelPropFiles = modelPropPath.listFiles();

 

5.扫描解压缩的文件,将所有模型属性数据并入库

1  List<UploadFile> uploadFileList = new ArrayList<>();
2         List<DesignDocTree> designDocTreeModelList = new ArrayList<>();
3         List<designDoc> designdocList = new ArrayList<>();
4         JSONObject codeObject = new JSONObject();
5         int treeI = 0;
6         for (File docsignDocFile : modelPropFiles) {
7         //业务处理     
8 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM