From 23e16cb8f135d17e98b34786eeedb1fd226dbc4a Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期六, 27 四月 2024 18:41:46 +0800 Subject: [PATCH] 1、修改DownloadFileCtrl中的文件名称转码方式; 2、用EasyExcel实现标识类二维码导出excel文件并下载功能。 --- pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/ResultVo.java | 2 /dev/null | 62 ---------- pms-parent/pms-web-file/src/main/java/com/dy/dyFile/download/DownloadFileCtrl.java | 97 ++++++++------- pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/MarkQrCodeCtrl.java | 102 +++++++++++++++++ pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/ExcelVo.java | 66 +++++++++++ 5 files changed, 219 insertions(+), 110 deletions(-) diff --git a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/ExcelVo.java b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/ExcelVo.java new file mode 100644 index 0000000..8baac6b --- /dev/null +++ b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/ExcelVo.java @@ -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()); + } + +} diff --git a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/MarkQrCodeCtrl.java b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/MarkQrCodeCtrl.java new file mode 100644 index 0000000..ddbd240 --- /dev/null +++ b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/MarkQrCodeCtrl.java @@ -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鐗堟湰瓒婇珮锛屽娉涘瀷绾︽潫瓒婁弗锛屾墍浠ラ厤缃甋uppressWarnings("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{ + // 杩欓噷娉ㄦ剰 鏈夊悓瀛﹀弽搴斾娇鐢╯wagger 浼氬鑷村悇绉嶉棶棰橈紝璇风洿鎺ョ敤娴忚鍣ㄦ垨鑰呯敤postman + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setCharacterEncoding("utf-8"); + // 杩欓噷URLEncoder.encode鍙互闃叉涓枃涔辩爜 褰撶劧鍜宔asyexcel娌℃湁鍏崇郴 + 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); + } + } +} diff --git a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/qrCode/ResultVo.java b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/ResultVo.java similarity index 83% rename from pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/qrCode/ResultVo.java rename to pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/ResultVo.java index 5344b21..500c9e3 100644 --- a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/qrCode/ResultVo.java +++ b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/MarkQrCode/ResultVo.java @@ -1,4 +1,4 @@ -package com.dy.pmsBase.qrCode; +package com.dy.pmsBase.MarkQrCode; import lombok.Data; diff --git a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/qrCode/MarkQrCodeCtrl.java b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/qrCode/MarkQrCodeCtrl.java deleted file mode 100644 index 7022f51..0000000 --- a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/qrCode/MarkQrCodeCtrl.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.dy.pmsBase.qrCode; - - -import cn.hutool.core.codec.Base64; -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 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.URL; -import java.util.ArrayList; -import java.util.List; - -/** - * 鐢熸垚鏍囪瘑绫讳簩缁寸爜 - */ -@Slf4j -@RestController -@RequestMapping(path = "markQrCode") -@SuppressWarnings("unchecked")//java鐗堟湰瓒婇珮锛屽娉涘瀷绾︽潫瓒婁弗锛屾墍浠ラ厤缃甋uppressWarnings("unchecked") -public class MarkQrCodeCtrl { - - /** - * 瀹㈡埛绔姹傚緱鍒伴粯璁ゅ瘑鐮� - * @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()); - } - } -} diff --git a/pms-parent/pms-web-file/src/main/java/com/dy/dyFile/download/DownloadFileCtrl.java b/pms-parent/pms-web-file/src/main/java/com/dy/dyFile/download/DownloadFileCtrl.java index 57f5648..f858373 100644 --- a/pms-parent/pms-web-file/src/main/java/com/dy/dyFile/download/DownloadFileCtrl.java +++ b/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); } + } } -- Gitblit v1.8.0