From 3be03ebded811745c3df73a9efc83b8b711ce46f Mon Sep 17 00:00:00 2001 From: little-cat-sweet <2116400472@qq.com> Date: Mon, 11 Oct 2021 11:28:39 +0800 Subject: [PATCH] Accomplishing the download of Terminal Two Dimension Code And Its Data By pileId or stationId. --- xhpc-modules/xhpc-charging-station/pom.xml | 7 + .../controller/XhpcDownCodeController.java | 49 ++++ .../station/mapper/XhpcDownCodeMapper.java | 26 +++ .../station/service/IXhpcDownCodeService.java | 20 ++ .../service/XhpcDownCodeServiceImpl.java | 188 +++++++++++++++ .../station/utils/img/DownMaterialUtil.java | 218 ++++++++++++++++++ .../resources/mapper/XhpcDownCodeMapper.xml | 77 +++++++ 7 files changed, 585 insertions(+) create mode 100644 xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/controller/XhpcDownCodeController.java create mode 100644 xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/mapper/XhpcDownCodeMapper.java create mode 100644 xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/IXhpcDownCodeService.java create mode 100644 xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcDownCodeServiceImpl.java create mode 100644 xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/img/DownMaterialUtil.java create mode 100644 xhpc-modules/xhpc-charging-station/src/main/resources/mapper/XhpcDownCodeMapper.xml diff --git a/xhpc-modules/xhpc-charging-station/pom.xml b/xhpc-modules/xhpc-charging-station/pom.xml index 2d028a0c..9e845fa9 100644 --- a/xhpc-modules/xhpc-charging-station/pom.xml +++ b/xhpc-modules/xhpc-charging-station/pom.xml @@ -128,6 +128,13 @@ 3.10.2 compile + + + org.apache.poi + poi-ooxml + 4.1.2 + + 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 new file mode 100644 index 00000000..a56f2af1 --- /dev/null +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/controller/XhpcDownCodeController.java @@ -0,0 +1,49 @@ +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.page.TableDataInfo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * program: ruoyi + * User: HongYun + * Date:2021-09-30 15 + */ +@RestController +@RequestMapping(value = "/code") +public class XhpcDownCodeController extends BaseController { + + @Autowired + private IXhpcDownCodeService iXhpcDownCodeService; + + @GetMapping(value = "/listStations") + public TableDataInfo listStations(String stationName){ + + startPage(); + return getDataTable(iXhpcDownCodeService.listStations(stationName)); + } + + @GetMapping(value = "/listPiles") + public TableDataInfo listPiles(@RequestParam Long stationId){ + + startPage(); + return getDataTable(iXhpcDownCodeService.listPiles(stationId)); + } + + @GetMapping(value = "/downMaterialByPileId") + public void downMaterialBy(@RequestParam Long pileId) throws Exception { + + iXhpcDownCodeService.downMaterialBy(pileId); + } + + @GetMapping(value = "/downMaterialByStationId") + public void downMaterialByStationId(@RequestParam Long stationId) throws Exception{ + + iXhpcDownCodeService.downMaterialByStationId(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 new file mode 100644 index 00000000..c00809a7 --- /dev/null +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/mapper/XhpcDownCodeMapper.java @@ -0,0 +1,26 @@ +package com.xhpc.charging.station.mapper; + +import org.apache.ibatis.annotations.Param; + +import java.util.List; +import java.util.Map; + +/** + * program: ruoyi + * User: HongYun + * Date:2021-09-30 15 + */ +public interface XhpcDownCodeMapper { + + List> selectStationsBy(@Param(value = "stationName") String stationName); + + List> selectPilesBy(@Param(value = "stationId") Long stationId); + + List> selectExcelDataByPileId(@Param("pileId") Long pileId ); + + List selectUrlsByPile(@Param(value = "pileId") Long pileId); + + List selectUrlsByStation(@Param(value = "stationId") Long stationId); + + List> selectExcelDataByStationId(@Param(value = "stationId") Long stationId); +} 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 new file mode 100644 index 00000000..fcb37a2e --- /dev/null +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/IXhpcDownCodeService.java @@ -0,0 +1,20 @@ +package com.xhpc.charging.station.service; + +import java.util.List; +import java.util.Map; + +/** + * program: ruoyi + * User: HongYun + * Date:2021-09-30 15 + */ +public interface IXhpcDownCodeService { + + List> listStations(String stationName); + + List> listPiles(Long stationId); + + void downMaterialBy(Long pileId) throws Exception; + + void 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 new file mode 100644 index 00000000..9b79cb9d --- /dev/null +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcDownCodeServiceImpl.java @@ -0,0 +1,188 @@ +package com.xhpc.charging.station.service; + +import com.xhpc.charging.station.mapper.XhpcDownCodeMapper; +import com.xhpc.charging.station.utils.img.DownMaterialUtil; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.util.List; +import java.util.Map; + +/** + * program: ruoyi + * User: HongYun + * Date:2021-09-30 15 + */ +@Service +public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService { + + + @Autowired + private XhpcDownCodeMapper xhpcDownCodeMapper; + + + @Override + public List> listStations(String stationName) { + + return xhpcDownCodeMapper.selectStationsBy(stationName); + } + + @Override + public List> listPiles(Long stationId) { + + return xhpcDownCodeMapper.selectPilesBy(stationId); + } + + @Override + public void downMaterialBy(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()){ + deleteFiles(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.saveImageToDisk(list1,file2,imgUrls); + File file = new File(file1.getAbsolutePath()); + DownMaterialUtil.fileToZip(file); + System.out.println("success"); + } + + @Override + public void downMaterialByStationId(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()) { + deleteFiles(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.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"); + } + + public void deleteFiles(File file){ + if (file.isDirectory()) { + File[] files=file.listFiles(); + if (null != files) { + for (File value : files) { + if (value.isDirectory()) { + deleteFiles(value); + } else { + System.out.println("里层文件:" + value.getName() + "--------" + value.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 new file mode 100644 index 00000000..d64b8803 --- /dev/null +++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/img/DownMaterialUtil.java @@ -0,0 +1,218 @@ +package com.xhpc.charging.station.utils.img; + +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +/** + * program: ruoyi + * User: HongYun + * Date:2021-10-09 10 + */ +public class DownMaterialUtil { + public static InputStream getInputStream(String urlImg) { + + InputStream inputStream = null; + HttpURLConnection httpURLConnection = null; + + try { + + URL url = new URL(urlImg); + httpURLConnection = (HttpURLConnection) url.openConnection(); + // 设置网络连接超时时间 + httpURLConnection.setConnectTimeout(3000); + // 设置应用程序要从网络连接读取数据 + httpURLConnection.setDoInput(true); + + httpURLConnection.setRequestMethod("GET"); + int responseCode = httpURLConnection.getResponseCode(); + if (responseCode == 200) { + // 从服务器返回一个输入流 + inputStream = httpURLConnection.getInputStream(); + + } + } catch (IOException e) { + e.printStackTrace(); + } + return inputStream; + } + + public static void saveImageToDisk(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); + + while ((len = inputStream.read(data)) != -1) { + fileOutputStream.write(data, 0, len); + } + } 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(); + } + private static void fileToZip(ZipOutputStream zos, File sourceFile, String path) throws Exception { + + //如果是文件夹只创建zip实体即可,如果是文件,创建zip实体后还要读取文件内容并写入 + if (sourceFile.isDirectory()) { + path = path + sourceFile.getName() + "/"; + ZipEntry zipEntry = new ZipEntry(path); + zos.putNextEntry(zipEntry); + for (File file : Objects.requireNonNull(sourceFile.listFiles())) { + fileToZip(zos, file, path); + } + } else { + //创建ZIP实体,并添加进压缩包 + ZipEntry zipEntry = new ZipEntry(path + sourceFile.getName()); + zos.putNextEntry(zipEntry); + byte[] bufs = new byte[1024 * 10]; + //读取待压缩的文件并写进压缩包里 + FileInputStream fis = new FileInputStream(sourceFile); + BufferedInputStream bis = new BufferedInputStream(fis, 1024 * 10); + int read = 0; + while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) { + zos.write(bufs, 0, read); + } + bis.close(); + fis.close(); + } + } + + public static void saveToFile(String targetUrl,String destUrl) { + + FileOutputStream fos = null; + BufferedInputStream bis = null; + HttpURLConnection httpUrl = null; + URL url = null; + int BUFFER_SIZE = 1024; + byte[] buf = new byte[BUFFER_SIZE]; + int size = 0; + try { + url = new URL(targetUrl); + httpUrl = (HttpURLConnection) url.openConnection(); + httpUrl.connect(); + bis = new BufferedInputStream(httpUrl.getInputStream()); + fos = new FileOutputStream(destUrl); + while ((size = bis.read(buf)) != -1) { + fos.write(buf, 0, size); + } + fos.flush(); + } catch (IOException | ClassCastException ignored) { + } finally { + try { + if(null != fos) { + fos.close(); + } + if(null != bis) { + bis.close(); + } + if(null != httpUrl) { + httpUrl.disconnect(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + + public static List getDownLoadUrls(String left, List imgUrl){ + List res = new ArrayList<>(); + for (String s : imgUrl) { + res.add(left + s); + } + return res; + } + + /** + * 以流的形式下载文件 + * + * @param file + * @param response + * @return + */ + public static HttpServletResponse downloadZip(File file, HttpServletResponse response) { + try { + // 以流的形式下载文件。 + InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath())); + byte[] buffer = new byte[fis.available()]; + fis.read(buffer); + fis.close(); + // 清空response + response.reset(); + OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); + response.setContentType("application/octet-stream"); + //如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理 + response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8")); + toClient.write(buffer); + toClient.flush(); + toClient.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } finally { + try { + File f = new File(file.getPath()); +// f.delete(); + } catch (Exception e) { + e.printStackTrace(); + } + } + return response; + } +} diff --git a/xhpc-modules/xhpc-charging-station/src/main/resources/mapper/XhpcDownCodeMapper.xml b/xhpc-modules/xhpc-charging-station/src/main/resources/mapper/XhpcDownCodeMapper.xml new file mode 100644 index 00000000..83e625d3 --- /dev/null +++ b/xhpc-modules/xhpc-charging-station/src/main/resources/mapper/XhpcDownCodeMapper.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file