From 2d93c365b97403d9e120e4c2a1d12335374d25b0 Mon Sep 17 00:00:00 2001 From: little-cat-sweet <851891179@qq.com> Date: Fri, 19 Nov 2021 10:46:06 +0800 Subject: [PATCH] Solving a bug from downCode. --- .../controller/XhpcDownCodeController.java | 22 ++- .../station/mapper/XhpcDownCodeMapper.java | 2 + .../station/service/IXhpcDownCodeService.java | 4 + .../service/XhpcDownCodeServiceImpl.java | 162 +++++++++++++++++- .../station/utils/img/DownMaterialUtil.java | 62 ++++++- 5 files changed, 244 insertions(+), 8 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 4250b622..63d0aa77 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 @@ -44,14 +44,26 @@ public class XhpcDownCodeController extends BaseController { String s = iXhpcDownCodeService.downMaterialBy(pileId); - return AjaxResult.success(environment.getProperty("img.getUrl")+s); + return AjaxResult.success(environment.getProperty("img.getUrl") + s); } - + @GetMapping(value = "/downMaterialByStationId") - public AjaxResult downMaterialByStationId(@RequestParam Long stationId) throws Exception{ + public AjaxResult downMaterialByStationId(@RequestParam Long stationId) throws Exception { - String s =iXhpcDownCodeService.downMaterialByStationId(stationId); + String s = iXhpcDownCodeService.downMaterialByStationId(stationId); - return AjaxResult.success(environment.getProperty("img.getUrl")+s); + return AjaxResult.success(environment.getProperty("img.getUrl") + s); } + +// @GetMapping(value = "/downMaterialByPileId1") +// public void downMaterialBy1(@RequestParam Long pileId) throws Exception { +// +// iXhpcDownCodeService.downMaterialBy11(pileId); +// } +// +// @GetMapping(value = "/downMaterialByStationId1") +// public void downMaterialByStationId1(@RequestParam Long stationId) throws Exception{ +// +// iXhpcDownCodeService.downMaterialByStationId11(stationId); +// } } diff --git a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/mapper/XhpcDownCodeMapper.java b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/mapper/XhpcDownCodeMapper.java index c00809a7..599d48f4 100644 --- a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/mapper/XhpcDownCodeMapper.java +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/mapper/XhpcDownCodeMapper.java @@ -1,5 +1,6 @@ package com.xhpc.charging.station.mapper; +import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; @@ -10,6 +11,7 @@ import java.util.Map; * User: HongYun * Date:2021-09-30 15 */ +@Mapper public interface XhpcDownCodeMapper { List> selectStationsBy(@Param(value = "stationName") String stationName); 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 a56c42aa..42ec749c 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 @@ -17,4 +17,8 @@ public interface IXhpcDownCodeService { String downMaterialBy(Long pileId) throws Exception; String downMaterialByStationId(Long stationId) throws Exception; +// +// void downMaterialBy11(Long pileId) throws Exception; +// +// void downMaterialByStationId11(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 3cd39c15..1a186ac8 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 @@ -54,7 +54,7 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService { File file1 = new File(environment.getProperty("img.url")); File file2 = new File(file1, "pictures"); if(file1.exists()){ - deleteFiles(file1); + delFile(file1); } boolean flag = file1.mkdir(); if(! flag) { @@ -129,7 +129,7 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService { File file1 = new File("Material"); File file2 = new File(file1, "pictures"); if (file1.exists()) { - deleteFiles(file1); + delFile(file1); } boolean flag = file1.mkdir(); if (!flag) { @@ -211,4 +211,162 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService { } //System.out.println("外层文件:"+file.getName()+"--------"+file.delete()); } + //test +// public void downMaterialBy11(Long pileId) throws Exception { +// +// List> list = xhpcDownCodeMapper.selectExcelDataByPileId(pileId); +// System.out.println(list); +// File file1 = new File("Material"); +// File file2 = new File(file1, "pictures"); +// if(file1.exists()){ +// delFile(file1); +// } +// boolean flag = file1.mkdir(); +// if(! flag) { +// System.out.println(file1.getName()+"文件夹创建失败!"); +// return; +// } +// boolean flag1 = file2.mkdir(); +// if(! flag1){ +// System.out.println(file2.getName() + "文件夹创建失败"); +// } +// //1、创建excel文档对象 +// HSSFWorkbook workbook = new HSSFWorkbook(); +// //2、创建sheet页 +// HSSFSheet sheet = workbook.createSheet("第一页"); +// //3、创建头部第一行 +// HSSFRow headRow = sheet.createRow(0); +// HSSFCell cell; +// String[] header = new String[] {"终端名称","归属电站","终端编码","电桩编码","终端类型(1直流(慢) 2交流(快)","电桩功率(KM)"}; +// for (int i = 0; i < header.length; i++) { +// cell = headRow.createCell(i); +// cell.setCellValue(header[i]); +// } +// //4、填写数据 +// HSSFRow dataRow; +// HSSFCell dataCell; +// String[] values = {"tName", "sName", "tSN", "psn", "type", "power"}; +// int valuesLength = values.length; +// for(int i = 1; i <= list.size(); i ++){ +// Map map = list.get(i - 1); +// if(null != map) { +// dataRow = sheet.createRow(i); +// for(int j = 0; j < valuesLength; j ++){ +// if(null != map.get(values[j])){ +// dataCell = dataRow.createCell(j); +// String s = String.valueOf(map.get(values[j])); +// dataCell.setCellValue(s); +// } +// } +// } +// } +// System.out.println(list); +// //5、写入 +// OutputStream fos = new FileOutputStream(file1.getAbsoluteFile() + "\\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.saveImageToDisk1(list1,file2,imgUrls); +// File file = new File(file1.getAbsolutePath()); +// DownMaterialUtil.fileToZip(file); +// System.out.println("success"); +// } +// +// @Override +// public void downMaterialByStationId11(Long stationId) throws Exception { +// +// List> list = xhpcDownCodeMapper.selectExcelDataByStationId(stationId); +// System.out.println(list); +// File file1 = new File("Material"); +// File file2 = new File(file1, "pictures"); +// if (file1.exists()) { +// delFile(file1); +// } +// boolean flag = file1.mkdir(); +// if (!flag) { +// System.out.println(file1.getName() + "文件夹创建失败!"); +// } +// boolean flag1 = file2.mkdir(); +// if (!flag1) { +// System.out.println(file2.getName() + "文件夹创建失败"); +// } +// //1、创建excel文档对象 +// HSSFWorkbook workbook = new HSSFWorkbook(); +// //2、创建sheet页 +// HSSFSheet sheet = workbook.createSheet("第一页"); +// //3、创建头部第一行 +// HSSFRow headRow = sheet.createRow(0); +// HSSFCell cell; +// String[] header = new String[]{"终端名称", "归属电站", "终端编码", "电桩编码", "终端类型(1直流(快) 2交流(慢)", "电桩功率(KM)"}; +// for (int i = 0; i < header.length; i++) { +// cell = headRow.createCell(i); +// cell.setCellValue(header[i]); +// } +// //4、填写数据 +// HSSFRow dataRow; +// HSSFCell dataCell; +// String[] values = {"tName", "sName", "tSN", "psn", "type", "power"}; +// int valuesLength = values.length; +// for (int i = 1; i <= list.size(); i++) { +// Map map = list.get(i - 1); +// if (null != map) { +// dataRow = sheet.createRow(i); +// for (int j = 0; j < valuesLength; j++) { +// if (null != map.get(values[j])) { +// dataCell = dataRow.createCell(j); +// String s = String.valueOf(map.get(values[j])); +// if (j==4) { +// if("1".equals(s)){ +// dataCell.setCellValue("直流"); +// }else{ +// dataCell.setCellValue("交流"); +// } +// }else{ +// dataCell.setCellValue(s); +// } +// } +// } +// } +// } +// //5、写入 +// OutputStream fos = new FileOutputStream(file1.getAbsoluteFile() + "\\data.xls"); +// workbook.write(fos); +// workbook.close(); +// fos.close(); +// String left = "https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/"; +// List imgUrls = xhpcDownCodeMapper.selectUrlsByStation(stationId); +// List list1 = DownMaterialUtil.getDownLoadUrls(left, imgUrls); +// DownMaterialUtil.saveImageToDisk1(list1, file2, imgUrls); +// File file = new File(file1.getAbsolutePath()); +// DownMaterialUtil.fileToZip(file); +// System.out.println("success"); +// File file = new File(file1); +// //储存路径 +// long l = System.currentTimeMillis(); +// String url=environment.getProperty("img.url")+l; +// +// DownMaterialUtil.fileToZip(file,url); +// +// return "Material"+l+".zip"; +// } + + static boolean delFile(File file) { + + if (!file.exists()) { + return false; + } + if (file.isDirectory()) { + File[] files = file.listFiles(); + if (null != files) { + for (File f : files) { + delFile(f); + } + } + } + return 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 526b32da..38726c47 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,67 @@ public class DownMaterialUtil { } } - public static void fileToZip(File sourceFile,String url) throws Exception { + public static void saveImageToDisk1(List urls, File file, List imgNames) { + + for (int i = 0; i < urls.size(); i++) { + InputStream inputStream = getInputStream(urls.get(i)); + byte[] data = new byte[1024]; + int len = 0; + FileOutputStream fileOutputStream = null; + try { + String str = imgNames.get(i); + int poke = 0; + for (int j = str.length() - 1; j >= 0; j--) { + if (str.charAt(j) == 47) { + poke = j; + break; + } + } + String real = str.substring(poke + 1); + System.out.println(real); + System.out.println(file.getName() + "\\" + real); + fileOutputStream = new FileOutputStream(file.getAbsoluteFile() + "\\" + real); + } catch (IOException e) { + e.printStackTrace(); + } finally { + + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (fileOutputStream != null) { + try { + fileOutputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + System.out.println("success"); + } + } + } + + public static void fileToZip(File sourceFile) throws Exception { + + if (!sourceFile.exists()) { + throw new RuntimeException("不存在"); + } + if (!sourceFile.isDirectory()) { + throw new RuntimeException("不是文件夹"); + } + //zip文件生成位置 + File zipFile = new File(sourceFile.getAbsolutePath() + ".zip"); + FileOutputStream fos = new FileOutputStream(zipFile); + ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos)); + fileToZip(zos, sourceFile, ""); + zos.close(); + fos.close(); + } + + public static void fileToZip(File sourceFile, String url) throws Exception { if (!sourceFile.exists()) { throw new RuntimeException("不存在");