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

用在线网站做的简历可以吗线上营销推广方式

用在线网站做的简历可以吗,线上营销推广方式,济阳做网站,WordPress导入hexoJAVA实现将富文本内容插入已有word文档并下载(dock4jjsoup) 需求描述: 最近公司项目需要开发一个功能,需要将前端保存的富文本内容和目录插入到已有的word文档模版里,并提供下载功能。参考了很多方法,也踩…

JAVA实现将富文本内容插入已有word文档并下载(dock4j+jsoup)

需求描述:

最近公司项目需要开发一个功能,需要将前端保存的富文本内容和目录插入到已有的word文档模版里,并提供下载功能。参考了很多方法,也踩了一些坑,最后使用dock4j+jsoup实现了;因为图片在富文本里保存的是相当路径,需要使用jsoup将富文本的标签解析出来并处理,dock4j无法直接将HTML的路径图片转换成word,所以需要将图片下载,并转换成base64编码格式。

引用依赖:

此处依赖是针对JDK8的,其实也写了一个JDK11的,提交代码的时候发现编译不通过,才想起公司运行的JDK版本是JDK1.8的。(一定要注意依赖版本)

 <dependency><groupId>org.docx4j</groupId><artifactId>docx4j-ImportXHTML</artifactId><version>8.3.10</version><exclusions><exclusion><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-impl</artifactId></exclusion><exclusion><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.docx4j</groupId><artifactId>docx4j-JAXB-Internal</artifactId><version>8.3.10</version><exclusions><exclusion><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-impl</artifactId></exclusion></exclusions></dependency><!-- 手动指定新版JAXB依赖 --><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.1</version></dependency><dependency><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-impl</artifactId><version>2.3.8</version></dependency><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1.1</version></dependency><dependency><groupId>org.docx4j</groupId><artifactId>docx4j-JAXB-ReferenceImpl</artifactId><version>8.3.10</version></dependency><!-- 其他工具 --><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.14.3</version></dependency>

代码实现

 private static final Map<String, String> IMAGE_CACHE = new ConcurrentHashMap<>();private static final ExecutorService IMAGE_EXECUTOR = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);public String exportSowToWord(String fileName, HashMap<String, String> param)throws Exception {// 1. 批量获取数据String versionId = param.getOrDefault("versionId", "test");List<CatalogTressDTO> catalogList = zentaoProSowCatalogMapper.queryTreeMode(versionId);// 批量获取所有内容List<String> catalogIds = catalogList.stream().map(CatalogTressDTO::getId).collect(Collectors.toList());Map<String, ZentaoProSowContent> contentMap = zentaoProSowContentMapper.selectList(new LambdaQueryWrapper<ZentaoProSowContent>().in(ZentaoProSowContent::getCatalogId, catalogIds)).stream().collect(Collectors.toMap(ZentaoProSowContent::getCatalogId, Function.identity()));// 2. 构建完整HTML内容StringBuilder contentHtml = new StringBuilder();for (CatalogTressDTO catalog : catalogList) {// 处理标题if (StringUtils.isNotBlank(catalog.getIndentedTitle())) {contentHtml.append(buildHeadingTag(catalog));}// 处理内容ZentaoProSowContent content = contentMap.get(catalog.getId());if (content != null && StringUtils.isNotBlank(content.getContent())) {contentHtml.append(content.getContent());}}// 3. 统一处理图片和HTMLString fullHtml = "<!DOCTYPE html><html><head><meta charset='UTF-8'></head><body>"+ contentHtml.toString() + "</body></html>";String processedHtml = processHtmlWithImages(fullHtml);// 4. 生成Word文档ClassPathResource templateResource = new ClassPathResource("templates/sow_V2.0.docx");WordprocessingMLPackage wordPackage = WordprocessingMLPackage.load(templateResource.getInputStream());MainDocumentPart mainDoc = wordPackage.getMainDocumentPart();// 查找插入位置int insertIndex = findInsertPosition(mainDoc);// 添加HTML内容mainDoc.addAltChunk(AltChunkType.Html, processedHtml.getBytes(), mainDoc, insertIndex);mainDoc.convertAltChunks();ByteArrayOutputStream outputStream = new ByteArrayOutputStream();// 生成目录generateTableOfContents(wordPackage, insertIndex);// 保存文档wordPackage.save(outputStream);return buildResponse(fileName, outputStream.toByteArray());}private String buildHeadingTag(CatalogTressDTO catalog) {int level = catalog.getLevel() != null ? Math.min(Integer.parseInt(catalog.getLevel()), 6) : 1;return String.format("<h%d style='mso-style-name:标题%d'>%s</h%d>",level, level, catalog.getIndentedTitle(), level);}private int findInsertPosition(MainDocumentPart mainDoc) {List<Object> content = mainDoc.getContent();for (int i = 0; i < content.size(); i++) {if (content.get(i) instanceof P) {P p = (P) content.get(i);String text= TextUtils.getText(p);if (text != null && text.contains("插入的内容")) {content.remove(i);  // 移除占位符段落return i+1;          // 返回插入位置}}}return content.size();  // 默认插入到文档末尾}private void generateTableOfContents(WordprocessingMLPackage wordPackage, int insertIndex) throws Exception {TocGenerator tocGenerator = new TocGenerator(wordPackage);Toc.setTocHeadingText("目录");tocGenerator.generateToc(insertIndex - 1, "TOC \\o \"1-3\" \\h \\z \\u ", true);}private String processHtmlWithImages(String html) {Document doc = Jsoup.parse(html);Elements imgs = doc.select("img");// 并行处理图片List<CompletableFuture<Void>> futures = imgs.stream().map(img -> CompletableFuture.runAsync(() -> processImageTag(img), IMAGE_EXECUTOR)).collect(Collectors.toList());// 等待所有任务完成CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();return doc.html();}private void processImageTag(Element img) {try {String src = img.attr("src");if (StringUtils.isBlank(src)) return;String networkUrl = convertToNetworkUrl(src);String base64 = IMAGE_CACHE.computeIfAbsent(networkUrl, this::fetchImageBase64);// 异步获取图片尺寸CompletableFuture<BufferedImage> imageFuture = CompletableFuture.supplyAsync(() -> {try {return ImageIO.read(new URL(networkUrl));} catch (Exception e) {return null;}}, IMAGE_EXECUTOR);BufferedImage image = imageFuture.get(3, TimeUnit.SECONDS);if (image != null) {int scaledWidth = (int) (image.getWidth() * 0.9);int scaledHeight = (int) (image.getHeight() * 0.9);img.attr("width", String.valueOf(scaledWidth)).attr("height", String.valueOf(scaledHeight));}img.attr("src", base64);} catch (Exception e) {img.attr("src", "#error");}}private String fetchImageBase64(String imageUrl) {try (InputStream in = new URL(imageUrl).openStream()) {byte[] bytes = IOUtils.toByteArray(in);String mimeType = getMimeType(imageUrl);return "data:" + mimeType + ";base64," + Base64.getEncoder().encodeToString(bytes);} catch (Exception e) {return "#error";}}// 以下为原有工具方法保持不变private String convertToNetworkUrl(String relativePath) {//富文本保存的是相对路径return "http://10.80.88.93:8090/" + relativePath.replaceFirst("^(?:\\.\\./)+", "");}private String getMimeType(String url) {if (url.endsWith(".png")) return "image/png";if (url.endsWith(".jpg") || url.endsWith(".jpeg")) return "image/jpeg";if (url.endsWith(".gif")) return "image/gif";return "application/octet-stream";}private String buildResponse(String fileName, byte[] content) throws UnsupportedEncodingException {//直接返回文件
//        String encodeFileName = URLEncoder.encode(fileName, "UTF-8").replace("\\+", "%20");
//        HttpHeaders header = new HttpHeaders();
//        header.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//        header.add("Content-Disposition", "attachment; filename=" + encodeFileName);
//        return new ResponseEntity<>(content, header, HttpStatus.OK);//上传到MINISOMultipartFile multipartFile = convertByteArrayToMultipartFile(content, fileName);Result result = fileFeign.addFileByInfo(multipartFile);String id = ((Map<String, Object>) result.getData()).get("id").toString();return id;}public MultipartFile convertByteArrayToMultipartFile(byte[] fileBytes, String filename) {return new MultipartFile() {@Overridepublic String getName() {return "file"; // 表单字段名}@Overridepublic String getOriginalFilename() {return filename;}@Overridepublic String getContentType() {return "application/octet-stream"; // 默认二进制流,可自定义(如 "image/png"}@Overridepublic boolean isEmpty() {return fileBytes == null || fileBytes.length == 0;}@Overridepublic long getSize() {return fileBytes.length;}@Overridepublic byte[] getBytes() throws IOException {return fileBytes;}@Overridepublic InputStream getInputStream() throws IOException {return new ByteArrayInputStream(fileBytes);}@Overridepublic void transferTo(File dest) throws IOException, IllegalStateException {try (FileOutputStream fos = new FileOutputStream(dest)) {fos.write(fileBytes);}}};}
}
http://www.dtcms.com/wzjs/244882.html

相关文章:

  • cms做网站不用后端网页制作工具
  • 阎良网站建设信息流推广渠道有哪些
  • 不一样的婚恋网站怎么做站长之家下载
  • 电子商务网站建设步代运营电商公司
  • 网站制作验收单交换链接平台
  • 经销做网站都有什么好处活动推广朋友圈文案
  • 蚌埠做网站多少钱昆明百度关键词优化
  • 如何做网站流量统计网络广告投放公司
  • 荆门公司做网站企业seo自助建站系统
  • 最新网站建设上海关键词优化的技巧
  • 国内商城网站建设江西seo
  • 哪些网站做任务好赚钱沈阳seo关键字优化
  • 自己的服务器如何给网站备案华夏思源培训机构官网
  • 运动健身型网站开发友情链接买卖平台
  • 苏州做网站需要多少钱北京百度推广电话号码
  • wordpress iis设置方法5g网络优化培训
  • 小程序开发定制平台在线工具seo
  • 域名备案成功怎么做网站自动推广工具
  • 杭州制作网页灰色行业seo
  • 门户网站要求企业管理培训课程视频
  • 付费阅读wordpress郑州百度seo
  • 大学网站开发与管理知识总结天津seo技术教程
  • 根据百度地图做网站百度应用平台
  • 网站建设发展方向sem是什么基团
  • 优秀网站主题今日小说排行榜百度搜索榜
  • 阿里巴巴网站服务器成本网络优化器
  • 我的世界做图片的网站品牌营销活动策划方案
  • 中国河北建设银行官网招聘网站优质的seo网站排名优化软件
  • 做网站标志有限颜色使用的吗企业网站seo案例分析
  • 嘉兴做营销型网站设计西安百度seo代理