/** * 解压解析ZIP文件生成行程信息 * * @param file * @throws IOException * @throws IllegalStateException */ @Transactional @SuppressWarnings("rawtypes") public void doUploadVisitReportZip(MultipartFile file) throws IllegalStateException, IOException { FileOutputStream outStream = null; InputStream inputStream = null; try { // 创建新行程 VisitReport visitReport = new VisitReport(); // 上传的ZIP文件目录 String zipCalculateRealPath = uploadFileService.zipCalculateRealPath() + File.separator; File saveFile = new File(zipCalculateRealPath + file.getOriginalFilename()); // 保存ZIP文件到磁盘 if (saveFile.exists()) { saveFile.delete(); } if (!saveFile.exists()) { file.transferTo(saveFile); } // 读取ZIP文件 ZipFile zipFile = new ZipFile(saveFile); // 遍历ZIP文件 for (Enumeration entities = zipFile.getEntries(); entities.hasMoreElements();) { ZipEntry zipEntry = (ZipEntry) entities.nextElement(); String unZipPath = ZipUtil.getUnZipPath(zipEntry.getName(), zipCalculateRealPath); File unZipFile = new File(unZipPath); ZipUtil.mkdirs(zipEntry, unZipFile); // 从ZIP条目获得输入流 inputStream = zipFile.getInputStream(zipEntry); // 如果不是目录 if (!zipEntry.isDirectory()) { outStream = new FileOutputStream(unZipFile); // 获得写入磁盘的输出流 ZipUtil.write2Disk(outStream, inputStream); } } // 读取Excel生成行程Visit Report String excelFilePath = zipCalculateRealPath + uploadFileService.breakSuffix(file.getOriginalFilename())[0] + File.separator + "visit-info.xls"; InputStream is = new FileInputStream(excelFilePath); // 读取excel属性赋值 visitReport = readExcelContent(visitReport, is); dao.saveOrUpdate(visitReport); } catch (Exception e) { e.printStackTrace(); } finally { try { if (outStream != null) { outStream.close(); } if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } 如题,功能是上传一个zip到服务器,然后保存解压,解析里面的内容 |
|
50分 |
同求,也遇到了,上传文件,文件名重复,然后文件被占用的问题,没解决,
加了个System.gc() 貌似没什么效果,图个心里安慰 |
顶上去
|
|
50分 |
俺一般是用file.getFileItem().getInputStream() 文件流读取的,这个可以关闭
|
结贴,每个上传文件加时间戳,定时清理文件就行了
|