二维码下载功能完成

This commit is contained in:
yuyang 2021-11-05 19:55:12 +08:00
parent fafd4479a3
commit 83575b1b4a
4 changed files with 62 additions and 33 deletions

View File

@ -2,8 +2,10 @@ package com.xhpc.charging.station.controller;
import com.xhpc.charging.station.service.IXhpcDownCodeService; import com.xhpc.charging.station.service.IXhpcDownCodeService;
import com.xhpc.common.core.web.controller.BaseController; 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 com.xhpc.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired; 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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@ -20,6 +22,8 @@ public class XhpcDownCodeController extends BaseController {
@Autowired @Autowired
private IXhpcDownCodeService iXhpcDownCodeService; private IXhpcDownCodeService iXhpcDownCodeService;
@Autowired
private Environment environment;
@GetMapping(value = "/listStations") @GetMapping(value = "/listStations")
public TableDataInfo listStations(String stationName){ public TableDataInfo listStations(String stationName){
@ -36,14 +40,18 @@ public class XhpcDownCodeController extends BaseController {
} }
@GetMapping(value = "/downMaterialByPileId") @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") @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);
} }
} }

View File

@ -14,7 +14,7 @@ public interface IXhpcDownCodeService {
List<Map<String, Object>> listPiles(Long stationId); List<Map<String, Object>> 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;
} }

View File

@ -9,6 +9,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.File; import java.io.File;
@ -29,6 +30,9 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService {
@Autowired @Autowired
private XhpcDownCodeMapper xhpcDownCodeMapper; private XhpcDownCodeMapper xhpcDownCodeMapper;
@Autowired
private Environment environment;
private static final Logger logger = LoggerFactory.getLogger(XhpcDownCodeServiceImpl.class); private static final Logger logger = LoggerFactory.getLogger(XhpcDownCodeServiceImpl.class);
@Override @Override
@ -44,11 +48,10 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService {
} }
@Override @Override
public void downMaterialBy(Long pileId) throws Exception { public String downMaterialBy(Long pileId) throws Exception {
List<Map<String,Object>> list = xhpcDownCodeMapper.selectExcelDataByPileId(pileId); List<Map<String,Object>> list = xhpcDownCodeMapper.selectExcelDataByPileId(pileId);
System.out.println(list); File file1 = new File(environment.getProperty("img.url"));
File file1 = new File("/www/wwwroot/xhpc.scxhua.com/dist/Material");
File file2 = new File(file1, "pictures"); File file2 = new File(file1, "pictures");
if(file1.exists()){ if(file1.exists()){
deleteFiles(file1); deleteFiles(file1);
@ -56,7 +59,6 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService {
boolean flag = file1.mkdir(); boolean flag = file1.mkdir();
if(! flag) { if(! flag) {
System.out.println(file1.getName()+"文件夹创建失败!"); System.out.println(file1.getName()+"文件夹创建失败!");
return;
} }
boolean flag1 = file2.mkdir(); boolean flag1 = file2.mkdir();
if(! flag1){ if(! flag1){
@ -69,7 +71,7 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService {
//3创建头部第一行 //3创建头部第一行
HSSFRow headRow = sheet.createRow(0); HSSFRow headRow = sheet.createRow(0);
HSSFCell cell; HSSFCell cell;
String[] header = new String[] {"终端名称","归属电站","终端编码","电桩编码","终端类型(1直流慢) 2交流)","电桩功率(KM)"}; String[] header = new String[] {"终端名称","归属电站","终端编码","电桩编码","终端类型(1直流快) 2交流)","电桩功率(KM)"};
for (int i = 0; i < header.length; i++) { for (int i = 0; i < header.length; i++) {
cell = headRow.createCell(i); cell = headRow.createCell(i);
cell.setCellValue(header[i]); cell.setCellValue(header[i]);
@ -87,31 +89,40 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService {
if(null != map.get(values[j])){ if(null != map.get(values[j])){
dataCell = dataRow.createCell(j); dataCell = dataRow.createCell(j);
String s = String.valueOf(map.get(values[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写入 //5写入
OutputStream fos = new FileOutputStream(file1.getAbsoluteFile() + "/data.xls"); OutputStream fos = new FileOutputStream(environment.getProperty("img.url") + "/data.xls");
workbook.write(fos); workbook.write(fos);
workbook.close(); workbook.close();
fos.close(); fos.close();
String left = "https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/"; String left = "https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/";
List<String> imgUrls = xhpcDownCodeMapper.selectUrlsByPile(pileId); List<String> imgUrls = xhpcDownCodeMapper.selectUrlsByPile(pileId);
System.out.println(imgUrls);
List<String> list1 = DownMaterialUtil.getDownLoadUrls(left,imgUrls); List<String> list1 = DownMaterialUtil.getDownLoadUrls(left,imgUrls);
DownMaterialUtil.saveImageToDisk(list1,file2,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,url);
DownMaterialUtil.fileToZip(file);
System.out.println("success"); return "Material"+l+".zip";
} }
@Override @Override
public void downMaterialByStationId(Long stationId) throws Exception { public String downMaterialByStationId(Long stationId) throws Exception {
List<Map<String, Object>> list = xhpcDownCodeMapper.selectExcelDataByStationId(stationId); List<Map<String, Object>> list = xhpcDownCodeMapper.selectExcelDataByStationId(stationId);
System.out.println(list); System.out.println(list);
@ -123,7 +134,6 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService {
boolean flag = file1.mkdir(); boolean flag = file1.mkdir();
if (!flag) { if (!flag) {
System.out.println(file1.getName() + "文件夹创建失败!"); System.out.println(file1.getName() + "文件夹创建失败!");
return;
} }
boolean flag1 = file2.mkdir(); boolean flag1 = file2.mkdir();
if (!flag1) { if (!flag1) {
@ -136,7 +146,7 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService {
//3创建头部第一行 //3创建头部第一行
HSSFRow headRow = sheet.createRow(0); HSSFRow headRow = sheet.createRow(0);
HSSFCell cell; HSSFCell cell;
String[] header = new String[]{"终端名称", "归属电站", "终端编码", "电桩编码", "终端类型(1直流慢) 2交流)", "电桩功率(KM)"}; String[] header = new String[]{"终端名称", "归属电站", "终端编码", "电桩编码", "终端类型(1直流快) 2交流)", "电桩功率(KM)"};
for (int i = 0; i < header.length; i++) { for (int i = 0; i < header.length; i++) {
cell = headRow.createCell(i); cell = headRow.createCell(i);
cell.setCellValue(header[i]); cell.setCellValue(header[i]);
@ -154,25 +164,36 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService {
if (null != map.get(values[j])) { if (null != map.get(values[j])) {
dataCell = dataRow.createCell(j); dataCell = dataRow.createCell(j);
String s = String.valueOf(map.get(values[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写入 //5写入
OutputStream fos = new FileOutputStream(file1.getAbsoluteFile() + "/data.xls"); OutputStream fos = new FileOutputStream(environment.getProperty("img.url") + "/data.xls");
workbook.write(fos); workbook.write(fos);
workbook.close(); workbook.close();
fos.close(); fos.close();
String left = "https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/"; String left = "https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/";
List<String> imgUrls = xhpcDownCodeMapper.selectUrlsByStation(stationId); List<String> imgUrls = xhpcDownCodeMapper.selectUrlsByStation(stationId);
System.out.println(imgUrls);
List<String> list1 = DownMaterialUtil.getDownLoadUrls(left, imgUrls); List<String> list1 = DownMaterialUtil.getDownLoadUrls(left, imgUrls);
DownMaterialUtil.saveImageToDisk(list1, file2, imgUrls); DownMaterialUtil.saveImageToDisk(list1, file2, imgUrls);
File file = new File(file1.getAbsolutePath()); File file = new File(environment.getProperty("img.url"));
DownMaterialUtil.fileToZip(file); //储存路径
System.out.println("success"); long l = System.currentTimeMillis();
String url=environment.getProperty("img.url")+l;
DownMaterialUtil.fileToZip(file,url);
return "Material"+l+".zip";
} }
public void deleteFiles(File file){ public void deleteFiles(File file){
@ -183,11 +204,11 @@ public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService {
if (value.isDirectory()) { if (value.isDirectory()) {
deleteFiles(value); deleteFiles(value);
} else { } 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());
} }
} }

View File

@ -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()) { if (!sourceFile.exists()) {
throw new RuntimeException("不存在"); throw new RuntimeException("不存在");
@ -100,7 +100,7 @@ public class DownMaterialUtil {
throw new RuntimeException("不是文件夹"); throw new RuntimeException("不是文件夹");
} }
//zip文件生成位置 //zip文件生成位置
File zipFile = new File(sourceFile.getAbsolutePath() + ".zip"); File zipFile = new File(url + ".zip");
FileOutputStream fos = new FileOutputStream(zipFile); FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos)); ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
fileToZip(zos, sourceFile, ""); fileToZip(zos, sourceFile, "");