企业网站开发研究现状重庆seo论
- 作者: 五速梦信息网
- 时间: 2026年03月21日 09:59
当前位置: 首页 > news >正文
企业网站开发研究现状,重庆seo论,网站建设与管理就业,兰州网站建设方案详细1.前言
由于最近做了一个需求#xff0c;界面上有一个按钮#xff0c;点击按钮后将一个文件夹中的word文档显示在页面中#xff0c;并且有一个下拉框可以选择不同的文档#xff0c;选择文档可以显示该文档。这里我选择使用fr.opensagres.poi.xwpf.converter.pdf-gae依赖包…1.前言
由于最近做了一个需求界面上有一个按钮点击按钮后将一个文件夹中的word文档显示在页面中并且有一个下拉框可以选择不同的文档选择文档可以显示该文档。这里我选择使用fr.opensagres.poi.xwpf.converter.pdf-gae依赖包来实现。
2.依赖
这里我只依赖了这些依赖包 dependencygroupIdfr.opensagres.xdocreport/groupIdartifactIdfr.opensagres.poi.xwpf.converter.pdf-gae/artifactIdversion2.0.1/version/dependency!– Apache PDFBox 依赖用于.docx转PDF –dependencygroupIdorg.apache.pdfbox/groupIdartifactIdpdfbox/artifactIdversion3.0.0/version !– 根据最新版本调整 –/dependency3.代码
Java代码部分这里我使用了两个文件夹中的文档
package com.hxgis.controller;import java.io.File;
import java.io.FileInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
//import org.apache.poi.xwpf.converter.pdf.PdfOptions;
//import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** ClassName: DocumentController* Package: com.hxgis.controller* Description:** Author dhn* Create 2024/1/31 11:56* Version 1.0*/
RestController
RequestMapping(/api/document)
public class DocumentController {private final String MONTH_PATH D:/ftp_data/month; // 修改为你的文档存储路径private final String QUARTER_PATH D:/ftp_data/quarter; // 修改为你的文档存储路径// private final String MONTH_PATH /home/geli/hbnyWeatherReport/month; // 修改为你的文档存储路径
// private final String QUARTER_PATH /home/geli/hbnyWeatherReport/quarter; // 修改为你的文档存储路径GetMapping(/monthLatest)public ResponseEntityResource getDocument(RequestParam(required false) String name) throws IOException {File folder new File(MONTH_PATH);File[] files folder.listFiles((dir, filename) - filename.endsWith(.docx));if (files null || files.length 0) {return ResponseEntity.notFound().build();}File fileToConvert;if (name ! null !name.isEmpty()) {// 用户选择了特定的文件fileToConvert Arrays.stream(files).filter(file - file.getName().equals(name)).findFirst().orElse(null);} else {// 没有特定选择找最新的文件Pattern pattern Pattern.compile((\d{4})年(\d{2})月风光资源趋势预测\.docx);fileToConvert Arrays.stream(files).filter(file - pattern.matcher(file.getName()).matches()).max(Comparator.comparingInt(file - {Matcher matcher pattern.matcher(file.getName());if (matcher.find()) {int year Integer.parseInt(matcher.group(1));int month Integer.parseInt(matcher.group(2));return year * 100 month;}return 0;})).orElse(null);}if (fileToConvert null) {return ResponseEntity.notFound().build();}// 以下为文件转换为PDF的代码try (XWPFDocument document new XWPFDocument(new FileInputStream(fileToConvert));ByteArrayOutputStream out new ByteArrayOutputStream()) {PdfOptions options PdfOptions.create();PdfConverter.getInstance().convert(document, out, options);byte[] pdfContent out.toByteArray();ByteArrayResource resource new ByteArrayResource(pdfContent);return ResponseEntity.ok().contentLength(pdfContent.length).header(Content-type, application/pdf).body(resource);}}GetMapping(/quarterLatest)public ResponseEntityResource quarterLatest(RequestParam(required false) String name) throws IOException {File folder new File(QUARTER_PATH);File[] files folder.listFiles((dir, filename) - filename.endsWith(.docx));if (files null || files.length 0) {return ResponseEntity.notFound().build();}File fileToConvert;if (name ! null !name.isEmpty()) {// 用户选择了特定的文件fileToConvert Arrays.stream(files).filter(file - file.getName().equals(name)).findFirst().orElse(null);} else {// 没有特定选择找最新的文件Pattern pattern Pattern.compile((\d{4})年(?:\d{2})月-(\d{4})年(?:\d{2})月风光资源趋势预测\.docx| (\d{4})年(?:\d{2})-(\d{2})月风光资源趋势预测\.docx);fileToConvert Arrays.stream(files).filter(file - pattern.matcher(file.getName()).matches()).max(Comparator.comparingInt(file - {Matcher matcher pattern.matcher(file.getName());if (matcher.find()) {if (matcher.group(1) ! null) {// 处理跨年文件名int yearStart Integer.parseInt(matcher.group(1));int yearEnd Integer.parseInt(matcher.group(2));return yearEnd * 100 (matcher.group(4) ! null ? Integer.parseInt(matcher.group(4)) : 0);} else if (matcher.group(3) ! null) {// 处理同一年文件名int year Integer.parseInt(matcher.group(3));int monthEnd Integer.parseInt(matcher.group(4));return year * 100 monthEnd;}}return 0;})).orElse(null);}if (fileToConvert null) {return ResponseEntity.notFound().build();}// 文件转换为PDF的代码try (XWPFDocument document new XWPFDocument(new FileInputStream(fileToConvert));ByteArrayOutputStream out new ByteArrayOutputStream()) {PdfOptions options PdfOptions.create();PdfConverter.getInstance().convert(document, out, options);byte[] pdfContent out.toByteArray();ByteArrayResource resource new ByteArrayResource(pdfContent);return ResponseEntity.ok().contentLength(pdfContent.length).header(Content-type, application/pdf).body(resource);}}GetMapping(/monthList)public ListString listAllDocs() {File folder new File(MONTH_PATH);// 修改为获取.docx文件File[] files folder.listFiles((dir, name) - name.endsWith(.docx));if (files null) {return Collections.emptyList();}// 返回文件名列表return Arrays.stream(files).map(File::getName).collect(Collectors.toList());}GetMapping(/quarterList)public ListString quarterList() {File folder new File(QUARTER_PATH);// 修改为获取.docx文件File[] files folder.listFiles((dir, name) - name.endsWith(.docx));if (files null) {return Collections.emptyList();}// 返回文件名列表return Arrays.stream(files).map(File::getName).collect(Collectors.toList());}}
前端代码html代码使用两个按钮点击后弹出模态框在模态框中有iframe来展示pdf
!– 模态框Modal –
div classmodal fade idpdfModalMonth tabindex-1 roledialog aria-labelledbypdfModalLabelMonth aria-hiddentruediv classmodal-dialog stylemax-width: 90%; width: auto;div classmodal-contentdiv classmodal-headerh4 classmodal-title idpdfModalLabelMonth月度预测文档/h4button typebutton classclose data-dismissmodal aria-labelClosespan aria-hiddentruetimes;/span/button/divdiv classmodal-body!– 下拉框用于选择文档 –select idMonthList classform-controloption value请选择文档/option!– 文档列表将在这里填充 –/selectiframe idMonthViewer stylewidth:100%; height:700px;/iframe !– 可以根据需要调整高度 –/divdiv classmodal-footerbutton typebutton classbtn btn-default data-dismissmodal关闭/button/div/div/div
/divdiv classmodal fade idpdfModalQuarter tabindex-1 roledialog aria-labelledbypdfModalLabelQuarter aria-hiddentruediv classmodal-dialog stylemax-width: 90%; width: auto;div classmodal-contentdiv classmodal-headerh4 classmodal-title idpdfModalLabelQuarter月度预测文档/h4button typebutton classclose data-dismissmodal aria-labelClosespan aria-hiddentruetimes;/span/button/divdiv classmodal-body!– 下拉框用于选择文档 –select idQuarterList classform-controloption value请选择文档/option!– 文档列表将在这里填充 –/selectiframe idQuarterViewer stylewidth:100%; height:700px;/iframe !– 可以根据需要调整高度 –/divdiv classmodal-footerbutton typebutton classbtn btn-default data-dismissmodal关闭/button/div/div/div
/div
a classbtn btn-sm btn-primary idloadMonth styletext-decoration: none;color: #ffffff月度预测/aa classbtn btn-sm btn-primary idloadQuarter styletext-decoration: none;color: #ffffff季度预测/ajs代码
\((document).ready(function() {// 加载月度列表function loadMonthList() {\).get(/api/document/monthList, function(data) {// 清空现有的选项\((#MonthList).empty();\)(#MonthList).append(option value请选择文档/option);// 假设文档名包含日期例如 2024年01月风光资源趋势预测.docx// 对文档进行倒序排序data.sort(function(a, b) {// 转换为日期格式进行比较var dateA new Date(a.split(年)[0], a.split(年)[1].split(月)[0] - 1);var dateB new Date(b.split(年)[0], b.split(年)[1].split(月)[0] - 1);return dateB - dateA; // 从新到旧排序});// 添加新的选项data.forEach(function(doc) {\((#MonthList).append(option value doc doc /option);});});}// 当点击加载最新文档的按钮时\)(#loadMonth).click(function() {loadMonthList(); // 调用函数加载文档列表// 加载最新的PDF文档\((#MonthViewer).attr(src, /api/document/monthLatest);// 显示模态框\)(#pdfModalMonth).modal(show);});// 当选择不同的文档时\((#MonthList).change(function() {var selectedFile \)(this).val();if(selectedFile) {\((#MonthViewer).attr(src, /api/document/monthLatest?name encodeURIComponent(selectedFile));}});// 加载季度列表function parseDateFromDocName(docName) {var year, month;var parts docName.match(/(\d{4})年(\d{2})-(\d{2})月|(\d{4})年(\d{2})月-(\d{4})年(\d{2})月/);if (parts) {if (parts[1]) {// 格式是 YYYY年MM-DD月year parseInt(parts[1], 10);month parseInt(parts[2], 10); // 使用开始月份} else {// 格式是 YYYY年MM月-YYYY年MM月year parseInt(parts[4], 10);month parseInt(parts[5], 10); // 使用开始月份}}return new Date(year, month - 1); // JavaScript中的月份是从0开始的}function loadQuarterList() {\).get(/api/document/quarterList, function(data) {// 清空现有的选项\((#QuarterList).empty();\)(#QuarterList).append(option value请选择文档/option);// 对文档进行倒序排序data.sort(function(a, b) {var dateA parseDateFromDocName(a);var dateB parseDateFromDocName(b);return dateB - dateA; // 从新到旧排序});// 添加新的选项data.forEach(function(doc) {\((#QuarterList).append(option value doc doc /option);});});}// 当点击加载最新文档的按钮时\)(#loadQuarter).click(function() {loadQuarterList(); // 调用函数加载文档列表// 加载最新的PDF文档\((#QuarterViewer).attr(src, /api/document/quarterLatest);// 显示模态框\)(#pdfModalQuarter).modal(show);});// 当选择不同的文档时\((#QuarterList).change(function() {var selectedFile \)(this).val();if(selectedFile) {$(#QuarterViewer).attr(src, /api/document/quarterLatest?name encodeURIComponent(selectedFile));}});
});
4.遇到的问题
做完一切后发现有些文档中的标题的中文没有显示出来我就对比显示的文档和没显示的文档发现是因为字体的原因宋体是可以显示出来的但是宋体中文显示不出来。把宋体中文改成宋体就可以显示。 但是这只是我windows系统上运行是没问题的我把项目部署到服务器centos后发现中文一点都展示不出来这时候我就很纳闷为什么在windows上能显示出来linux上显示不出来经过查阅资料我发现是由于Linux上缺乏一些中文字体例如宋体、仿宋等这些字体是我文档中用到的字体所以下一步我要将windows中的字体放在服务器上。
5.在Linux中安装宋体
在Linux系统中安装宋体SimSun字体需要手动下载字体文件或从Windows系统中复制字体文件然后将其安装到Linux系统中。宋体不包含在开源字体包中因为它是微软的商业字体。下面是一般步骤
从Windows复制如果你有访问Windows系统的权限可以从C:\Windows\Fonts目录找到simsun.ttc宋体和其他中文字体文件并将其复制到你的Linux系统中。在Linux系统上安装字体一般有以下几个步骤 创建字体目录如果尚不存在
sudo mkdir -p /usr/share/fonts/chinese将字体文件复制到创建的目录中假设你已经将simsun.ttc字体文件复制到了Linux系统的某个位置例如~/Downloads运行以下命令将其移动到字体目录
sudo cp ~/Downloads/simsun.ttc /usr/share/fonts/chinese/更新字体缓存安装完字体后需要更新字体缓存以便系统识别新安装的字体
sudo fc-cache -fv安装后你可以使用fc-list命令确认字体是否已正确安装
fc-list | grep simsun至此大功告成
- 上一篇: 企业网站开发需求文档个人交养老保险价格表
- 下一篇: 企业网站开发一般多少钱公司网站建设p开发
相关文章
-
企业网站开发需求文档个人交养老保险价格表
企业网站开发需求文档个人交养老保险价格表
- 技术栈
- 2026年03月21日
-
企业网站开发哪个好薇怎么加快登录网站速度
企业网站开发哪个好薇怎么加快登录网站速度
- 技术栈
- 2026年03月21日
-
企业网站开发建设市场推广策略
企业网站开发建设市场推广策略
- 技术栈
- 2026年03月21日
-
企业网站开发一般多少钱公司网站建设p开发
企业网站开发一般多少钱公司网站建设p开发
- 技术栈
- 2026年03月21日
-
企业网站开发用什么mvc5 网站开发之美 pdf
企业网站开发用什么mvc5 网站开发之美 pdf
- 技术栈
- 2026年03月21日
-
企业网站开发知名品牌有哪些可以设计制作网页的软件
企业网站开发知名品牌有哪些可以设计制作网页的软件
- 技术栈
- 2026年03月21日






