From 13f8fda108441c9cff723052518312d1348ff142 Mon Sep 17 00:00:00 2001 From: chuang <994001556@qq.com> Date: Sat, 18 Feb 2023 17:07:58 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=87=E8=B4=AD=E4=BF=A1=E6=81=AF=E7=94=9F?= =?UTF-8?q?=E6=88=90PDF=EF=BC=882023=E5=B9=B42=E6=9C=8818=E6=97=A5?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mobilePort/utils/MultipartFileDto.java | 117 ++++++++++++++++++ .../mobilePort/utils/WBigDecimalUtils.java | 11 -- .../impl/PurchaseorderitemServiceImpl.java | 31 ++++- .../main/resources/templates/export/test.ftl | 45 ++++--- 4 files changed, 168 insertions(+), 36 deletions(-) create mode 100644 SC-boot/linkage-scm/src/main/java/jnpf/mobilePort/utils/MultipartFileDto.java delete mode 100644 SC-boot/linkage-scm/src/main/java/jnpf/mobilePort/utils/WBigDecimalUtils.java diff --git a/SC-boot/linkage-scm/src/main/java/jnpf/mobilePort/utils/MultipartFileDto.java b/SC-boot/linkage-scm/src/main/java/jnpf/mobilePort/utils/MultipartFileDto.java new file mode 100644 index 00000000..0b3b5914 --- /dev/null +++ b/SC-boot/linkage-scm/src/main/java/jnpf/mobilePort/utils/MultipartFileDto.java @@ -0,0 +1,117 @@ +package jnpf.mobilePort.utils; + +import org.springframework.util.FileCopyUtils; +import org.springframework.web.multipart.MultipartFile; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +/** + * @Author: WangChuang + * @Date: 18/2/2023 下午1:50 + * @Description //注释: + * file转MultipartFile的时候会用到MockMultipartFile + * * 当你导入spring-test依赖的时候 会跟某些依赖冲突(暂未找到具体是哪个冲突) + * * 解决方法 重写一个类去实现MultipartFile接口 + * * 直接用MockMultipartFile的源码 + * @Version 1.0 + */ +public class MultipartFileDto implements MultipartFile { + private final String name; + + private String originalFilename; + + private String contentType; + + private final byte[] content; + + /** + * Create a new MultipartFileDto with the given content. + * @param name the name of the file + * @param content the content of the file + */ + public MultipartFileDto(String name, byte[] content) { + this(name, "", null, content); + } + + /** + * Create a new MultipartFileDto with the given content. + * @param name the name of the file + * @param contentStream the content of the file as stream + * @throws IOException if reading from the stream failed + */ + public MultipartFileDto(String name, InputStream contentStream) throws IOException { + this(name, "", null, FileCopyUtils.copyToByteArray(contentStream)); + } + + /** + * Create a new MultipartFileDto with the given content. + * @param name the name of the file + * @param originalFilename the original filename (as on the client's machine) + * @param contentType the content type (if known) + * @param content the content of the file + */ + public MultipartFileDto(String name, String originalFilename, String contentType, byte[] content) { + this.name = name; + this.originalFilename = (originalFilename != null ? originalFilename : ""); + this.contentType = contentType; + this.content = (content != null ? content : new byte[0]); + } + + /** + * Create a new MultipartFileDto with the given content. + * @param name the name of the file + * @param originalFilename the original filename (as on the client's machine) + * @param contentType the content type (if known) + * @param contentStream the content of the file as stream + * @throws IOException if reading from the stream failed + */ + public MultipartFileDto(String name, String originalFilename, String contentType, InputStream contentStream) + throws IOException { + + this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream)); + } + + @Override + public String getName() { + return this.name; + } + + @Override + public String getOriginalFilename() { + return this.originalFilename; + } + + @Override + public String getContentType() { + return this.contentType; + } + + @Override + public boolean isEmpty() { + return (this.content.length == 0); + } + + @Override + public long getSize() { + return this.content.length; + } + + @Override + public byte[] getBytes() throws IOException { + return this.content; + } + + @Override + public InputStream getInputStream() throws IOException { + return new ByteArrayInputStream(this.content); + } + + @Override + public void transferTo(File dest) throws IOException, IllegalStateException { + FileCopyUtils.copy(this.content, dest); + } + +} diff --git a/SC-boot/linkage-scm/src/main/java/jnpf/mobilePort/utils/WBigDecimalUtils.java b/SC-boot/linkage-scm/src/main/java/jnpf/mobilePort/utils/WBigDecimalUtils.java deleted file mode 100644 index bf584cfb..00000000 --- a/SC-boot/linkage-scm/src/main/java/jnpf/mobilePort/utils/WBigDecimalUtils.java +++ /dev/null @@ -1,11 +0,0 @@ -package jnpf.mobilePort.utils; - -/** - * @Author: WangChuang - * @Date: 17/2/2023 下午5:48 - * @Description //注释: - * @Version 1.0 - */ -public class WBigDecimalUtils { - -} diff --git a/SC-boot/linkage-scm/src/main/java/jnpf/purchaseorder/service/impl/PurchaseorderitemServiceImpl.java b/SC-boot/linkage-scm/src/main/java/jnpf/purchaseorder/service/impl/PurchaseorderitemServiceImpl.java index 25d94bcc..8b1fc07d 100644 --- a/SC-boot/linkage-scm/src/main/java/jnpf/purchaseorder/service/impl/PurchaseorderitemServiceImpl.java +++ b/SC-boot/linkage-scm/src/main/java/jnpf/purchaseorder/service/impl/PurchaseorderitemServiceImpl.java @@ -7,8 +7,10 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import jnpf.config.ConfigValueUtil; import jnpf.mobilePort.entity.PurchaseOrderVO; import jnpf.mobilePort.utils.ExportPdf; +import jnpf.mobilePort.utils.MultipartFileDto; import jnpf.permission.model.authorize.AuthorizeConditionModel; import jnpf.permission.service.AuthorizeService; import jnpf.purchaseorder.entity.*; @@ -19,15 +21,16 @@ import jnpf.purchaseorder.service.*; import jnpf.util.ServletUtil; import jnpf.util.StringUtil; import jnpf.util.UserProvider; +import org.apache.http.entity.ContentType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; -import java.io.ByteArrayOutputStream; -import java.io.IOException; +import java.io.*; import java.lang.reflect.Field; import java.math.BigDecimal; import java.net.URLEncoder; @@ -73,6 +76,8 @@ public class PurchaseorderitemServiceImpl extends ServiceImpl getPdfInfo(){ PurchaseorderPagination pagination = new PurchaseorderPagination(); @@ -93,7 +98,7 @@ if (pMap!=null&&pMap.size()>0){ map.put("contractNo", pVO.getContractNo()); map.put("contractCode",pVO.getContractCode()); map.put("supplierName", pVO.getSupplierName()); - + map.put("documentNo",pVO.getDocumentNo()); List list = pVO.getPurchaseorder_item0List(); Integer settlement=0; BigDecimal settlementSum = new BigDecimal(settlement); @@ -116,13 +121,29 @@ if (pMap!=null&&pMap.size()>0){ System.out.println("AAAAAAAAAAAAAAAAAAAAAA "); //repotData自己加测试数据 - out= exportPdf.createPdf(map, "test.ftl", "/templates/export"); + out = exportPdf.createPdf(map, "test.ftl", "/templates/export"); bytes = out.toByteArray(); - String fileName="测试.pdf"; + String fileName="废钢采购订单("+map.get("supplierName")+")("+map.get("creatorTime")+").pdf"; fileName = URLEncoder.encode(fileName, "utf-8"); headers = new HttpHeaders(); headers.add("Content-Disposition", "attachment; filename=" + fileName); +// 存在本地文件中 + InputStream inputStream = new ByteArrayInputStream(bytes); + MultipartFile file = new MultipartFileDto(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream); + + String templateFilePath = configValueUtil.getTemplateFilePath(); +// FileUtil.upFile(file, templateFilePath+"/wc/", fileName); + try { + File f = new File(templateFilePath+"/wc/", fileName); + //将上传的文件存储到指定位置 + file.transferTo(f); + }catch (Exception e){ + e.printStackTrace(); + } + + + //"attachment; filename=" + new String(fileName.getBytes("gbk"), StandardCharsets.ISO_8859_1)); } catch (Exception e) { log.error("导出pdf失败:{}", e); diff --git a/SC-boot/linkage-scm/src/main/resources/templates/export/test.ftl b/SC-boot/linkage-scm/src/main/resources/templates/export/test.ftl index 52d80b36..66691c4d 100644 --- a/SC-boot/linkage-scm/src/main/resources/templates/export/test.ftl +++ b/SC-boot/linkage-scm/src/main/resources/templates/export/test.ftl @@ -30,9 +30,9 @@

业务部门:${name!""}

- - - + + + @@ -42,21 +42,21 @@ - - + + - + - + - + @@ -68,29 +68,34 @@ <#if reportData??> <#if reportData_index == 0> - - - - - - - - + + + + + + + + + + + + + - + - - + + - +
订单日期${creatorTime!""}对方合同编号订单日期${creatorTime!""}对方合同编号 ${contractNo!""} 合同编号 ${contractCode!""}
供货单位${supplierName!""}供货单位${supplierName!""}
订单号订单号 行号物料名称、规格型号物料名称、规格型号 数量 单位 单价(含税) 金额(含税)税率(含税)税率
${reportData.documentNo!""}${reportData_index+1!""}${reportData.materialName!""}${reportData.settlement!""}${reportData.unit!""}${reportData.price!""}${reportData.amount!""}${reportData.rate!""}${documentNo!""}${reportData_index+1!""}${reportData.materialName!""}${reportData.settlement!""}${reportData.unit!""}${reportData.price!""}${reportData.amount!""}${reportData.rate!""}
合计合计 ${settlementSum!""}${settlementSum!""} ${amountSum!""}${amountSum!""}