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 }