pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/ExcelVo.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/MarkQrCodeCtrl.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/ResultVo.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/qrCode/MarkQrCodeCtrl.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
pms-parent/pms-web-file/src/main/java/com/dy/dyFile/download/DownloadFileCtrl.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/ExcelVo.java
New file @@ -0,0 +1,66 @@ package com.dy.pmsBase.MarkQrCode; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.WriteConverterContext; import com.alibaba.excel.enums.BooleanEnum; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.poi.BorderStyleEnum; import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum; import com.alibaba.excel.enums.poi.VerticalAlignmentEnum; import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.data.ImageData; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import lombok.Data; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.*; import java.util.ArrayList; import java.util.List; //参考: https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write @Data @HeadRowHeight(40) //设置标题高度 @HeadFontStyle(fontName="宋体", fontHeightInPoints = 16) @HeadStyle(wrapped = BooleanEnum.TRUE, shrinkToFit = BooleanEnum.TRUE, horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER, borderLeft = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderTop = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN, leftBorderColor = 8, rightBorderColor = 8, topBorderColor = 8, bottomBorderColor = 8) //IndexedColors.BLACK @ContentRowHeight(220) //设置内容高度 @ContentFontStyle(fontName="宋体", fontHeightInPoints = 14) @ContentStyle(wrapped = BooleanEnum.TRUE, shrinkToFit = BooleanEnum.TRUE, horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER, borderLeft = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderTop = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN, leftBorderColor = 8, rightBorderColor = 8, topBorderColor = 8, bottomBorderColor = 8) //IndexedColors.BLACK @ColumnWidth(16) //设置列宽 public class ExcelVo implements Converter<byte[]> { @ExcelProperty("编码") public String code ; //编码 @ExcelProperty("名称") public String name ; //名称 @ColumnWidth(40) //设置列宽(可以修饰类,也可以修饰具体属性) @ExcelProperty(value = "二维码", converter = ExcelVo.class) public byte[] image ; public WriteCellData<?> convertToExcelData(byte[] value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { WriteCellData cellData = new WriteCellData() ; List<ImageData> list = new ArrayList<>(); ImageData imd = new ImageData(); imd.setImage(value); imd.setImageType(ImageData.ImageType.PICTURE_TYPE_PICT); imd.setLeft(10); imd.setTop(10); imd.setRight(10); imd.setBottom(10); list.add(imd) ; cellData.setImageDataList(list); cellData.setType(CellDataTypeEnum.EMPTY); return cellData; } public WriteCellData<?> convertToExcelData(WriteConverterContext<byte[]> context) throws Exception { return this.convertToExcelData(context.getValue(), context.getContentProperty(), context.getWriteContext().currentWriteHolder().globalConfiguration()); } } pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/MarkQrCodeCtrl.java
New file @@ -0,0 +1,102 @@ package com.dy.pmsBase.MarkQrCode; import cn.hutool.core.codec.Base64; import com.alibaba.excel.EasyExcel; import com.dy.common.util.QrCodeGenerator; import com.dy.common.webUtil.BaseResponse; import com.dy.common.webUtil.BaseResponseUtils; import com.dy.pmsGlobal.util.QrCodeConstant; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.awt.image.BufferedImage; import java.io.File; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; /** * 生成标识类二维码 */ @Slf4j @RestController @RequestMapping(path = "markQrCode") @SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked") public class MarkQrCodeCtrl { private static final String fileName = "标识类二维码" ; private static final String sheetName = "标识类二维码" ; /** * 客户端请求得到标识类二维码 * @return */ @GetMapping(path = "show") public BaseResponse<List<ResultVo>> show() { try{ List<ResultVo> list = new ArrayList<>() ; Resource resource = new ClassPathResource("/images/logo.png"); File logoPngFile = resource.getFile() ; String[][] marks = QrCodeConstant.Marks() ; for(String[] mark : marks){ ResultVo vo = new ResultVo() ; vo.code = mark[0] ; vo.name = mark[1] ; //vo.code = "1234567890123456789012" ; //BufferedImage bufferedImage = QrCodeGenerator.toBufferedImage(QrCodeGenerator.createBitMatrix(vo.code, 30, 30)); BufferedImage bufferedImage = QrCodeGenerator.toBufferedImage(QrCodeGenerator.createBitMatrix(vo.code, QrCodeConstant.MarkQrCodeWidth, QrCodeConstant.MarkQrCodeHeight)); if(logoPngFile != null && logoPngFile.exists()){ bufferedImage = QrCodeGenerator.addQrCodeLogo(bufferedImage, logoPngFile); } byte[] codes = QrCodeGenerator.bufferedImageToByteArray(bufferedImage, "JPG"); vo.imgBase64 = "data:image/jpeg;base64," + Base64.encode(codes); list.add(vo) ; } return BaseResponseUtils.buildSuccess(list); }catch (Exception e){ log.error("生成标识类二维码时异常", e); return BaseResponseUtils.buildException(e.getMessage()); } } /** * 客户端请求下载标识类二维码的excel文件 * @return */ @GetMapping(path = "download") public void download(HttpServletResponse response){ try{ // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileName = URLEncoder.encode(MarkQrCodeCtrl.fileName, "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); List<ExcelVo> list = new ArrayList<>() ; Resource resource = new ClassPathResource("/images/logo.png"); File logoPngFile = resource.getFile() ; String[][] marks = QrCodeConstant.Marks() ; for(String[] mark : marks){ ExcelVo vo = new ExcelVo() ; vo.code = mark[0] ; vo.name = mark[1] ; BufferedImage bufferedImage = QrCodeGenerator.toBufferedImage(QrCodeGenerator.createBitMatrix(vo.code, QrCodeConstant.MarkQrCodeWidth, QrCodeConstant.MarkQrCodeHeight)); if(logoPngFile != null && logoPngFile.exists()){ bufferedImage = QrCodeGenerator.addQrCodeLogo(bufferedImage, logoPngFile); } vo.image = QrCodeGenerator.bufferedImageToByteArray(bufferedImage, "JPG"); list.add(vo) ; } EasyExcel.write(response.getOutputStream(), ExcelVo.class).sheet(MarkQrCodeCtrl.sheetName).doWrite(list); }catch (Exception e){ log.error("下载标识类二维码时异常", e); } } } pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/ResultVo.java
File was renamed from pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/qrCode/ResultVo.java @@ -1,4 +1,4 @@ package com.dy.pmsBase.qrCode; package com.dy.pmsBase.MarkQrCode; import lombok.Data; pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/qrCode/MarkQrCodeCtrl.java
File was deleted pms-parent/pms-web-file/src/main/java/com/dy/dyFile/download/DownloadFileCtrl.java
@@ -17,6 +17,7 @@ import java.io.File; import java.io.FileInputStream; import java.net.URLEncoder; /** * web文件下载 @@ -44,65 +45,67 @@ */ @GetMapping("/down") public void down(String id, HttpServletRequest req, HttpServletResponse rep){ OthFile fPo = sv.selectById(id) ; if(fPo != null){ FileRestVo frVo = fileOp.parseHashcode(fmUrl, fPo.hash) ; if(frVo.fileSysAbsolutePath != null){ if(!frVo.fileSysAbsolutePath.endsWith("\\\\") && !frVo.fileSysAbsolutePath.endsWith("/") ){ frVo.fileSysAbsolutePath = frVo.fileSysAbsolutePath + "/" ; try{ OthFile fPo = sv.selectById(id) ; if(fPo != null){ FileRestVo frVo = fileOp.parseHashcode(fmUrl, fPo.hash) ; if(frVo.fileSysAbsolutePath != null){ if(!frVo.fileSysAbsolutePath.endsWith("\\\\") && !frVo.fileSysAbsolutePath.endsWith("/") ){ frVo.fileSysAbsolutePath = frVo.fileSysAbsolutePath + "/" ; } } } String filePath = frVo.fileSysAbsolutePath + fPo.filePath ; File f = new File(filePath) ; if(f.exists()){ String fileReName = fPo.orgName + "." + fPo.extName ; try { fileReName = new String(fileReName.getBytes("UTF-8"), "ISO-8859-1"); } catch (Exception e) { fileReName = "file" ; } rep.addHeader("content-type", "application/octet-stream"); rep.addHeader("Content-Disposition", "attachment;fileName=" + fileReName); String filePath = frVo.fileSysAbsolutePath + fPo.filePath ; File f = new File(filePath) ; if(f.exists()){ String fileReName = fPo.orgName + "." + fPo.extName ; //URLEncoder.encode可以防止中文乱码 fileReName = URLEncoder.encode(fileReName, "UTF-8").replaceAll("\\+", "%20"); rep.addHeader("content-type", "application/octet-stream"); rep.addHeader("Content-Disposition", "attachment;fileName=" + fileReName); ServletOutputStream out = null; FileInputStream in = null ; try { out = rep.getOutputStream() ; } catch (Exception ee) { out = null ; }finally{ if(out != null){ byte[] bs = new byte[1024] ; int len = -1 ; try { in = new FileInputStream(f); len = in.read(bs) ; while(len != -1){ out.write(bs, 0, len); ServletOutputStream out = null; FileInputStream in = null ; try { out = rep.getOutputStream() ; } catch (Exception ee) { out = null ; }finally{ if(out != null){ byte[] bs = new byte[1024] ; int len = -1 ; try { in = new FileInputStream(f); len = in.read(bs) ; } } catch (Exception eee) { } finally { if(out != null){ try{ out.flush(); out.close(); }catch(Exception e){ }finally{ if(in != null){ try{ in.close(); }catch(Exception e){ while(len != -1){ out.write(bs, 0, len); len = in.read(bs) ; } } catch (Exception eee) { } finally { if(out != null){ try{ out.flush(); out.close(); }catch(Exception e){ }finally{ if(in != null){ try{ in.close(); }catch(Exception e){ } } } } } } } }else{ } }else{ } }else{ }catch (Exception e){ log.error("下载文件时异常", e); } } }