| | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | import com.jcraft.jsch.SftpException; |
| | | import com.panzhihua.common.utlis.*; |
| | | import org.apache.commons.io.FileUtils; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | |
| | | import com.panzhihua.common.model.vos.community.*; |
| | | import com.panzhihua.common.service.community.CommunityService; |
| | | import com.panzhihua.common.service.partybuilding.PartyBuildingService; |
| | | import com.panzhihua.common.utlis.HttpUtils; |
| | | import com.panzhihua.common.utlis.SFTPUtil; |
| | | import com.panzhihua.common.utlis.StringUtils; |
| | | import com.panzhihua.community_backstage.excel.CustomSheetWriteHandler; |
| | | |
| | | import freemarker.template.Configuration; |
| | |
| | | @PostMapping("/export/safetyWorkRecord") |
| | | public R exportSafetyWorkRecord(@RequestBody List<Long> ids) { |
| | | Long communityId = this.getLoginUserInfo().getCommunityId(); |
| | | List<String> downLoadUrl = new ArrayList<>(); |
| | | try { |
| | | for (Long id : ids) { |
| | | SFTPUtil sftp = new SFTPUtil(userName, password, host, port); |
| | | sftp.login(); |
| | | String property = System.getProperty("user.dir"); |
| | | String sourceFile =property+File.separator+"word"+File.separator+System.currentTimeMillis()+File.separator; |
| | | String zipFile=property+File.separator+"zip"+File.separator; |
| | | String ftpUrl = "/mnt/data/web/excel/"; |
| | | for (Long id : ids) { |
| | | try { |
| | | Map<String, Object> dataMap = new HashMap<String, Object>(); |
| | | ComSwSafetyWorkRecordVO comSwSafetyWorkRecordVO = JSONObject.parseObject( |
| | | JSONObject.toJSONString(communityService.detailSafetyWorkRecord(id, communityId).getData()), |
| | | ComSwSafetyWorkRecordVO.class); |
| | | // 生成动态模板excel通过ftp工具上传到主节点,然后返回模板下载地址 |
| | | String ftpUrl = "/mnt/data/web/excel/"; |
| | | String name = "安全工作记录_" + comSwSafetyWorkRecordVO.getId() + ".doc"; |
| | | boolean existDir = sftp.isExistDir(ftpUrl + name); |
| | | if (!existDir) { |
| | | String property = System.getProperty("user.dir"); |
| | | String fileName = property + File.separator + name; |
| | | // 这里 需要指定写用哪个class去写 |
| | | ExcelWriter excelWriter = null; |
| | | InputStream inputStream = null; |
| | | File file0=new File("d:/safetyWork/file"); |
| | | if(!file0.isDirectory()&&!file0.exists()){ |
| | | file0.mkdirs(); |
| | | } |
| | | file0 = new File(sourceFile); |
| | | if(!file0.isDirectory()&&!file0.exists()){ |
| | | file0.mkdirs(); |
| | | } |
| | |
| | | // 输出文档路径及名称 |
| | | // File outFile = new File("mnt/data/web/excel/安全工作记录_" + comSwSafetyWorkRecordVO.getId() + |
| | | // ".doc"); |
| | | File file = new File(fileName); |
| | | File file = new File(sourceFile + name); |
| | | |
| | | // 以utf-8的编码读取ftl文件 |
| | | Template template = configuration.getTemplate("安全工作记录.ftl", "utf-8"); |
| | |
| | | new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8), 10240); |
| | | template.process(dataMap, out); |
| | | out.close(); |
| | | inputStream = new FileInputStream(file); |
| | | sftp.uploadMore(ftpUrl, name, inputStream); |
| | | sftp.logout(); |
| | | inputStream.close(); |
| | | String absolutePath = file.getAbsolutePath(); |
| | | boolean delete = file.delete(); |
| | | log.info("删除excel【{}】结果【{}】", absolutePath, delete); |
| | | // inputStream = new FileInputStream(file); |
| | | // sftp.uploadMore(ftpUrl, name, inputStream); |
| | | // sftp.logout(); |
| | | // inputStream.close(); |
| | | // String absolutePath = file.getAbsolutePath(); |
| | | // boolean delete = file.delete(); |
| | | // log.info("删除excel【{}】结果【{}】", absolutePath, delete); |
| | | } finally { |
| | | // 千万别忘记finish 会帮忙关闭流 |
| | | if (inputStream != null) { |
| | |
| | | } |
| | | } |
| | | } |
| | | downLoadUrl.add(excelUrl + name); |
| | | } |
| | | return R.ok(downLoadUrl); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("文件传输失败【{}】", e.getMessage()); |
| | | } |
| | | return R.fail(); |
| | | } |
| | | FileUtil.compressToZip(sourceFile,zipFile,"安全工作记录.zip"); |
| | | String currentDateString = String.valueOf(System.currentTimeMillis()); |
| | | String name = "安全工作记录_"+ currentDateString+".zip"; |
| | | try { |
| | | InputStream inputStream=new FileInputStream(zipFile+"安全工作记录.zip"); |
| | | sftp.uploadMore(ftpUrl, name , inputStream); |
| | | sftp.logout(); |
| | | inputStream.close(); |
| | | FileUtils.deleteDirectory(new File(property+File.separator+"word"+File.separator)); |
| | | FileUtils.deleteDirectory(new File(zipFile)); |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (SftpException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return R.ok(excelUrl + name); |
| | | } |
| | | |
| | | @ApiOperation(value = "下载巡查记录报告") |
| | | @PostMapping("/export/patrolRecord") |
| | | public R exportPatrolRecord(@RequestBody List<Long> ids) { |
| | | Long communityId = this.getLoginUserInfo().getCommunityId(); |
| | | List<String> downLoadUrl = new ArrayList<>(); |
| | | try { |
| | | for (Long id : ids) { |
| | | SFTPUtil sftp = new SFTPUtil(userName, password, host, port); |
| | | sftp.login(); |
| | | String property = System.getProperty("user.dir"); |
| | | String sourceFile =property+File.separator+"word"+File.separator+System.currentTimeMillis()+File.separator; |
| | | String zipFile=property+File.separator+"zip"+File.separator; |
| | | String ftpUrl = "/mnt/data/web/excel/"; |
| | | for (Long id : ids) { |
| | | try { |
| | | Map<String, Object> dataMap = new HashMap<String, Object>(); |
| | | ComSwPatrolRecordVO comSwPatrolRecordVO = |
| | | JSONObject.parseObject(JSONObject.toJSONString(communityService.detailPatrolRecord(id).getData()), |
| | | ComSwPatrolRecordVO.class); |
| | | // 生成动态模板excel通过ftp工具上传到主节点,然后返回模板下载地址 |
| | | String ftpUrl = "/mnt/data/web/excel/"; |
| | | String name = "巡查记录_" + comSwPatrolRecordVO.getId() + ".doc"; |
| | | |
| | | boolean existDir = sftp.isExistDir(ftpUrl + name); |
| | | if (!existDir) { |
| | | String property = System.getProperty("user.dir"); |
| | | String fileName = property + File.separator + name; |
| | | // 这里 需要指定写用哪个class去写 |
| | | ExcelWriter excelWriter = null; |
| | | InputStream inputStream = null; |
| | | File file0=new File("d:/patrolRecord/file"); |
| | | if(!file0.isDirectory()&&!file0.exists()){ |
| | | file0.mkdirs(); |
| | | } |
| | | file0 = new File(sourceFile); |
| | | if(!file0.isDirectory()&&!file0.exists()){ |
| | | file0.mkdirs(); |
| | | } |
| | |
| | | // 输出文档路径及名称 |
| | | // File outFile = new File("mnt/data/web/excel/安全工作记录_" + comSwSafetyWorkRecordVO.getId() + |
| | | // ".doc"); |
| | | File file = new File(fileName); |
| | | File file = new File(sourceFile + name); |
| | | |
| | | // 以utf-8的编码读取ftl文件 |
| | | Template template = configuration.getTemplate("巡查记录.ftl", "utf-8"); |
| | |
| | | new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8), 10240); |
| | | template.process(dataMap, out); |
| | | out.close(); |
| | | inputStream = new FileInputStream(file); |
| | | sftp.uploadMore(ftpUrl, name, inputStream); |
| | | sftp.logout(); |
| | | inputStream.close(); |
| | | String absolutePath = file.getAbsolutePath(); |
| | | boolean delete = file.delete(); |
| | | log.info("删除excel【{}】结果【{}】", absolutePath, delete); |
| | | // inputStream = new FileInputStream(file); |
| | | // sftp.uploadMore(ftpUrl, name, inputStream); |
| | | // sftp.logout(); |
| | | // inputStream.close(); |
| | | // String absolutePath = file.getAbsolutePath(); |
| | | // boolean delete = file.delete(); |
| | | // log.info("删除excel【{}】结果【{}】", absolutePath, delete); |
| | | } finally { |
| | | // 千万别忘记finish 会帮忙关闭流 |
| | | if (inputStream != null) { |
| | |
| | | } |
| | | } |
| | | } |
| | | downLoadUrl.add(excelUrl + name); |
| | | } |
| | | return R.ok(downLoadUrl); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("文件传输失败【{}】", e.getMessage()); |
| | | } |
| | | return R.fail(); |
| | | } |
| | | FileUtil.compressToZip(sourceFile,zipFile,"巡查记录.zip"); |
| | | String currentDateString = String.valueOf(System.currentTimeMillis()); |
| | | String name = "巡查记录_"+ currentDateString+".zip"; |
| | | try { |
| | | InputStream inputStream=new FileInputStream(zipFile+"巡查记录.zip"); |
| | | sftp.uploadMore(ftpUrl, name , inputStream); |
| | | sftp.logout(); |
| | | inputStream.close(); |
| | | FileUtils.deleteDirectory(new File(property+File.separator+"word"+File.separator)); |
| | | FileUtils.deleteDirectory(new File(zipFile)); |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (SftpException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return R.ok(excelUrl + name); |
| | | } |
| | | |
| | | @ApiOperation(value = "下载隐患报告") |
| | | @PostMapping("/export/DangerReport") |
| | | public R exportDangerReport(@RequestBody List<Long> ids) { |
| | | Long communityId = this.getLoginUserInfo().getCommunityId(); |
| | | List<String> downLoadUrl = new ArrayList<>(); |
| | | try { |
| | | for (Long id : ids) { |
| | | SFTPUtil sftp = new SFTPUtil(userName, password, host, port); |
| | | sftp.login(); |
| | | String property = System.getProperty("user.dir"); |
| | | String sourceFile =property+File.separator+"word"+File.separator+System.currentTimeMillis()+File.separator; |
| | | String zipFile=property+File.separator+"zip"+File.separator; |
| | | String ftpUrl = "/mnt/data/web/excel/"; |
| | | for (Long id : ids) { |
| | | try { |
| | | Map<String, Object> dataMap = new HashMap<String, Object>(); |
| | | ComSwDangerReportVO comSwDangerReportVO = |
| | | JSONObject.parseObject(JSONObject.toJSONString(communityService.detailDangerReport(id).getData()), |
| | | ComSwDangerReportVO.class); |
| | | // 生成动态模板excel通过ftp工具上传到主节点,然后返回模板下载地址 |
| | | String ftpUrl = "/mnt/data/web/excel/"; |
| | | String name = "隐患报告_" + comSwDangerReportVO.getId() + ".doc"; |
| | | |
| | | boolean existDir = sftp.isExistDir(ftpUrl + name); |
| | | if (!existDir) { |
| | | String property = System.getProperty("user.dir"); |
| | | String fileName = property + File.separator + name; |
| | | // 这里 需要指定写用哪个class去写 |
| | | ExcelWriter excelWriter = null; |
| | | InputStream inputStream = null; |
| | | File file0=new File("d:/dangerReport/file"); |
| | | if(!file0.isDirectory()&&!file0.exists()){ |
| | | file0.mkdirs(); |
| | | } |
| | | file0 = new File(sourceFile); |
| | | if(!file0.isDirectory()&&!file0.exists()){ |
| | | file0.mkdirs(); |
| | | } |
| | |
| | | // 输出文档路径及名称 |
| | | // File outFile = new File("mnt/data/web/excel/安全工作记录_" + comSwSafetyWorkRecordVO.getId() + |
| | | // ".doc"); |
| | | File file = new File(fileName); |
| | | File file = new File(sourceFile+name); |
| | | |
| | | // 以utf-8的编码读取ftl文件 |
| | | Template template = configuration.getTemplate("隐患报告.ftl", "utf-8"); |
| | |
| | | new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8), 10240); |
| | | template.process(dataMap, out); |
| | | out.close(); |
| | | inputStream = new FileInputStream(file); |
| | | sftp.uploadMore(ftpUrl, name, inputStream); |
| | | sftp.logout(); |
| | | inputStream.close(); |
| | | String absolutePath = file.getAbsolutePath(); |
| | | boolean delete = file.delete(); |
| | | log.info("删除excel【{}】结果【{}】", absolutePath, delete); |
| | | // inputStream = new FileInputStream(file); |
| | | // sftp.uploadMore(ftpUrl, name, inputStream); |
| | | // sftp.logout(); |
| | | // inputStream.close(); |
| | | // String absolutePath = file.getAbsolutePath(); |
| | | // boolean delete = file.delete(); |
| | | // log.info("删除excel【{}】结果【{}】", absolutePath, delete); |
| | | } finally { |
| | | // 千万别忘记finish 会帮忙关闭流 |
| | | if (inputStream != null) { |
| | |
| | | } |
| | | } |
| | | } |
| | | downLoadUrl.add(excelUrl + name); |
| | | } |
| | | return R.ok(downLoadUrl); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("文件传输失败【{}】", e.getMessage()); |
| | | } |
| | | return R.fail(); |
| | | } |
| | | FileUtil.compressToZip(sourceFile,zipFile,"隐患报告.zip"); |
| | | String currentDateString = String.valueOf(System.currentTimeMillis()); |
| | | String name = "隐患报告_"+ currentDateString+".zip"; |
| | | try { |
| | | InputStream inputStream=new FileInputStream(zipFile+"隐患报告.zip"); |
| | | sftp.uploadMore(ftpUrl, name , inputStream); |
| | | sftp.logout(); |
| | | inputStream.close(); |
| | | FileUtils.deleteDirectory(new File(property+File.separator+"word"+File.separator)); |
| | | FileUtils.deleteDirectory(new File(zipFile)); |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (SftpException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return R.ok(excelUrl + name); |
| | | } |
| | | |
| | | private List<List<String>> headDataFilling() { |