当前位置: 首页 > news >正文

网站开发语言字典徐州seo外包平台

网站开发语言字典,徐州seo外包平台,设计制作网站板面,公司做二手网站的用意目录 需求一、准备模板文件二、引入Poi-tl、Apache POI依赖三、创建实体类(用于保存向Word中写入的数据)四、实现Service接口五、Controller层实现 需求 在服务端提前准备好Word模板文件,并在用户请求接口时服务端动态获取图片。数据等信息插…

目录

    • 需求
    • 一、准备模板文件
    • 二、引入Poi-tl、Apache POI依赖
    • 三、创建实体类(用于保存向Word中写入的数据)
    • 四、实现Service接口
    • 五、Controller层实现

需求

在服务端提前准备好Word模板文件,并在用户请求接口时服务端动态获取图片。数据等信息插入到模板当中,然后返回包含数据信息的Word文件流。

一、准备模板文件

在需要插入图片的地方使用:{{@参数名}},文本信息使用:{{参数名}},进行占位,占位格式将会被保留,经过处理后格式不变

在这里插入图片描述

将准备好的模板文件放在resources目录下

blog.csdnimg.cn/direct/6d1474091083427483e11ea71e25ef51.png)

二、引入Poi-tl、Apache POI依赖

poi-tl(poi template language)是Word模板引擎,基于Apache POI,提供更友好的API,使用起来更加简单
版本对应关系参考Poi-tl官网

<!--    替换自己使用的版本    -->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.*</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.*</version>
</dependency>
<!--    Word模板引擎    -->
<dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.7.*</version>
</dependency>

三、创建实体类(用于保存向Word中写入的数据)

参数名必须同Word模板中的参数名称保持一致

import com.deepoove.poi.data.PictureRenderData;@Data
public class DownloadDate {//图片使用PictureRenderData类型private PictureRenderData image;private String name;private String a;private String b;private String c;private String d;private String e;private String f;private String g;private String h;private String i;
}

四、实现Service接口

public interface DownloadService {void download(HttpServletResponse response, DownloadDTO downloadDTO) throws IOException;
}

@Service
@Slf4j
public class DownloadServiceImpl implements DownloadService {@Resource//远程调用服务private FeignService feignService;@Overridepublic void download(HttpServletResponse response, DownloadDTO downloadDTO) throws IOException {BufferedImage、字节数组),Base64可以转字节数组后使用//通过调用其它接口获取待写入的数据信息WordData wordData = feignService.getData(downloadDTO);/** * 图片可以是多种格式------------------------* 图片路径:PictureRenderData(int width, int height, String path)* File:PictureRenderData(int width, int height, File picture)* InputStream:PictureRenderData(int width, int height, String format, InputStream input)* BufferedImage:PictureRenderData(int width, int height, String format, BufferedImage image)* 字节数组:PictureRenderData(int width, int height, String format, byte[] data)* Base64可以转字节数组后使用*///以Base64为例,先获取图片的Base64编码(wordData.getImg是原始图片Base64数据)String base64ImageData = wordData.getImg.substring(data.indexOf(",") + 1);//获取图片类型String format = getBase64Type(base64ImageData);// 将base64数据转为字节数组byte[] imageBytes = Base64.getDecoder().decode(base64ImageData);// 将字节数组包装成PictureRenderDataPictureRenderData pictureRenderData = new PictureRenderData(690,530,format,imageBytes);//待写入Word的数据DownloadDate downloadDate = new DownloadDate();//图片信息downloadDate.setImage(pictureRenderData);//其它信息downloadDate.setName(wordData.getName());//...XWPFTemplate template = null;BufferedOutputStream bufferedOutputStream = null;ServletOutputStream outputStream = null;try {/** * 该方法会导致在部分环境中资源找不到的情况,不推荐使用*///获得resource路径+模板路径//String path = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("")).getPath() + "word/template.docx";// 读取Word模板//FileInputStream templateInputStream = new FileInputStream(path);// 模板绑定数据//template = XWPFTemplate.compile(templateInputStream).render(imageDownloadDate);// 从资源中加载Word模板try (InputStream templateInputStream = getClass().getClassLoader().getResourceAsStream("word/template.docx")) {if (templateInputStream != null) {// 模板绑定数据template = XWPFTemplate.compile(templateInputStream).render(imageDownloadDate);} else {// 处理模板资源未找到的情况log.error("Word模板资源未找到");response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);return;}}//文件名String encodedFileName = URLEncoder.encode(System.currentTimeMillis()+"", "utf-8");//设置响应信息response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName + ".docx");response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");response.setCharacterEncoding("utf-8");outputStream = response.getOutputStream();bufferedOutputStream = new BufferedOutputStream(outputStream);template.write(bufferedOutputStream);//清空流bufferedOutputStream.flush();outputStream.flush();} catch (Exception e) {log.info(e.getMessage());response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);} finally {//关闭资源PoitlIOUtils.closeQuietlyMulti(template, bufferedOutputStream, outputStream);}}//根据base64编码获取图片格式信息private String getBase64Type(String base64) {byte[] b = Base64.getDecoder().decode(base64);String type = ".png";if (0x424D == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {type = ".bmp";} else if (0x8950 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {type = ".png";} else if (0xFFD8 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {type = ".jpg";} else if (0x49492A00 == ((b[0] & 0xff) << 24 | (b[1] & 0xff) << 16 | (b[2] & 0xff) << 8 | (b[3] & 0xff))) {type = ".tif";}return type;}}

五、Controller层实现

@RestController
@RequestMapping("/test")
@Api(tags = "获取商品图片")
public class GetImageController {@ResourceDownloadService downloadService;@PostMapping("/download")@ApiOperation(value = "下载Word")void download(HttpServletResponse response,@RequestBody DownloadDTO downloadDTO) throws IOException {//鉴权或其它处理//....downloadService.download(response,downloadDTO);}}
http://www.dinnco.com/news/62705.html

相关文章:

  • 公司网站 域名 cn com上海关键词seo
  • 医院网站设计模板页面优化算法
  • 网站建设的设计思路四川seo整站优化吧
  • asp影楼网站设计怎么找专业的营销团队
  • 网站开发的项目开发宁波seo公司
  • 网站flash banner中国职业培训在线
  • 山东做网站找谁手机网站seo免费软件
  • 网站建好了怎么做淘宝客基本seo
  • 大丰建站谷歌排名算法
  • 百色网站优化短视频搜索优化
  • 企业建设网站好吗搜索引擎优化seo应用
  • 怎么开一个做网站的工作室品牌服务推广
  • 中国建设银行网站-诚聘英才seo网站优化方法
  • 代理注册公司行情上海做seo的公司
  • 电商公司做网站网站优化的方法与技巧
  • 网上赚钱的门路石家庄seo优化公司
  • 域名网站空间友链交换不限内容
  • 最火的网页游戏排行榜西安网络推广seo0515
  • 大连云建站模板登封搜索引擎优化
  • html购物网站模板怎么制作网站
  • 做ppt音乐模板下载网站建网站需要多少钱
  • 做网站的公司多吗怎么做好seo内容优化
  • wordpress 切换语言seo推广思路
  • 保山企业网站建设国内网络营销公司排名
  • 代网站备案费用吗每日英语新闻
  • 承德很好的网络建站凡科网免费建站
  • 河北网站建设与管理网站搭建需要多少钱
  • 品质好的女装品牌seo技巧seo排名优化
  • 上海做网站那家公司好关键词如何优化排名
  • 蔬菜派送网站怎么做分销系统