From 83575b1b4a208454ad063f12d356fc760851f3c4 Mon Sep 17 00:00:00 2001 From: yuyang <2265829957@qq.com> Date: Fri, 5 Nov 2021 19:55:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E7=BB=B4=E7=A0=81=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/XhpcDownCodeController.java | 16 +++-- .../station/service/IXhpcDownCodeService.java | 4 +- .../service/XhpcDownCodeServiceImpl.java | 71 ++++++++++++------- .../station/utils/img/DownMaterialUtil.java | 4 +- 4 files changed, 62 insertions(+), 33 deletions(-) diff --git a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/controller/XhpcDownCodeController.java b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/controller/XhpcDownCodeController.java index a56f2af1..4250b622 100644 --- a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/controller/XhpcDownCodeController.java +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/controller/XhpcDownCodeController.java @@ -2,8 +2,10 @@ package com.xhpc.charging.station.controller; import com.xhpc.charging.station.service.IXhpcDownCodeService; import com.xhpc.common.core.web.controller.BaseController; +import com.xhpc.common.core.web.domain.AjaxResult; import com.xhpc.common.core.web.page.TableDataInfo; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -20,6 +22,8 @@ public class XhpcDownCodeController extends BaseController { @Autowired private IXhpcDownCodeService iXhpcDownCodeService; + @Autowired + private Environment environment; @GetMapping(value = "/listStations") public TableDataInfo listStations(String stationName){ @@ -36,14 +40,18 @@ public class XhpcDownCodeController extends BaseController { } @GetMapping(value = "/downMaterialByPileId") - public void downMaterialBy(@RequestParam Long pileId) throws Exception { + public AjaxResult downMaterialBy(@RequestParam Long pileId) throws Exception { - iXhpcDownCodeService.downMaterialBy(pileId); + String s = iXhpcDownCodeService.downMaterialBy(pileId); + + return AjaxResult.success(environment.getProperty("img.getUrl")+s); } @GetMapping(value = "/downMaterialByStationId") - public void downMaterialByStationId(@RequestParam Long stationId) throws Exception{ + public AjaxResult downMaterialByStationId(@RequestParam Long stationId) throws Exception{ - iXhpcDownCodeService.downMaterialByStationId(stationId); + String s =iXhpcDownCodeService.downMaterialByStationId(stationId); + + return AjaxResult.success(environment.getProperty("img.getUrl")+s); } } diff --git a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/IXhpcDownCodeService.java b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/IXhpcDownCodeService.java index fcb37a2e..a56c42aa 100644 --- a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/IXhpcDownCodeService.java +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/IXhpcDownCodeService.java @@ -14,7 +14,7 @@ public interface IXhpcDownCodeService { List> listPiles(Long stationId); - void downMaterialBy(Long pileId) throws Exception; + String downMaterialBy(Long pileId) throws Exception; - void downMaterialByStationId(Long stationId) throws Exception; + String downMaterialByStationId(Long stationId) throws Exception; } diff --git a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcDownCodeServiceImpl.java b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcDownCodeServiceImpl.java index c1a7b72f..3cd39c15 100644 --- a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcDownCodeServiceImpl.java +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcDownCodeServiceImpl.java @@ -9,6 +9,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import java.io.File; @@ -29,6 +30,9 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService { @Autowired private XhpcDownCodeMapper xhpcDownCodeMapper; + @Autowired + private Environment environment; + private static final Logger logger = LoggerFactory.getLogger(XhpcDownCodeServiceImpl.class); @Override @@ -44,11 +48,10 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService { } @Override - public void downMaterialBy(Long pileId) throws Exception { + public String downMaterialBy(Long pileId) throws Exception { List> list = xhpcDownCodeMapper.selectExcelDataByPileId(pileId); - System.out.println(list); - File file1 = new File("/www/wwwroot/xhpc.scxhua.com/dist/Material"); + File file1 = new File(environment.getProperty("img.url")); File file2 = new File(file1, "pictures"); if(file1.exists()){ deleteFiles(file1); @@ -56,7 +59,6 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService { boolean flag = file1.mkdir(); if(! flag) { System.out.println(file1.getName()+"文件夹创建失败!"); - return; } boolean flag1 = file2.mkdir(); if(! flag1){ @@ -69,7 +71,7 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService { //3、创建头部第一行 HSSFRow headRow = sheet.createRow(0); HSSFCell cell; - String[] header = new String[] {"终端名称","归属电站","终端编码","电桩编码","终端类型(1直流(慢) 2交流(快)","电桩功率(KM)"}; + String[] header = new String[] {"终端名称","归属电站","终端编码","电桩编码","终端类型(1直流(快) 2交流(慢)","电桩功率(KM)"}; for (int i = 0; i < header.length; i++) { cell = headRow.createCell(i); cell.setCellValue(header[i]); @@ -87,31 +89,40 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService { if(null != map.get(values[j])){ dataCell = dataRow.createCell(j); String s = String.valueOf(map.get(values[j])); - dataCell.setCellValue(s); + if (j==4) { + if("1".equals(s)){ + dataCell.setCellValue("直流"); + }else{ + dataCell.setCellValue("交流"); + } + }else{ + dataCell.setCellValue(s); + } } } } } - System.out.println(list); //5、写入 - OutputStream fos = new FileOutputStream(file1.getAbsoluteFile() + "/data.xls"); + OutputStream fos = new FileOutputStream(environment.getProperty("img.url") + "/data.xls"); workbook.write(fos); workbook.close(); fos.close(); String left = "https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/"; List imgUrls = xhpcDownCodeMapper.selectUrlsByPile(pileId); - System.out.println(imgUrls); List list1 = DownMaterialUtil.getDownLoadUrls(left,imgUrls); DownMaterialUtil.saveImageToDisk(list1,file2,imgUrls); - File file = new File(file1.getAbsolutePath()); + File file = new File(environment.getProperty("img.url")); + //储存路径 + long l = System.currentTimeMillis(); + String url=environment.getProperty("img.url")+l; - logger.info("file地址:"+file1.getAbsolutePath()); - DownMaterialUtil.fileToZip(file); - System.out.println("success"); + DownMaterialUtil.fileToZip(file,url); + + return "Material"+l+".zip"; } @Override - public void downMaterialByStationId(Long stationId) throws Exception { + public String downMaterialByStationId(Long stationId) throws Exception { List> list = xhpcDownCodeMapper.selectExcelDataByStationId(stationId); System.out.println(list); @@ -123,7 +134,6 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService { boolean flag = file1.mkdir(); if (!flag) { System.out.println(file1.getName() + "文件夹创建失败!"); - return; } boolean flag1 = file2.mkdir(); if (!flag1) { @@ -136,7 +146,7 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService { //3、创建头部第一行 HSSFRow headRow = sheet.createRow(0); HSSFCell cell; - String[] header = new String[]{"终端名称", "归属电站", "终端编码", "电桩编码", "终端类型(1直流(慢) 2交流(快)", "电桩功率(KM)"}; + String[] header = new String[]{"终端名称", "归属电站", "终端编码", "电桩编码", "终端类型(1直流(快) 2交流(慢)", "电桩功率(KM)"}; for (int i = 0; i < header.length; i++) { cell = headRow.createCell(i); cell.setCellValue(header[i]); @@ -154,25 +164,36 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService { if (null != map.get(values[j])) { dataCell = dataRow.createCell(j); String s = String.valueOf(map.get(values[j])); - dataCell.setCellValue(s); + if (j==4) { + if("1".equals(s)){ + dataCell.setCellValue("直流"); + }else{ + dataCell.setCellValue("交流"); + } + }else{ + dataCell.setCellValue(s); + } } } } } - System.out.println(list); //5、写入 - OutputStream fos = new FileOutputStream(file1.getAbsoluteFile() + "/data.xls"); + OutputStream fos = new FileOutputStream(environment.getProperty("img.url") + "/data.xls"); workbook.write(fos); workbook.close(); fos.close(); String left = "https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/"; List imgUrls = xhpcDownCodeMapper.selectUrlsByStation(stationId); - System.out.println(imgUrls); List list1 = DownMaterialUtil.getDownLoadUrls(left, imgUrls); DownMaterialUtil.saveImageToDisk(list1, file2, imgUrls); - File file = new File(file1.getAbsolutePath()); - DownMaterialUtil.fileToZip(file); - System.out.println("success"); + File file = new File(environment.getProperty("img.url")); + //储存路径 + long l = System.currentTimeMillis(); + String url=environment.getProperty("img.url")+l; + + DownMaterialUtil.fileToZip(file,url); + + return "Material"+l+".zip"; } public void deleteFiles(File file){ @@ -183,11 +204,11 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService { if (value.isDirectory()) { deleteFiles(value); } else { - System.out.println("里层文件:" + value.getName() + "--------" + value.delete()); + //System.out.println("里层文件:" + value.getName() + "--------" + value.delete()); } } } } - System.out.println("外层文件:"+file.getName()+"--------"+file.delete()); + //System.out.println("外层文件:"+file.getName()+"--------"+file.delete()); } } diff --git a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/img/DownMaterialUtil.java b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/img/DownMaterialUtil.java index 6486cb5b..526b32da 100644 --- a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/img/DownMaterialUtil.java +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/img/DownMaterialUtil.java @@ -91,7 +91,7 @@ public class DownMaterialUtil { } } - public static void fileToZip(File sourceFile) throws Exception { + public static void fileToZip(File sourceFile,String url) throws Exception { if (!sourceFile.exists()) { throw new RuntimeException("不存在"); @@ -100,7 +100,7 @@ public class DownMaterialUtil { throw new RuntimeException("不是文件夹"); } //zip文件生成位置 - File zipFile = new File(sourceFile.getAbsolutePath() + ".zip"); + File zipFile = new File(url + ".zip"); FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos)); fileToZip(zos, sourceFile, "");