java使用apache.poi导出word文件
功能说明:
将试卷导出word,并可以打印,装订,效果图:
下面是实现代码:
package com.xxxxx.business.course.utils;
import com.alibaba.fastjson.JSONObject;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import com.xxxxx.business.course.domain.*;
import com.xxxxx.business.utils.UtilTools;
import com.xxxxx.business.utils.domain.JudgeRule;
import com.xxxxx.business.utils.impl.BaseImpl;
import com.xxxxx.common.core.domain.R;
import com.xxxxx.common.core.utils.DateUtils;
import com.xxxxx.common.core.utils.StringUtils;
import com.xxxxx.common.core.utils.file.FileTypeUtils;
import com.xxxxx.common.core.utils.file.ImageUtils;
import com.xxxxx.common.core.utils.reflect.ReflectUtils;
import com.xxxxx.common.core.utils.sign.Base64;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
/**
* 试卷归档帮助类
*/
public class VerifyPaperFileUtils extends BaseImpl {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
public static VerifyPaperFileUtils getInstance() {
return new VerifyPaperFileUtils();
}
private VerifyPaperFileUtils() {
}
private Integer pageNum = 0;
private XWPFDocument docx;
private CTSectPr ctSectPr;
private VerifyPaper verifyPaper;
private PaperTemplate paperTemplate;
private boolean includeAnswer = false;
private String imgRegex = "img";
private String[] quSortNoArr = new String[]{"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十"};
private String rgb_black = "000000";
/**
* 外部调用方法,导出word
*
* @param response
* @param verifyPaper
* @throws IOException
*/
public R<?> exportWord(HttpServletResponse response, VerifyPaper verifyPaper, PaperTemplate paperTemplate) throws Exception {
this.verifyPaper = verifyPaper;
if (paperTemplate == null) paperTemplate = VerifyPaperFileUtils.getDefaultPaperTemplate();
this.paperTemplate = paperTemplate;
R<?> r = this.init();
if (!r.isSuccess()) throw new Exception(r.getMsg());
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.setCharacterEncoding("utf-8");
return this.exportWord(response);
}
/**
* 验证相关数据
*
* @return
*/
private R<?> init() {
if (this.verifyPaper == null) {
return R.fail("未选择试卷");
}
if (this.verifyPaper.getVerifyPaperQtList() == null || verifyPaper.getVerifyPaperQtList().size() == 0) {
return R.fail("未设置课程题型");
}
if (this.verifyPaper.getVerifyPaperQtList().stream().filter(s -> s.getVerifyPaperQuList() == null || s.getVerifyPaperQuList().size() == 0).count() > 0)
return R.fail("有题型未设置试题");
return validTemplate();
}
private R<?> validTemplate() {
if (this.paperTemplate == null) {
return R.fail("未设置模板");
}
return R.ok();
}
/**
* 导出word,私有方法
*
* @param response
* @throws IOException
*/
private R<?> exportWord(HttpServletResponse response) throws IOException, InvalidFormatException {
try {
R<?> r = writeWord();
if (!r.isSuccess()) return r;
docx.write(response.getOutputStream());
return R.ok();
} catch (Exception e) {
logger.error("导出Word异常,{}", e.getMessage());
throw e;
} finally {
IOUtils.closeQuietly(docx);
}
}
/**
* 试题数据写入文档
*/
private R<?> writeWord() throws IOException, InvalidFormatException {
R<XWPFDocument> r = createDocxTemplate();
if (!r.isSuccess()) return R.fail(r.getMsg());
docx = r.getData();
writePaperInfo();
writeWordQts();
return R.ok();
}
/**
* 制作模板
* 模板各种设置
*
* @return
*/
private R<XWPFDocument> createDocxTemplate() {
XWPFDocument document = new NiceXWPFDocument();
R<?> r = writeWordPageSetting(document);
if (!r.isSuccess()) return R.fail(r.getMsg());
//整体设置
writeWordPageSetting(document);
//页眉
writeWordHeader(document);
//页脚
writeWordFooter(document);
return R.ok(document);
}
/**
* 页面设置
*
* @return
*/
private R<?> writeWordPageSetting(XWPFDocument docx) {
if (StringUtils.isEmpty(paperTemplate.getPtPageSize())) return R.fail("未设置模板尺寸");
ctSectPr = docx.getDocument().getBody().getSectPr();
if (ctSectPr == null) ctSectPr = docx.getDocument().getBody().addNewSectPr();
CTPageSz ctPageSz = ctSectPr.isSetPgSz() ? ctSectPr.getPgSz() : ctSectPr.addNewPgSz();
switch (paperTemplate.getPtPageSize().toUpperCase()) {
case "A3":
ctPageSz.setW(getTwipFromCM(29.7)); //设置宽度(这里是一个ulong类型,单位:缇 , 1厘米=567缇)
ctPageSz.setH(getTwipFromCM(42)); //设置高度(这里是一个ulong类型)
break;
case "A4":
ctPageSz.setW(getTwipFromCM(21));
ctPageSz.setH(getTwipFromCM(29.7));
break;
case "A5":
ctPageSz.setW(getTwipFromCM(14.8));
ctPageSz.setH(getTwipFromCM(21));
break;
case "B4":
ctPageSz.setW(getTwipFromCM(25.7));
ctPageSz.setH(getTwipFromCM(36));
break;
case "B5":
ctPageSz.setW(getTwipFromCM(18.2));
ctPageSz.setH(getTwipFromCM(25.7));
break;
case "16开":
ctPageSz.setW(getTwipFromCM(18.4));
ctPageSz.setH(getTwipFromCM(26));
break;
case "32开":
ctPageSz.setW(getTwipFromCM(13));
ctPageSz.setH(getTwipFromCM(18.4));
break;
case "大32开":
ctPageSz.setW(getTwipFromCM(14));
ctPageSz.setH(getTwipFromCM(20.3));
break;
default:
return R.fail(StringUtils.format("不支持的尺寸:{}", paperTemplate.getPtPageSize()));
}
if (paperTemplate.getPtDirection().intValue() == 1) // 横向
{
BigInteger tempW = ctPageSz.getW();
ctPageSz.setW(ctPageSz.getH());
ctPageSz.setH(tempW);
ctPageSz.setOrient(STPageOrientation.LANDSCAPE);
}
//设置边距
CTPageMar pageMar = ctSectPr.getPgMar();
if (pageMar == null) pageMar = ctSectPr.addNewPgMar();
pageMar.setBottom(getTwipFromCM(paperTemplate.getPtMarginBottom().doubleValue()));
pageMar.setTop(getTwipFromCM(paperTemplate.getPtMarginTop().doubleValue()));
pageMar.setRight(getTwipFromCM(paperTemplate.getPtMarginRight().doubleValue()));
pageMar.setLeft(getTwipFromCM(paperTemplate.getPtMarginLeft().doubleValue()));
pageMar.setHeader(getTwipFromCM(paperTemplate.getPtHeaderMargin().doubleValue()));//页眉到顶端的距离
pageMar.setFooter(getTwipFromCM(paperTemplate.getPtFooterMargin().doubleValue()));//页脚到低端的距离
if (paperTemplate.getPtMarginBottom().doubleValue() > 2) {
pageMar.setFooter(getTwipFromCM(1));
} else {
double leftH = paperTemplate.getPtMarginBottom().doubleValue() - 1;
if (leftH > 0) {
pageMar.setFooter(getTwipFromCM(leftH));
}
}
//处理分栏
if (paperTemplate.getPtColumNum().intValue() > 1) {
if (!ctSectPr.isSetCols()) ctSectPr.addNewCols();
if (!UtilTools.compareBigIntegerValue(BigInteger.valueOf(paperTemplate.getPtColumNum()), ctSectPr.getCols().getNum())) {
CTColumns ctColumns = ctSectPr.getCols();
ctColumns.setNum(BigInteger.valueOf(paperTemplate.getPtColumNum().longValue()));
ctSectPr.setCols(ctColumns);
}
}
return R.ok();
}
/**
* 设置头部
*/
private void writeWordHeader(XWPFDocument docx) {
if (StringUtils.isEmpty(paperTemplate.getPtSealLineType())) {
paperTemplate.setPtSealLineType("once");
}
XWPFHeader header = null;
//STHdrFtr.Enum headerType = StringUtils.equalsIgnoreCase(paperTemplate.getPtSealLineType(), "once") ? STHdrFtr.DEFAULT : STHdrFtr.Enum.forString(paperTemplate.getPtSealLineType());
if (!StringUtils.equalsIgnoreCase(paperTemplate.getPtSealLineType(), "once")) {
XWPFHeaderFooterPolicy headerFooterPolicy = docx.getHeaderFooterPolicy();
if (headerFooterPolicy == null) headerFooterPolicy = docx.createHeaderFooterPolicy();
String sealType = "";
sealType = paperTemplate.getPtSealLineType().toLowerCase();
switch (sealType) {
case "default":
//每页
header = headerFooterPolicy.createHeader(STHdrFtr.DEFAULT);
break;
case "odd":
//奇数页
header = headerFooterPolicy.createHeader(STHdrFtr.DEFAULT);
docx.setEvenAndOddHeadings(true);
break;
case "even":
//偶数页
header = headerFooterPolicy.createHeader(STHdrFtr.EVEN);
docx.setEvenAndOddHeadings(true);
break;
}
}
if (paperTemplate.getPtPageHeadShow() && StringUtils.isNotEmpty(paperTemplate.getPtPageHeadText())) {
if (header == null) {
XWPFHeaderFooterPolicy headerFooterPolicy = docx.getHeaderFooterPolicy();
if (headerFooterPolicy == null) headerFooterPolicy = docx.createHeaderFooterPolicy();
header = headerFooterPolicy.createHeader(STHdrFtr.DEFAULT);
}
XWPFParagraph p = header.createParagraph();
if (StringUtils.isNotEmpty(paperTemplate.getPtPageHeadAlign()))
p.setAlignment(Enum.valueOf(ParagraphAlignment.class, paperTemplate.getPtPageHeadAlign()));
XWPFRun run = p.createRun();
run.setText(paperTemplate.getPtPageHeadText());
run.setFontSize(paperTemplate.getPtPageHeadSize());
}
//在页眉里面添加装订线
//制作左边装订线
writeWordBroadside(docx, header);
}
/**
* 设置侧边栏
* 换新页面设置一个
*/
private void writeWordBroadside(XWPFDocument docx, XWPFHeader header) {
if (paperTemplate.getPtSealLineShow() == null || !paperTemplate.getPtSealLineShow()) return;
ctSectPr = docx.getDocument().getBody().getSectPr();
if (ctSectPr == null) ctSectPr = docx.getDocument().getBody().addNewSectPr();
//创建一个
CTPageMar pageMar = ctSectPr.isSetPgMar() ? ctSectPr.getPgMar() : ctSectPr.addNewPgMar();
//装订线宽度
BigInteger slideBarWidth = pageMar.getLeft();
if (pageMar.getGutter() != null) slideBarWidth.add(pageMar.getGutter());
if (paperTemplate.getPtSealLineWidth().doubleValue() > 2.5) {
slideBarWidth = slideBarWidth.subtract(getTwipFromCM(0.5));
}
BigInteger cellWidth = slideBarWidth.divide(BigInteger.valueOf(2));
//BigInteger top = BigInteger.valueOf(1);
BigInteger slideBarHeight = ctSectPr.getPgSz().getH().subtract(BigInteger.valueOf(2));
// 创建一个侧边栏
XWPFTable sidebarTable =
StringUtils.equalsIgnoreCase(paperTemplate.getPtSealLineType(), "once")
? docx.createTable(1, 2)
: header.createTable(1, 2);
CTTblPr ctTblPr = sidebarTable.getCTTbl().getTblPr();
if (ctTblPr == null) ctTblPr = sidebarTable.getCTTbl().addNewTblPr();
//设置宽度
ctTblPr.getTblW().setType(STTblWidth.DXA);
ctTblPr.getTblW().setW(slideBarWidth);
// BigInteger x = BigInteger.ZERO.subtract(slideBarWidth);
//居左
CTJc jcTable = ctTblPr.isSetJc() ? ctTblPr.getJc() : ctTblPr.addNewJc();
jcTable.setVal(STJc.LEFT);
sidebarTable.setTableAlignment(TableRowAlign.LEFT);
//定位
CTTblPPr ctTblPPr = ctTblPr.isSetTblpPr() ? ctTblPr.getTblpPr() : ctTblPr.addNewTblpPr();
ctTblPPr.setVertAnchor(STVAnchor.PAGE);
ctTblPPr.setHorzAnchor(STHAnchor.PAGE);
ctTblPPr.setTblpXSpec(STXAlign.LEFT);
ctTblPPr.setTblpYSpec(STYAlign.TOP);
ctTblPPr.setTblpX(BigInteger.ZERO);
ctTblPPr.setTblpY(BigInteger.ZERO);
//去除边框
setTableBorder(sidebarTable, false);
for (XWPFTableRow row : sidebarTable.getRows()) {
row.setHeight(slideBarHeight.intValue());
for (XWPFTableCell cell : row.getTableCells()) {
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
CTTcPr ctTcPr = cell.getCTTc().isSetTcPr() ? cell.getCTTc().getTcPr() : cell.getCTTc().addNewTcPr();
if (!ctTcPr.isSetTcW()) ctTcPr.addNewTcW();
ctTcPr.getTcW().setW(cellWidth);
//文字方向
CTTextDirection cellDirection = ctTcPr.isSetTextDirection() ? ctTcPr.getTextDirection() : ctTcPr.addNewTextDirection();
cellDirection.setVal(STTextDirection.BT_LR);
//垂直居中
CTVerticalJc ctVerticalJc = ctTcPr.isSetVAlign() ? ctTcPr.getVAlign() : ctTcPr.addNewVAlign();
ctVerticalJc.setVal(STVerticalJc.Enum.forString("center"));
//左对齐
CTOnOff noWap = ctTcPr.isSetNoWrap() ? ctTcPr.getNoWrap() : ctTcPr.addNewNoWrap();
noWap.setVal(STOnOff.Enum.forString("on"));
}
}
//学生信息
XWPFTableCell cellInfo = sidebarTable.getRow(0).getCell(0);
//String blankStr = " ";
String blankStr = "_____________";
XWPFParagraph paragraph = cellInfo.getParagraphs().get(0);
paragraph.setVerticalAlignment(TextAlignment.CENTER);
paragraph.setAlignment(ParagraphAlignment.CENTER);
//paragraph.setFontAlignment(2);
if (paperTemplate.getPtSealLineStudentCode()) {
addTextToSidebar(paragraph, " 学号:", false);
addTextToSidebar(paragraph, blankStr, false);
}
if (paperTemplate.getPtSealLineStudentName()) {
addTextToSidebar(paragraph, " 姓名:", false);
addTextToSidebar(paragraph, blankStr, false);
}
if (paperTemplate.getPtSealLineStudentCollege()) {
addTextToSidebar(paragraph, " 学院:", false);
addTextToSidebar(paragraph, blankStr, false);
}
if (paperTemplate.getPtSealLineStudentGrade()) {
addTextToSidebar(paragraph, " 年级:", false);
addTextToSidebar(paragraph, blankStr, false);
}
if (paperTemplate.getPtSealLineStudentDepartment()) {
addTextToSidebar(paragraph, " 系:", false);
addTextToSidebar(paragraph, blankStr, false);
}
if (paperTemplate.getPtSealLineStudentSpecialty()) {
addTextToSidebar(paragraph, " 专业:", false);
addTextToSidebar(paragraph, blankStr, false);
}
if (paperTemplate.getPtSealLineStudentClass()) {
addTextToSidebar(paragraph, " 班级:", false);
addTextToSidebar(paragraph, blankStr, false);
}
//装订线
XWPFTableCell cellLine = sidebarTable.getRow(0).getCell(1);
paragraph = cellLine.getParagraphs().get(0);
paragraph.setVerticalAlignment(TextAlignment.CENTER);
paragraph.setAlignment(ParagraphAlignment.CENTER);
// paragraph.setAlignment(ParagraphAlignment.CENTER);
// paragraph.setFontAlignment(2);
//String lineStr = "-----------------------------------------------";
String lineStr = "-------------------------------------------";
addTextToSidebar(paragraph, lineStr, false);
addTextToSidebar(paragraph, " 密 - 封 - 线 ", false);
addTextToSidebar(paragraph, lineStr, false);
}
private void addTextToSidebar(XWPFParagraph paragraph, String text, boolean isUnderline) {
XWPFRun studentRun = paragraph.createRun();
studentRun.setText(text);
studentRun.setFontSize(paperTemplate.getPtSealLineSize());
studentRun.setFontFamily(paperTemplate.getPtSealLineFamily());
if (isUnderline) {
studentRun.setUnderline(UnderlinePatterns.SINGLE);
}
}
private void addTextToSidebar(XWPFParagraph paragraph, String text, boolean isUnderline, boolean isBold) {
XWPFRun studentRun = paragraph.createRun();
studentRun.setText(text);
studentRun.setFontSize(paperTemplate.getPtSealLineSize());
studentRun.setFontFamily(paperTemplate.getPtSealLineFamily());
if (isBold) studentRun.setBold(true);
if (isUnderline) {
studentRun.setUnderline(UnderlinePatterns.SINGLE);
}
}
/**
* 设置页脚
*/
private void writeWordFooter(XWPFDocument docx) {
boolean hasFoot = paperTemplate.getPtPageFootShow() && StringUtils.isNotEmpty(paperTemplate.getPtPageFootText());
boolean hasPageNum = paperTemplate.getPtPageNumShow();
if (hasFoot || hasPageNum) {
XWPFHeaderFooterPolicy headerFooterPolicy = docx.getHeaderFooterPolicy();
if (headerFooterPolicy == null) headerFooterPolicy = docx.createHeaderFooterPolicy();
XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
XWPFParagraph p = footer.getParagraphs() != null && footer.getParagraphs().size() > 0 ? footer.getParagraphs().get(0) : footer.createParagraph();
if (hasFoot) {
if (StringUtils.isNotEmpty(paperTemplate.getPtPageFootAlign()))
p.setAlignment(Enum.valueOf(ParagraphAlignment.class, paperTemplate.getPtPageFootAlign()));
XWPFRun run = p.createRun();
run.setText(paperTemplate.getPtPageFootText());
run.setFontSize(paperTemplate.getPtPageFootSize());
if (hasPageNum) {
p.setAlignment(ParagraphAlignment.LEFT);
}
} else {
p.setAlignment(ParagraphAlignment.CENTER);
}
if (hasPageNum) {
//设置分页
if (hasFoot) {
CTTabStop tabStop = p.getCTP().getPPr().addNewTabs().addNewTab();
tabStop.setVal(STTabJc.RIGHT);
//设置位置?
int twipsPerInch = 1440;
tabStop.setPos(BigInteger.valueOf(6 * twipsPerInch));
}
XWPFRun run = p.createRun();
run.setFontSize(paperTemplate.getPtPageNumSize());
run.setText("第");
if (hasFoot) run.addTab();
//当前页 begin
//p.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
run = p.createRun();
run.setShadow(false);
run.setFontSize(paperTemplate.getPtPageNumSize());
CTFldChar fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));
run.setFontSize(paperTemplate.getPtPageNumSize());
CTText ctText = run.getCTR().addNewInstrText();
ctText.setStringValue("PAGE \\* MERGEFORMAT");
ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("end"));
//当前页 end
run = p.createRun();
run.setFontSize(paperTemplate.getPtPageNumSize());
run.setText("页 (共");
//总页数 begin
run = p.createRun();
run.setFontSize(paperTemplate.getPtPageNumSize());
fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));
run = p.createRun();
run.setFontSize(paperTemplate.getPtPageNumSize());
ctText = run.getCTR().addNewInstrText();
ctText.setStringValue("NUMPAGES \\* MERGEFORMAT");
ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
//run = p.createRun();
//run.setFontSize(paperTemplate.getPtPageNumSize());
fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("end"));
//总页数 end
run = p.createRun();
run.setFontSize(paperTemplate.getPtPageNumSize());
run.setText("页)");
}
}
}
/**
* 写入试卷信息
*/
private void writePaperInfo() throws IOException, InvalidFormatException {
//顶部标题
if (paperTemplate.getPtTitle() && StringUtils.isNotEmpty(verifyPaper.getVerifyPaperTopName())) {
XWPFParagraph topTitle = createParagraph(paperTemplate.getPtTitleFontSize());
topTitle.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = topTitle.createRun();
run.setFontFamily(paperTemplate.getPtTitleFontFamily());
run.setFontSize(paperTemplate.getPtTitleFontSize());
run.setText(verifyPaper.getVerifyPaperTopName());
run.setBold(paperTemplate.getPtTitleBold());
}
//试卷标题
if (StringUtils.isNotEmpty(verifyPaper.getVerifyPaperName())) {
XWPFParagraph topTitle = createParagraph(paperTemplate.getPtTitleSubFontSize());
topTitle.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = topTitle.createRun();
run.setFontFamily(paperTemplate.getPtTitleSubFontFamily());
run.setFontSize(paperTemplate.getPtTitleSubFontSize());
run.setText(verifyPaper.getVerifyPaperName());
run.setBold(paperTemplate.getPtTitleSubBold());
}
// 答案及标准字样
if (includeAnswer) {
XWPFParagraph topTitle = createParagraph(paperTemplate.getPtTitleSubFontSize());
topTitle.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = topTitle.createRun();
run.setFontFamily(paperTemplate.getPtTitleSubFontFamily());
run.setFontSize(paperTemplate.getPtTitleSubFontSize());
run.setText("参考答案及评分标准");
}
//考试信息
if (paperTemplate.getPtPaperInfoShow()) {
XWPFParagraph topTitle = createParagraph(paperTemplate.getPtPaperInfoSize());
topTitle.setAlignment(ParagraphAlignment.CENTER);
boolean haPre = false;
if (paperTemplate.getPtShowAnswerMode()) {
XWPFRun run = topTitle.createRun();
run.setFontFamily(paperTemplate.getPtPaperInfoFamily());
run.setFontSize(paperTemplate.getPtPaperInfoSize());
run.setText("考试形式:");
run = topTitle.createRun();
run.setFontFamily(paperTemplate.getPtPaperInfoFamily());
run.setFontSize(paperTemplate.getPtPaperInfoSize());
run.setText(StringUtils.format(" {} ", verifyPaper.getVpAnswerMode() == 0 ? "闭卷" : "开卷"));
run.setUnderline(UnderlinePatterns.SINGLE);
haPre = true;
}
if (paperTemplate.getPtShowAnswerTime()) {
XWPFRun run = topTitle.createRun();
run.setFontFamily(paperTemplate.getPtPaperInfoFamily());
run.setFontSize(paperTemplate.getPtPaperInfoSize());
run.setText((haPre ? " " : "") + "考试时间:");
run = topTitle.createRun();
run.setFontFamily(paperTemplate.getPtPaperInfoFamily());
run.setFontSize(paperTemplate.getPtPaperInfoSize());
run.setText(StringUtils.format(" {} ", verifyPaper.getVpTotalTimeMinute()));
run.setUnderline(UnderlinePatterns.SINGLE);
run = topTitle.createRun();
run.setFontFamily(paperTemplate.getPtPaperInfoFamily());
run.setFontSize(paperTemplate.getPtPaperInfoSize());
run.setText("分钟");
haPre = true;
}
if (paperTemplate.getPtShowAnswerTime()) {
XWPFRun run = topTitle.createRun();
run.setFontFamily(paperTemplate.getPtPaperInfoFamily());
run.setFontSize(paperTemplate.getPtPaperInfoSize());
run.setText((haPre ? " " : "") + "满分:");
run = topTitle.createRun();
run.setFontFamily(paperTemplate.getPtPaperInfoFamily());
run.setFontSize(paperTemplate.getPtPaperInfoSize());
run.setText(StringUtils.format(" {} ", verifyPaper.getVpTotalMark().stripTrailingZeros().toPlainString()));
run.setUnderline(UnderlinePatterns.SINGLE);
run = topTitle.createRun();
run.setFontFamily(paperTemplate.getPtPaperInfoFamily());
run.setFontSize(paperTemplate.getPtPaperInfoSize());
run.setText("分。");
haPre = true;
}
addNewBlankLine(docx);
}
//得分栏
if (paperTemplate.getPtScoreColumn()) {
int colNum = 2 + verifyPaper.getVerifyPaperQtList().size();
double colWidth = 2;
XWPFTable scoreTable = docx.createTable(3, colNum);
scoreTable.setTableAlignment(TableRowAlign.CENTER);
scoreTable.setWidth(getTwipFromCM(colWidth * colNum).intValue());
setTableBorder(scoreTable, true);
for (XWPFTableRow row : scoreTable.getRows()) {
row.setHeight(getTwipFromCM(1).intValue());
for (XWPFTableCell cell : row.getTableCells()) {
cell.setWidth(getTwipFromCM(colWidth).toString());
}
}
setCellText(scoreTable.getRows().get(0).getTableCells().get(0), "题 号",
paperTemplate.getPtScoreColumnFamily(), paperTemplate.getPtScoreColumnSize(),
paperTemplate.getPtScoreColumnBold(), false,
ParagraphAlignment.CENTER, XWPFTableCell.XWPFVertAlign.CENTER);
for (int i = 0; i < verifyPaper.getVerifyPaperQtList().size(); i++) {
setCellText(scoreTable.getRow(0).getCell(i + 1), this.quSortNoArr[i],
paperTemplate.getPtScoreColumnFamily(), paperTemplate.getPtScoreColumnSize(),
paperTemplate.getPtScoreColumnBold(), false,
ParagraphAlignment.CENTER, XWPFTableCell.XWPFVertAlign.CENTER);
}
setCellText(scoreTable.getRow(0).getCell(colNum - 1), "总 分",
paperTemplate.getPtScoreColumnFamily(), paperTemplate.getPtScoreColumnSize(),
paperTemplate.getPtScoreColumnBold(), false,
ParagraphAlignment.CENTER, XWPFTableCell.XWPFVertAlign.CENTER);
setCellText(scoreTable.getRow(1).getCell(0), "得 分",
paperTemplate.getPtScoreColumnFamily(), paperTemplate.getPtScoreColumnSize(),
paperTemplate.getPtScoreColumnBold(), false,
ParagraphAlignment.CENTER, XWPFTableCell.XWPFVertAlign.CENTER);
setCellText(scoreTable.getRow(2).getCell(0), "评阅人",
paperTemplate.getPtScoreColumnFamily(), paperTemplate.getPtScoreColumnSize(),
paperTemplate.getPtScoreColumnBold(), false,
ParagraphAlignment.CENTER, XWPFTableCell.XWPFVertAlign.CENTER);
addNewBlankLine(docx);
}
if (StringUtils.isNotEmpty(paperTemplate.getPtReadme())) {
XWPFTable readmeTable = docx.createTable(1, 2);
readmeTable.setTableAlignment(TableRowAlign.CENTER);
readmeTable.setWidth(getTwipFromCM(15).intValue());
setTableBorder(readmeTable, false);
XWPFTableCell cell1 = readmeTable.getRow(0).getCell(0);
cell1.setWidth(getTwipFromCM(2).toString());
XWPFParagraph paragraph = cell1.getParagraphs().get(0);
paragraph.setVerticalAlignment(TextAlignment.TOP);
paragraph.setAlignment(ParagraphAlignment.RIGHT);
XWPFRun run = paragraph.createRun();
run.setFontSize(paperTemplate.getPtReadmeFontSize());
run.setFontFamily(paperTemplate.getPtReadmeFontFamily());
if (paperTemplate.getPtReadmeBold()) run.setBold(true);
run.setText("注意:");
//右侧注意事项
XWPFTableCell cell2 = readmeTable.getRow(0).getCell(1);
paragraph = cell2.getParagraphs().get(0);
paragraph.setSpacingLineRule(LineSpacingRule.AT_LEAST);
paragraph.setVerticalAlignment(TextAlignment.TOP);
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setFontSize(paperTemplate.getPtReadmeFontSize());
run.setFontFamily(paperTemplate.getPtReadmeFontFamily());
if (paperTemplate.getPtReadmeBold()) run.setBold(true);
run.setText(paperTemplate.getPtReadme());
addNewBlankLine(docx);
}
}
private XWPFParagraph createParagraph(Integer fontSize) {
XWPFParagraph paragraph = docx.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
paragraph.setVerticalAlignment(TextAlignment.CENTER);
if (fontSize != null) {
setParagraphTopPadding(paragraph, fontSize);
}
return paragraph;
}
/**
* 设置段落第一行头部间隔
* 间隔为字体的一半
*
* @param paragraph
* @param fontSize
*/
private void setParagraphTopPadding(XWPFParagraph paragraph, Integer fontSize) {
BigInteger space = getTwipFromPound(fontSize.doubleValue()).divide(BigInteger.valueOf(2));
paragraph.setSpacingBefore(space.intValue());
}
/**
* 表格设置文字
*
* @param cell
* @param text
* @param fontFamily
* @param fontSize
* @param isBold
* @param isUnderLine
* @param align
* @param vAlign
*/
private void setCellText(XWPFTableCell cell, String text, String fontFamily, Integer fontSize, Boolean isBold, Boolean isUnderLine,
ParagraphAlignment align, XWPFTableCell.XWPFVertAlign vAlign
) {
XWPFParagraph paragraph = cell.getParagraphs().get(0);
if (align != null)
paragraph.setAlignment(align);
if (vAlign != null)
cell.setVerticalAlignment(vAlign);
XWPFRun run = paragraph.createRun();
run.setText(text);
if (StringUtils.isNotEmpty(fontFamily)) run.setFontFamily(fontFamily);
if (fontSize != null) run.setFontSize(fontSize);
if (isBold != null) run.setBold(isBold);
if (isUnderLine != null && isUnderLine) run.setUnderline(UnderlinePatterns.SINGLE);
}
/**
* 添加空行
*
* @param document
*/
private void addNewBlankLine(XWPFDocument document) {
XWPFParagraph paragraph = document.createParagraph();
paragraph.createRun().setText("");
}
/**
* 设置题型
*/
private void writeWordQts() throws IOException, InvalidFormatException {
for (int i = 0; i < verifyPaper.getVerifyPaperQtList().size(); i++) {
VerifyPaperQt qt = verifyPaper.getVerifyPaperQtList().get(i);
//处理标题
writeWordQtTitle(qt, i);
//处理试题
writeQuestionForQt(qt);
}
}
/**
* 写入题型试题
*
* @return
*/
private void writeWordQtTitle(VerifyPaperQt qt, Integer qtIndex) {
String ext = qt.getVpQtDescription();
if (StringUtils.isEmpty(ext))
ext = StringUtils.format("(本大题{}小题,每小题{}分,共{}分)", qt.getVpQtQuNum(), qt.getVpQtQuMark().stripTrailingZeros().toPlainString(), qt.getVpQtMark().stripTrailingZeros().toPlainString());
String title = StringUtils.format("{}、{} {}", quSortNoArr[qtIndex], qt.getVerifyPaperQtName(), ext);
//表格
if (paperTemplate.getPtQtScoreColumn() != null && paperTemplate.getPtQtScoreColumn()) {
if (qtIndex > 1) addNewBlankLine(docx);
setQtTitleScoreColumn(title);
} else {
XWPFParagraph paragraph = createParagraph(paperTemplate.getPtQtTitleSize());
paragraph.setAlignment(ParagraphAlignment.LEFT); //字体左对齐
XWPFRun runText = paragraph.createRun();
runText.setBold(paperTemplate.getPtQtTitleBold());
runText.setText(title);
runText.setFontSize(paperTemplate.getPtQtTitleSize().intValue());
runText.setFontFamily(paperTemplate.getPtQtTitleFamily(), XWPFRun.FontCharRange.ascii);
}
}
/**
* 添加得分栏+标题
* 创建表格合并行
*
* @param title
*/
private void setQtTitleScoreColumn(String title) {
BigInteger w = this.ctSectPr.getPgSz().getW();
XWPFTable pTable = docx.createTable(2, 2);
pTable.setTableAlignment(TableRowAlign.LEFT);
CTTblPr ctTblPr = pTable.getCTTbl().getTblPr();
ctTblPr.getTblW().setType(STTblWidth.DXA);
//设置宽度
ctTblPr.getTblW().setW(w);
mergeCell(pTable, 0, 1, 1);
pTable.getRow(0).setHeight(getTwipFromCM(1).intValue());
pTable.getRow(1).setHeight(getTwipFromCM(1).intValue());
//得分栏 2 厘米
XWPFTableCell cellScore = pTable.getRow(0).getCell(0);
cellScore.setWidth(getTwipFromCM(2).toString());
setCellText(cellScore, "得分", paperTemplate.getPtQtScoreColumnFamily(), paperTemplate.getPtQtScoreColumnSize(), true, false, ParagraphAlignment.CENTER, XWPFTableCell.XWPFVertAlign.CENTER);
//标题栏
XWPFTableCell cellTitle = pTable.getRow(0).getCell(1);
cellTitle.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
XWPFParagraph paragraph = cellTitle.getParagraphs().get(0);
paragraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQtTitleSize()).divide(BigInteger.valueOf(2)).intValue());
paragraph.setAlignment(ParagraphAlignment.LEFT);
paragraph.setVerticalAlignment(TextAlignment.CENTER);
XWPFRun runText = paragraph.createRun();
if (paperTemplate.getPtQtTitleBold() != null) runText.setBold(paperTemplate.getPtQtTitleBold());
runText.setText(title);
runText.setFontSize(paperTemplate.getPtQtTitleSize().intValue());
runText.setFontFamily(paperTemplate.getPtQtTitleFamily(), XWPFRun.FontCharRange.ascii);
setTableBorder(pTable, false);
//设置边框
CTTcBorders borders = CTTcBorders.Factory.newInstance();
CTBorder border = CTBorder.Factory.newInstance();
border.setVal(STBorder.SINGLE);
border.setColor(rgb_black);
border.setSz(BigInteger.ONE);
border.setSpace(BigInteger.ZERO);
borders.setLeft(border);
borders.setBottom(border);
borders.setRight(border);
borders.setTop(border);
CTTcPr ctTcPr1 = pTable.getRow(0).getCell(0).getCTTc().getTcPr();
if (ctTcPr1 == null) ctTcPr1 = pTable.getRow(0).getCell(0).getCTTc().addNewTcPr();
ctTcPr1.setTcBorders(borders);
CTTcPr ctTcPr2 = pTable.getRow(1).getCell(0).getCTTc().getTcPr();
if (ctTcPr2 == null) ctTcPr2 = pTable.getRow(1).getCell(0).getCTTc().addNewTcPr();
ctTcPr2.setTcBorders(borders);
}
//
// private void setQtTitleScoreColumn(String title) {
// BigInteger w = this.ctSectPr.getPgSz().getW();
// XWPFTable pTable = docx.createTable(1, 2);
// setTableBorder(pTable, false);
// pTable.setTableAlignment(TableRowAlign.LEFT);
// CTTblPr ctTblPr = pTable.getCTTbl().getTblPr();
// ctTblPr.getTblW().setType(STTblWidth.DXA);
// //设置宽度
// ctTblPr.getTblW().setW(w);
//
// pTable.getRow(0).setHeight(getTwipFromCM(2).intValue());
// //得分栏 2 厘米
// XWPFTableCell cellScore = pTable.getRow(0).getCell(0);
// cellScore.setWidth(getTwipFromCM(2).toString());
// setQtScoreColumnTable(cellScore);
//
// //标题栏
// XWPFTableCell cellTitle = pTable.getRow(0).getCell(1);
// cellTitle.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
//
// XWPFParagraph paragraph = cellTitle.getParagraphs().get(0);
// paragraph.setAlignment(ParagraphAlignment.LEFT);
// paragraph.setVerticalAlignment(TextAlignment.CENTER);
// XWPFRun runText = paragraph.createRun();
// if (paperTemplate.getPtQtTitleBold() != null) runText.setBold(paperTemplate.getPtQtTitleBold());
// runText.setText(title);
// runText.setFontSize(paperTemplate.getPtQtTitleSize().intValue());
// runText.setFontFamily(paperTemplate.getPtQtTitleFamily(), XWPFRun.FontCharRange.ascii);
// }
/**
* 合并单元格
*
* @param table 表格对象
* @param beginRowIndex 开始行索引
* @param endRowIndex 结束行索引
* @param colIndex 合并列索引
*/
private void mergeCell(XWPFTable table, int beginRowIndex, int endRowIndex, int colIndex) {
if (beginRowIndex == endRowIndex || beginRowIndex > endRowIndex) {
return;
}
//合并行单元格的第一个单元格
CTVMerge startMerge = CTVMerge.Factory.newInstance();
startMerge.setVal(STMerge.RESTART);
//合并行单元格的第一个单元格之后的单元格
CTVMerge endMerge = CTVMerge.Factory.newInstance();
endMerge.setVal(STMerge.CONTINUE);
CTTc ctTc = table.getRow(beginRowIndex).getCell(colIndex).getCTTc();
(ctTc.isSetTcPr() ? ctTc.getTcPr() : ctTc.addNewTcPr()).setVMerge(startMerge);
for (int i = beginRowIndex + 1; i <= endRowIndex; i++) {
ctTc = table.getRow(i).getCell(colIndex).getCTTc();
(ctTc.isSetTcPr() ? ctTc.getTcPr() : ctTc.addNewTcPr()).setVMerge(endMerge);
}
}
/**
* 获取得分栏
*
* @return
*/
private void setQtScoreColumnTable(XWPFTableCell cell) {
XWPFTable xwpfTable = docx.createTable(2, 1);// new XWPFTable(CTTbl.Factory.newInstance(), cell, 2, 1);//
cell.insertTable(0, xwpfTable);
logger.info("表格:{}", cell.getTables().size());
xwpfTable = cell.getTables().get(0);
xwpfTable.setTableAlignment(TableRowAlign.CENTER);
xwpfTable.setWidth(getTwipFromCM(2).toString());
//设置边框
setTableBorder(xwpfTable, true);
XWPFTableRow row1 = xwpfTable.getRow(0);
row1.setHeight(getTwipFromCM(1).intValue());
XWPFTableCell cell1 = row1.getCell(0);
cell1.setWidth(getTwipFromCM(2).toString());
cell1.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
XWPFParagraph p = cell1.getParagraphs().get(0);
p.setAlignment(ParagraphAlignment.CENTER); //字体左对齐
p.setVerticalAlignment(TextAlignment.CENTER);
XWPFRun rt = p.createRun();
rt.setBold(paperTemplate.getPtQtScoreColumnBold());
rt.setFontSize(paperTemplate.getPtScoreColumnSize());
rt.setFontFamily(paperTemplate.getPtScoreColumnFamily(), XWPFRun.FontCharRange.ascii);
rt.setText("得分");
XWPFTableRow row2 = xwpfTable.getRow(1);
row2.setHeight(getTwipFromCM(1).intValue());
}
/**
* 写入题型试题
*
* @return
*/
private void writeQuestionForQt(VerifyPaperQt qt) throws IOException, InvalidFormatException {
for (VerifyPaperQu qu : qt.getVerifyPaperQuList()) {
getQuLines(qu, qu.getVpQuIndexQt());
}
}
/**
* 将文字写入段落
*
* @param paragraph
* @param line
* @throws IOException
* @throws InvalidFormatException
*/
private void writeWordQuLine(XWPFParagraph paragraph, String line) throws IOException, InvalidFormatException {
for (LineIncludeImg lineImg : getContainImgArr(line)) {
if (StringUtils.isEmpty(lineImg.text) && lineImg.image == null) continue;
String imgLine = lineImg.text;
String picExtension = "";
if (imgLine == null) imgLine = "";
byte[] imgBytes = null;
if (lineImg.image != null) {
imgLine = "";
imgBytes = lineImg.image;
} else if (imgLine.startsWith("data:image/")) {
String base64Str = imgLine.substring(imgLine.indexOf(',') + 1);
picExtension = imgLine.substring(imgLine.indexOf('/') + 1, imgLine.indexOf(';'));
imgBytes = Base64.decode(base64Str);
for (int i = 0; i < imgBytes.length; ++i) {
if (imgBytes[i] < 0) {
imgBytes[i] += 256;
}
}
imgLine = "";
}
if (imgBytes != null) {
InputStream inputStream = new ByteArrayInputStream(imgBytes);
BufferedImage image = ImageIO.read(inputStream);
int width = image.getWidth();
int height = image.getHeight();
inputStream.close();
XWPFRun picRun = paragraph.createRun();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String picName = simpleDateFormat.format(DateUtils.getNowDate()) + "." + picExtension;
InputStream picStream = new ByteArrayInputStream(imgBytes);
//picRun.addPicture(picStream, getPictureType(picExtension), picName, width, height);
double scaling = 1.0;
//2)2023-06-13 优化 若图片宽度超过word的最大宽值,图片将会变形、所以通过等比缩小来处理 word文档中:宽大最大650px(17厘米)
BigDecimal maxWidthWord = new BigDecimal(650);
BigDecimal widthDecimal = new BigDecimal(width);
if (widthDecimal.compareTo(maxWidthWord) > 0) {
BigDecimal divide = maxWidthWord.divide(widthDecimal, 2, RoundingMode.HALF_UP);
scaling = divide.doubleValue();
picRun.addPicture(picStream, getPictureType(picExtension), picName, Units.toEMU(width - width * scaling), Units.toEMU(height - height * scaling));
} else {
picRun.addPicture(picStream, getPictureType(picExtension), picName, Units.toEMU(width), Units.toEMU(height));
}
CTInline inline = picRun.getCTR().getDrawingList().get(0).getInlineArray(0);
inline.getDocPr().setId(1L); //id必须从1开始 解决打开文档时 报错(NPOI v2.4 已修复)
//此处是否可关闭
picStream.close();
} else if (StringUtils.isNotEmpty(imgLine)) {
//创建 run对象
XWPFRun runText = paragraph.createRun();
runText.setText(imgLine);
runText.setBold(paperTemplate.getPtQuStemBold());
runText.setFontSize(paperTemplate.getPtQuStemSize());
runText.setFontFamily(paperTemplate.getPtQuStemFamily(), XWPFRun.FontCharRange.ascii);
}
}
}
/**
* 获取一道试题段落
*
* @param qe
* @param qIndex
* @return
*/
private void getQuLines(VerifyPaperQu qe, Integer qIndex) throws IOException, InvalidFormatException {
if (qe.getQuestionIsMultipleStem() == null || !qe.getQuestionIsMultipleStem()) {
//无子试题
//list.addAll();
getSingleLines(qe, qIndex);
} else {
//有子试题
getMultipleLines(qe, qIndex);
}
}
/**
* 获取单题面行
*
* @param qe
* @return
*/
private void getSingleLines(VerifyPaperQu qe, Integer qIndex) throws IOException, InvalidFormatException {
VerifyPaperQuStem quStem = qe.getVerifyPaperQuStem();
//题面
String stem = StringUtils.getTextFromHTML(quStem.getQuStem(), imgRegex);
String line = StringUtils.format("{}. ", qIndex);
XWPFParagraph stemParagraph = createParagraph(paperTemplate.getPtQuStemSize());
//试题题号加粗
XWPFRun run = stemParagraph.createRun();
run.setText(line);
run.setFontSize(paperTemplate.getPtQuStemSize());
run.setBold(true);
writeWordQuLine(stemParagraph, stem);
//选项
for (int i = 0; i < 10; i++) {
char c = ((char) (65 + i));
String optionTag = String.valueOf(c).toUpperCase();
String pName = StringUtils.format("quOption{}{}", optionTag, i + 1);
String option = ReflectUtils.invokeGetter(quStem, pName);
if (StringUtils.isEmpty(option)) break;
option = StringUtils.getTextFromHTML(option, imgRegex);
XWPFParagraph optionParagraph = createParagraph(paperTemplate.getPtQuStemSize());
optionParagraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize() * 2).intValue());
writeWordQuLine(optionParagraph, StringUtils.format("{}、{}", optionTag, option));
}
//答案
if (includeAnswer && StringUtils.isNotEmpty(quStem.getQuAnswer())) {
String answer = StringUtils.getTextFromHTML(quStem.getQuAnswer(), imgRegex);
XWPFParagraph paragraph = createParagraph(paperTemplate.getPtQuStemSize());
paragraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize() * 2).intValue());
writeWordQuLine(paragraph, StringUtils.format("{}{}", importRule.AnswerStartWord, answer));
}
//评分点,填空题不显示评分点
if (includeAnswer && StringUtils.isNotEmpty(quStem.getQuJudgePoint()) && !qe.getQuestionIsFillBlank()) {
JudgeRule judgeRule = JSONObject.parseObject(quStem.getQuJudgePoint(), JudgeRule.class);
if (judgeRule.getItems() != null && judgeRule.getItems().size() > 0) {
String points = StringUtils.joinWith(importRule.PointSeparateWord, judgeRule.getItems().get(0).getPoints().stream().map(p -> p.getAnswer()).toArray(String[]::new));
XWPFParagraph paragraph = createParagraph(paperTemplate.getPtQuStemSize());
paragraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize() * 2).intValue());
writeWordQuLine(paragraph, StringUtils.format("{}{}", importRule.PointStartWord, points));
}
}
//评析
if (includeAnswer && StringUtils.isNotEmpty(quStem.getQuAnalyse())) {
String answer = StringUtils.getTextFromHTML(quStem.getQuAnalyse(), imgRegex);
XWPFParagraph paragraph = createParagraph(paperTemplate.getPtQuStemSize());
paragraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize() * 2).intValue());
writeWordQuLine(paragraph, StringUtils.format("{}{}", importRule.AnalyseStartWord, answer));
}
if (includeAnswer) {
//难度
XWPFParagraph paragraph = createParagraph(paperTemplate.getPtQuStemSize());
paragraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize() * 2).intValue());
writeWordQuLine(paragraph, StringUtils.format("{}{}", importRule.DifficultyStartWord, qe.getDifficulty()));
}
//知识点
if (includeAnswer && StringUtils.isNotEmpty(qe.getKnowledgeList())) {
//须处理关系 ?????????
//list.add(StringUtils.format("{}{}", importRule.KnowledgeStartWord,qe.getKnowledgeList().));
}
//年份
if (includeAnswer) {
XWPFParagraph paragraph = createParagraph(paperTemplate.getPtQuStemSize());
paragraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize() * 2).intValue());
writeWordQuLine(paragraph, StringUtils.format("{}{}", importRule.YearStartWord, qe.getQuestionYear()));
}
//来源
if (includeAnswer && StringUtils.isNotEmpty(qe.getQuestionOrigin())) {
XWPFParagraph paragraph = createParagraph(paperTemplate.getPtQuStemSize());
paragraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize() * 2).intValue());
writeWordQuLine(paragraph, StringUtils.format("{}{}", importRule.OriginStartWord, qe.getQuestionOrigin()));
}
docx.createParagraph().createRun().setText("\n");
//添加一个空行
//list.add("\n");
if (!includeAnswer && qe.getQuestionIsSubjective()) {
//自适应高度计算,根据答案文字数量
}
}
/**
* 获取多题面试题行
*
* @param qe
* @return
*/
private void getMultipleLines(VerifyPaperQu qe, Integer qIndex) throws IOException, InvalidFormatException {
VerifyPaperQuStem quStem = qe.getVerifyPaperQuStem();
//题面
String stem = StringUtils.getTextFromHTML(quStem.getQuStem(), imgRegex);
String line = (StringUtils.format("{}.", qIndex.toString()));
XWPFParagraph stemParagraph = createParagraph(paperTemplate.getPtQuStemSize());
//试题题号加粗
XWPFRun run = stemParagraph.createRun();
run.setText(line);
run.setFontSize(paperTemplate.getPtQuStemSize());
run.setBold(true);
writeWordQuLine(stemParagraph, stem);
//子试题
for (int i = 0; i < qe.getSubQuestionList().size(); i++) {
getSubQuLines(qe.getSubQuestionList().get(i), i + 1);
}
//评析
if (includeAnswer && StringUtils.isNotEmpty(quStem.getQuAnalyse())) {
String answer = StringUtils.getTextFromHTML(quStem.getQuAnalyse(), imgRegex);
XWPFParagraph paragraph = createParagraph(paperTemplate.getPtQuStemSize());
paragraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize() * 2).intValue());
writeWordQuLine(paragraph, StringUtils.format("{}{}", importRule.AnalyseStartWord, answer));
}
//难度
if (includeAnswer) {
XWPFParagraph paragraph = createParagraph(paperTemplate.getPtQuStemSize());
paragraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize() * 2).intValue());
writeWordQuLine(paragraph, StringUtils.format("{}{}", importRule.DifficultyStartWord, qe.getDifficulty()));
//知识点
if (includeAnswer && StringUtils.isNotEmpty(qe.getKnowledgeList())) {
//list.add(StringUtils.format("{}{}", importRule.KnowledgeStartWord, qe.getKnowledgeName()));
}
//年份
paragraph = createParagraph(paperTemplate.getPtQuStemSize());
paragraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize() * 2).intValue());
writeWordQuLine(paragraph, StringUtils.format("{}{}", importRule.YearStartWord, qe.getQuestionYear()));
//来源
if (includeAnswer && StringUtils.isNotEmpty(qe.getQuestionOrigin())) {
paragraph = createParagraph(paperTemplate.getPtQuStemSize());
paragraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize() * 2).intValue());
writeWordQuLine(paragraph, StringUtils.format("{}{}", importRule.OriginStartWord, qe.getQuestionOrigin()));
}
}
//添加一个空行
this.addNewBlankLine(docx);
}
/**
* 获取子题面行
*
* @param subQe
* @return
*/
private void getSubQuLines(VerifyPaperQu subQe, Integer qSubIndex) throws IOException, InvalidFormatException {
//题面
VerifyPaperQuStem quStem = subQe.getVerifyPaperQuStem();
String stem = StringUtils.getTextFromHTML(quStem.getQuStem(), imgRegex);
String line = (StringUtils.format("{}). ", qSubIndex));
XWPFParagraph stemParagraph = createParagraph(paperTemplate.getPtQuStemSize());
stemParagraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize()).intValue());
//试题题号加粗
XWPFRun run = stemParagraph.createRun();
run.setText(line);
run.setFontSize(paperTemplate.getPtQuStemSize());
run.setBold(true);
writeWordQuLine(stemParagraph, stem);
//选项
for (int i = 0; i < 10; i++) {
char c = ((char) (65 + i));
String optionTag = String.valueOf(c).toUpperCase();
String pName = StringUtils.format("quOption{}{}", optionTag, i + 1);
String option = ReflectUtils.invokeGetter(quStem, pName);
if (StringUtils.isEmpty(option)) break;
option = StringUtils.getTextFromHTML(option, imgRegex);
XWPFParagraph optionParagraph = createParagraph(paperTemplate.getPtQuStemSize());
optionParagraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize()).intValue());
writeWordQuLine(optionParagraph, StringUtils.format("{}、{}", optionTag, option));
}
//答案
if (includeAnswer && StringUtils.isNotEmpty(quStem.getQuAnswer())) {
String answer = StringUtils.getTextFromHTML(quStem.getQuAnswer(), imgRegex);
XWPFParagraph paragraph = createParagraph(paperTemplate.getPtQuStemSize());
paragraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize()).intValue());
writeWordQuLine(paragraph, StringUtils.format("{}{}", importRule.AnswerStartWord, answer));
}
//评分点
if (includeAnswer && StringUtils.isNotEmpty(quStem.getQuJudgePoint()) && !subQe.getQuestionIsFillBlank()) {
JudgeRule judgeRule = JSONObject.parseObject(quStem.getQuJudgePoint(), JudgeRule.class);
if (judgeRule.getItems() != null && judgeRule.getItems().size() > 0) {
String points = StringUtils.joinWith(importRule.PointSeparateWord, judgeRule.getItems().get(0).getPoints().stream().map(p -> p.getAnswer()).toArray(String[]::new));
XWPFParagraph paragraph = createParagraph(paperTemplate.getPtQuStemSize());
paragraph.setIndentationLeft(getTwipFromPound(paperTemplate.getPtQuStemSize()).intValue());
writeWordQuLine(paragraph, StringUtils.format("{}{}", importRule.PointStartWord, points));
}
}
//添加一个空行
this.addNewBlankLine(docx);
if (!includeAnswer && subQe.getQuestionIsSubjective()) {
//自适应高度计算,根据答案文字数量
}
}
private boolean isMatchWithRegex(String regex, String input) {
if (StringUtils.isEmpty(regex)) return false;
return Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(input).find();
}
/**
* 通过图片分割
*
* @param str
* @return
*/
public List<LineIncludeImg> getContainImgArr(String str) {
String imgReg = "<img\\s*/?[^>]*>";
List<LineIncludeImg> list = new ArrayList<>();
if (!isMatchWithRegex(imgReg, str)) {
list.add(new LineIncludeImg(str));
return list;
}
String getSrcReg = "\\bsrc\\b\\s*=\\s*['\"]?([^'\"]*)['\"]?";// "<img.*src=('|\")(.+?)('|\").*/?[^>]*>";
String removeSrcReg = "(^\\bsrc\\b\\s*=\\s*['\"]?)|(['\"]?$)";
//读取base64
Matcher matcher = Pattern.compile(imgReg, Pattern.CASE_INSENSITIVE).matcher(str);
int _index = 0;
//2023-06-13 支持多个图片
while (matcher.find()) {
if (matcher.start() > _index) {
list.add(new LineIncludeImg(str.substring(_index, matcher.start())));
}
_index = matcher.end();
String group = str.substring(matcher.start(), matcher.end());
Matcher mSrc = Pattern.compile(getSrcReg, Pattern.CASE_INSENSITIVE).matcher(group);
if (mSrc.find()) {
String srcText = mSrc.group();
String src = srcText.replaceAll(removeSrcReg, "");
if (src.startsWith("data:image/")) {
list.add(new LineIncludeImg(src));
} else if (StringUtils.ishttp(src)) {
String extend = FileTypeUtils.getFileType(src);
byte[] srcBase64 = ImageUtils.getImage(src);// UtilTools.getImageDataFromServer(src);
if (StringUtils.isEmpty(extend)) extend = FileTypeUtils.getFileExtendName(srcBase64);
//下载图片获取base64HttpUtils
list.add(new LineIncludeImg(srcBase64, extend));
}
}
}
if (_index < str.length()) {
list.add(new LineIncludeImg(str.substring(_index)));
}
return list;
}
/**
* 获取图片类型
*
* @param picExtension
* @return
*/
public int getPictureType(String picExtension) {
switch (picExtension.toUpperCase()) {
case "EMF":
return 2;
case "WMF":
return 3;
case "PICT":
return 4;
case "JPEG":
return 5;
case "PNG":
return 6;
case "DIB":
return 7;
case "GIF":
return 8;
case "TIFF":
return 9;
case "EPS":
return 10;
case "BMP":
return 11;
case "WPG":
return 12;
default:
return 5;
}
}
public class LineIncludeImg {
public LineIncludeImg(byte[] image, String extend) {
this.image = image;
this.extend = extend;
}
public LineIncludeImg(String text) {
this.text = text;
}
/**
* 文本内容
*/
public String text;
//图片二进制数据
public byte[] image;
//扩展名,不带 .
public String extend;
}
/**
* 每厘米567缇
*/
private double teePerCM = 567;
/**
* 每厘米28.35磅
*/
private double poundPerCm = 28.35;
/**
* 根据磅,获取缇
*
* @return
*/
private BigInteger getTwipFromPound(double pound) {
Double d = pound * teePerCM / poundPerCm;
return BigInteger.valueOf(d.longValue());
}
/**
* 根据厘米,获取缇
*
* @param cm
* @return
*/
private BigInteger getTwipFromCM(double cm) {
Double d = cm * teePerCM;
return BigInteger.valueOf(d.longValue());
}
private void setTableBorder(XWPFTable table, boolean hasBorder) {
if (hasBorder) {
table.setTopBorder(XWPFTable.XWPFBorderType.SINGLE, 1, 0, rgb_black);
table.setRightBorder(XWPFTable.XWPFBorderType.SINGLE, 1, 0, rgb_black);
table.setLeftBorder(XWPFTable.XWPFBorderType.SINGLE, 1, 0, rgb_black);
table.setBottomBorder(XWPFTable.XWPFBorderType.SINGLE, 1, 0, rgb_black);
table.setInsideVBorder(XWPFTable.XWPFBorderType.SINGLE, 1, 0, rgb_black);
table.setInsideHBorder(XWPFTable.XWPFBorderType.SINGLE, 1, 0, rgb_black);
} else {
table.setLeftBorder(XWPFTable.XWPFBorderType.NONE, 0, 0, "");
table.setRightBorder(XWPFTable.XWPFBorderType.NONE, 0, 0, "");
table.setTopBorder(XWPFTable.XWPFBorderType.NONE, 0, 0, "");
table.setBottomBorder(XWPFTable.XWPFBorderType.NONE, 0, 0, "");
table.setInsideHBorder(XWPFTable.XWPFBorderType.NONE, 0, 0, "");
table.setInsideVBorder(XWPFTable.XWPFBorderType.NONE, 0, 0, "");
}
}
/**
* 获取默认模板
*
* @return
*/
public static PaperTemplate getDefaultPaperTemplate() {
PaperTemplate paperTemplate = new PaperTemplate();
paperTemplate.setPtFooterMargin(BigDecimal.ZERO);
paperTemplate.setPaperTemplateName("默认模板配置");
paperTemplate.setPaperTemplateCode("default-template");
paperTemplate.setPtPageSize("A4");
paperTemplate.setPtDirection(0);
paperTemplate.setPtColumNum(1);
paperTemplate.setPtEnFontFamily("");//Helvetica
paperTemplate.setPtTitle(true);
paperTemplate.setPtTitleBold(true);
paperTemplate.setPtTitleFontFamily("黑体");
paperTemplate.setPtTitleFontSize(18);
paperTemplate.setPtTitleSub("");
paperTemplate.setPtTitleSubBold(true);
paperTemplate.setPtTitleSubFontFamily("黑体");
paperTemplate.setPtTitleSubFontSize(18);
paperTemplate.setPtPaperInfoShow(true);
paperTemplate.setPtPaperInfoFamily("黑体");
paperTemplate.setPtPaperInfoSize(12);
paperTemplate.setPtPaperInfoBold(false);
paperTemplate.setPtShowAnswerMode(true);
paperTemplate.setPtShowAnswerTime(true);
paperTemplate.setPtShowPaperMark(true);
paperTemplate.setPtScoreColumn(true);
paperTemplate.setPtScoreColumnFamily("宋体");
paperTemplate.setPtScoreColumnSize(12);
paperTemplate.setPtScoreColumnBold(false);
paperTemplate.setPtReadme("");
paperTemplate.setPtReadmeBold(false);
paperTemplate.setPtReadmeFontFamily("宋体");
paperTemplate.setPtReadmeBold(false);
paperTemplate.setPtReadmeFontSize(12);
paperTemplate.setPtScoreColumn(true);
paperTemplate.setPtScoreColumnBold(false);
paperTemplate.setPtScoreColumnFamily("宋体");
paperTemplate.setPtScoreColumnSize(12);
paperTemplate.setPtQtScoreColumn(true);
paperTemplate.setPtQtScoreColumnBold(true);
paperTemplate.setPtQtScoreColumnFamily("宋体");
paperTemplate.setPtQtScoreColumnSize(12);
paperTemplate.setPtQtTitleBold(true);
paperTemplate.setPtQtTitleFamily("宋体");
paperTemplate.setPtQtTitleSize(12);
paperTemplate.setPtQuStemBold(false);
paperTemplate.setPtQuStemFamily("宋体");
paperTemplate.setPtQuStemSize(11);
paperTemplate.setPtMarginTop(BigDecimal.valueOf(2.54));
paperTemplate.setPtMarginBottom(BigDecimal.valueOf(2.54));
paperTemplate.setPtMarginLeft(BigDecimal.valueOf(3.18));
paperTemplate.setPtMarginRight(BigDecimal.valueOf(3.18));
paperTemplate.setPtPageHeadShow(false);
paperTemplate.setPtHeaderMargin(BigDecimal.ZERO);
paperTemplate.setPtPageHeadFontFamily("宋体");
paperTemplate.setPtPageHeadSize(11);
paperTemplate.setPtPageHeadText("");
paperTemplate.setPtPageHeadAlign("center");
paperTemplate.setPtPageFootShow(false);
paperTemplate.setPtFooterMargin(BigDecimal.ZERO);
paperTemplate.setPtPageFootFontFamily("宋体");
paperTemplate.setPtPageFootSize(11);
paperTemplate.setPtPageFootText("");
paperTemplate.setPtPageFootAlign("left");
paperTemplate.setPtPageNumShow(true);
paperTemplate.setPtPageNumAlign("center");
paperTemplate.setPtPageNumFontFamily("宋体");
paperTemplate.setPtPageNumSize(9);
paperTemplate.setPtSealLineShow(true);
paperTemplate.setPtSealLineType("once");
paperTemplate.setPtSealLineFamily("宋体");
paperTemplate.setPtSealLineSize(12);
paperTemplate.setPtSealLineStudentName(true);
paperTemplate.setPtSealLineStudentCode(true);
paperTemplate.setPtSealLineStudentCollege(true);
paperTemplate.setPtSealLineStudentGrade(true);
paperTemplate.setPtSealLineStudentSpecialty(true);
paperTemplate.setPtSealLineStudentDepartment(false);
paperTemplate.setPtSealLineStudentClass(false);
return paperTemplate;
}
}
补充工具类 ----------------------------------------------------
package com.xxxxx.business.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONValidator;
import com.xxxxx.business.utils.domain.LabelValuePair;
import com.xxxxx.common.core.text.CharsetKit;
import com.xxxxx.common.core.text.Convert;
import com.xxxxx.common.core.utils.SpringUtils;
import com.xxxxx.common.core.utils.StringUtils;
import com.xxxxx.common.core.utils.bean.BeanUtils;
import com.xxxxx.common.core.utils.file.FileTypeUtils;
import com.xxxxx.common.core.utils.file.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BOMInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.web.multipart.MultipartFile;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.*;
/**
* 工具类
*/
public class UtilTools {
private static final Logger log = LoggerFactory.getLogger(UtilTools.class);
/**
* 通过请求填充实体对象
*
* @param beanClass
* @param request
* @return
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static Object FillEntityFromRequest(Class<?> beanClass, HttpServletRequest request) throws IllegalAccessException, InstantiationException {
Object o = SpringUtils.getBean(beanClass);// beanClass.newInstance();
Map<String, Object> mapObject = new HashMap<String, Object>();
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(beanClass);
for (PropertyDescriptor p : pds) {
String pName = p.getName();
String pVal = request.getParameter(pName);
Method m = p.getWriteMethod();
if (m != null) {
if (pVal != null && !pVal.startsWith("[object ")) mapObject.put(pName, pVal);
}
}
if (mapObject.size() > 0) {
o = JSONObject.parseObject(JSONObject.toJSONString(mapObject), beanClass);
}
return o;
}
/**
* 克隆集合
*
* @param collection
* @param <T>
* @return
* @throws ClassNotFoundException
* @throws IOException
*/
@SuppressWarnings("unchecked")
public static <T> Collection<T> cloneCollection(Collection<T> collection) throws ClassNotFoundException, IOException {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(collection);
out.close();
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
Collection<T> dest = (Collection<T>) in.readObject();
in.close();
return dest;
}
/**
* 克隆对象
*
* @param obj
* @param <T>
* @return
*/
@SuppressWarnings("unchecked")
public static <T extends Serializable> T cloneObject(T obj) {
T cloneObj = null;
try {
// 写⼊字节流
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream obs = new ObjectOutputStream(out);
obs.writeObject(obj);
obs.close();
// 分配内存,写⼊原始对象,⽣成新对象
ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream ois = new ObjectInputStream(ios);
// 返回⽣成的新对象
cloneObj = (T) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
return cloneObj;
}
/**
* map 转换对象
*
* @param map
* @param <T>
* @return
*/
public static <T extends Serializable> T mapToObject(Map<String, Object> map) {
if (map == null) return null;
String jsonString = JSON.toJSONString(map);
JSONObject jsonObject = JSON.parseObject(jsonString);
T t = (T) jsonObject;
return t;
}
/**
* map 转换对象
*
* @param map
* @param clazz
* @param <T>
* @return
*/
public static <T extends Serializable> T mapToObject(Map<String, Object> map, Class<T> clazz) {
if (map == null) return null;
String jsonString = JSON.toJSONString(map);
T t = BeanUtils.instantiateClass(clazz);
BeanUtils.copyBeanProp(t, JSON.parseObject(jsonString, clazz));
return t;
}
/**
* map集合 转换对象集合
*
* @param mapList
* @param <T>
* @return
*/
public static <T extends Serializable> List<T> mapToObjectList(List<Map<String, Object>> mapList) {
if (mapList == null) return null;
if (mapList.size() == 0) return new ArrayList<>();
List<T> tList = new ArrayList<>();
//避免字符串过大,无法解析,故循环
for (int i = 0; i < mapList.size(); i++) {
tList.add(mapToObject(mapList.get(i)));
}
return tList;
}
/**
* map集合 转换对象集合
*
* @param mapList
* @param clazz
* @param <T>
* @return
*/
public static <T extends Serializable> List<T> mapToObjectList(List<Map<String, Object>> mapList, Class<T> clazz) {
if (mapList == null) return null;
if (mapList.size() == 0) return new ArrayList<>();
List<T> tList = new ArrayList<>();
//避免字符串过大,无法解析,故循环
for (int i = 0; i < mapList.size(); i++) {
tList.add(mapToObject(mapList.get(i), clazz));
}
return tList;
}
/**
* 对象转 Map
*
* @param object
* @return Map<String, Object>
*/
public static Map<String, Object> objectToMap(Object object) {
if (object == null) return null;
JSONObject jsonObject = (JSONObject) JSON.toJSON(object);
Map<String, Object> map = new HashMap<>();
for (String key : jsonObject.keySet()) {
map.put(key, jsonObject.get(key));
}
return map;
}
/**
* 对象转 Map
*
* @param object
* @return Map<String, Object>
*/
public static Map<String, String> objectToStringMap(Object object) {
if (object == null) return null;
JSONObject jsonObject = (JSONObject) JSON.toJSON(object);
Map<String, String> map = new HashMap<>();
for (String key : jsonObject.keySet()) {
Object val = jsonObject.get(key);
if (val == null) continue;
map.put(key, val.toString());
}
return map;
}
public static List<LabelValuePair<Object, Object>> getListFromLabelValuePairs(List<LabelValuePair<Object, Object>> labelValuePairList, String label) {
List<LabelValuePair<Object, Object>> rList = new ArrayList<>();
for (LabelValuePair<Object, Object> labelValuePair : labelValuePairList) {
if (StringUtils.equalsIgnoreCase(labelValuePair.getLabel().toString(), label)) rList.add(labelValuePair);
}
return rList;
}
public static Long strToLong(String inputStr, long def) {
try {
if (inputStr != null && inputStr.length() > 0) return Long.valueOf(inputStr);
} catch (NumberFormatException e) {
}
return def;
}
public static Long objToLong(Object inputObj, long def) {
try {
String inputStr = inputObj == null ? "0" : inputObj.toString();
if (inputStr != null && inputStr.length() > 0) return Long.valueOf(inputStr);
} catch (NumberFormatException e) {
}
return def;
}
public static Integer strToInteger(String inputStr, int def) {
try {
if (inputStr != null && inputStr.length() > 0) return Integer.valueOf(inputStr);
} catch (NumberFormatException e) {
}
return def;
}
public static Integer objToInteger(Object intObj, int def) {
try {
if (intObj == null) return def;
return strToInteger(intObj.toString(), def);
} catch (NumberFormatException e) {
}
return def;
}
public static boolean strToBool(String inputStr, boolean def) {
if (inputStr == null || inputStr.isEmpty()) return def;
inputStr = inputStr.trim().toLowerCase();
switch (inputStr) {
case "0":
return false;
case "1":
return true;
case "-1":
return true;
case "否":
return false;
case "是":
return true;
case "false":
return false;
case "true":
return true;
default:
return def;
}
}
public static boolean objToBool(Object inputObj, boolean def) {
String inputStr = inputObj == null ? "" : inputObj.toString();
return strToBool(inputStr, def);
}
public static boolean strToBoolForQuestion(String inputStr, boolean def) {
if (inputStr == null || inputStr.isEmpty()) return def;
inputStr = inputStr.trim().toLowerCase();
switch (inputStr) {
case "0":
case "否":
case "错误":
case "错":
case "false":
case "×":
return false;
case "1":
case "-1":
case "是":
case "正确":
case "对":
case "true":
case "√":
return true;
default:
return def;
}
}
private static Random random = null;
public static Random getRandom() {
if (random == null) random = new Random();
return random;
}
/**
* 获取随机字符串,数字类型
*
* @param length
* @return
*/
public static String GetRandomNumbers(int length) {
String _random = "";
if (length <= 0) return _random;
List<String> sList = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
sList.add(String.valueOf(i));
}
for (int i = 0; i < length; i++) {
int rid = getRandom().nextInt(10);
_random = _random.concat(sList.get(rid));
}
return _random;
}
/**
* 判断BigDecimal是否相等
* null -> 0
*
* @param d1
* @param d2
* @return
*/
public static boolean compareBigDecimal(BigDecimal d1, BigDecimal d2) {
if (d1 == null) d1 = BigDecimal.valueOf(0);
if (d2 == null) d2 = BigDecimal.valueOf(0);
return d1.setScale(2).compareTo(d2.setScale(2)) == 0;
}
public static boolean gtBigDecimal(BigDecimal d1) {
if (d1 == null) d1 = BigDecimal.valueOf(0);
BigDecimal d2 = BigDecimal.valueOf(0);
return d1.setScale(2).compareTo(d2.setScale(2)) > 0;
}
public static boolean geBigDecimal(BigDecimal d1) {
if (d1 == null) d1 = BigDecimal.valueOf(0);
BigDecimal d2 = BigDecimal.valueOf(0);
return d1.setScale(2).compareTo(d2.setScale(2)) >= 0;
}
public static boolean ltBigDecimal(BigDecimal d1) {
if (d1 == null) d1 = BigDecimal.valueOf(0);
BigDecimal d2 = BigDecimal.valueOf(0);
return d1.setScale(2).compareTo(d2.setScale(2)) < 0;
}
public static boolean leBigDecimal(BigDecimal d1) {
if (d1 == null) d1 = BigDecimal.valueOf(0);
BigDecimal d2 = BigDecimal.valueOf(0);
return d1.setScale(2).compareTo(d2.setScale(2)) <= 0;
}
/**
* 判断BigDecimal是否相等
*
* @param d1
* @param d2
* @param scale 比较精度
* @return
*/
public static int compareBigDecimal(BigDecimal d1, BigDecimal d2, int scale) {
if (d1 == null) d1 = BigDecimal.valueOf(0);
if (d2 == null) d2 = BigDecimal.valueOf(0);
return d1.setScale(scale).compareTo(d2.setScale(scale));
}
/**
* 比较 Integer 数值 null 为 0
*
* @param d1
* @param d2
* @return
*/
public static boolean compareIntegerValue(Integer d1, Integer d2) {
if (d1 == null) d1 = 0;
if (d2 == null) d2 = 0;
return d1.intValue() == d2.intValue();
}
public static boolean compareBigIntegerValue(BigInteger d1, BigInteger d2) {
if (d1 == null) d1 = BigInteger.ZERO;
if (d2 == null) d2 = BigInteger.ZERO;
return d1.intValue() == d2.intValue();
}
public static int compareIntegerValueByInt(Integer d1, Integer d2) {
if (d1 == null) d1 = 0;
if (d2 == null) d2 = 0;
return d1.compareTo(d2.intValue());
}
public static boolean gtIntegerValue(Integer d1) {
if (d1 == null) d1 = 0;
Integer d2 = 0;
return d1.intValue() > d2.intValue();
}
/**
* 大于等于0
*
* @param d1
* @return
*/
public static boolean geIntegerValue(Integer d1) {
if (d1 == null) d1 = 0;
Integer d2 = 0;
return d1.intValue() >= d2.intValue();
}
/**
* 小于0
*
* @param d1
* @return
*/
public static boolean ltIntegerValue(Integer d1) {
if (d1 == null) d1 = 0;
Integer d2 = 0;
return d1.intValue() < d2.intValue();
}
/**
* 小于等于0
*
* @param d1
* @return
*/
public static boolean leIntegerValue(Integer d1) {
if (d1 == null) d1 = 0;
Integer d2 = 0;
return d1.intValue() <= d2.intValue();
}
/**
* 比较Long 数值 null 为 0
*
* @param d1
* @param d2
* @return
*/
public static boolean compareLongValue(Long d1, Long d2) {
if (d1 == null) d1 = 0L;
if (d2 == null) d2 = 0L;
return d1.longValue() == d2.longValue();
}
public static int compareLongValueByInt(Long d1, Long d2) {
if (d1 == null) d1 = 0L;
if (d2 == null) d2 = 0L;
return Long.compare(d1, d2);
}
/**
* 判断大于0
*
* @param d1
* @return
*/
public static boolean gtLongValueZero(Long d1) {
if (d1 == null) d1 = 0L;
Long d2 = 0L;
return d1.longValue() > d2.longValue();
}
/**
* 大于等于
*
* @param d1
* @return
*/
public static boolean geLongValueZero(Long d1) {
if (d1 == null) d1 = 0L;
Long d2 = 0L;
return d1.longValue() >= d2.longValue();
}
/**
* 判断小于等于0
*
* @param d1
* @return
*/
public static boolean leLongValueZero(Long d1) {
if (d1 == null) d1 = 0L;
Long d2 = 0L;
return d1.longValue() <= d2.longValue();
}
/**
* 判断小于0
*
* @param d1
* @return
*/
public static boolean ltLongValueZero(Long d1) {
if (d1 == null) d1 = 0L;
Long d2 = 0L;
return d1.longValue() < d2.longValue();
}
/**
* 比较大小
*
* @param inum Integer
* @param num int
* @return inum-num
*/
public static int compareIntegerAndInt(Integer inum, int num) {
if (inum == null) inum = Integer.valueOf(0);
return inum - num;
}
/**
* 字符串转换成Int
*
* @param num
* @param defVal
* @return
*/
public static int StringToInt(String num, int defVal) {
try {
if (num == null || num.equals("")) return defVal;
return new Integer(num).intValue();
} catch (Exception e) {
return defVal;
}
}
/**
* 获取错误信息
*
* @param e
* @return
*/
public static String getExceptionInfo(Exception e) {
StringBuffer info = new StringBuffer("\n" + e.toString() + "\n");
StackTraceElement[] elements = e.getStackTrace();
for (StackTraceElement element : elements) {
info.append("\t\t" + element.toString() + "\n");
}
return info.toString();
}
/**
* 比较大小
*
* @param str1
* @param str2
* @return 比较有效字符,区分大小写
*/
public static boolean compareString(String str1, String str2) {
if (str1 == null) str1 = "";
if (str2 == null) str2 = "";
return StringUtils.equals(str1, str2);
}
/**
* 生成 MD5
*
* @param data 待处理数据
* @return MD5结果
*/
public static String MD5(String data) {
StringBuilder sb = new StringBuilder();
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] array = md.digest(data.getBytes("UTF-8"));
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return sb.toString().toUpperCase();
}
/**
* 校验银行卡号方法
*
* @param bankCard
* @return
*/
public static boolean checkBankCard(String bankCard) {
if (bankCard.length() < 15 || bankCard.length() > 19) {
return false;
}
char bit = getBankCardCheckCode(bankCard.substring(0, bankCard.length() - 1));
if (bit == 'N') {
return false;
}
return bankCard.charAt(bankCard.length() - 1) == bit;
}
/**
* 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
*
* @param nonCheckCodeBankCard
* @return
*/
private static char getBankCardCheckCode(String nonCheckCodeBankCard) {
if (nonCheckCodeBankCard == null || nonCheckCodeBankCard.trim().length() == 0
|| !nonCheckCodeBankCard.matches("\\d+")) {
//如果传的不是数据返回N
return 'N';
}
char[] chs = nonCheckCodeBankCard.trim().toCharArray();
int luhmSum = 0;
for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
int k = chs[i] - '0';
if (j % 2 == 0) {
k *= 2;
k = k / 10 + k % 10;
}
luhmSum += k;
}
return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
}
/**
* 加密
*
* @param inputStr 明文
* @param key
* @return
*/
public static String aesEncrypt(String inputStr, String key) throws Exception {
if (inputStr == null || inputStr.isEmpty()) return inputStr;
if (key == null || key.isEmpty()) {
throw new Exception("参数错误");
}
String algorithm = "AES";
Cipher cipher = Cipher.getInstance(algorithm);
KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes(StandardCharsets.UTF_8));
keyGenerator.init(128, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
byte[] encodeKey = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(encodeKey, algorithm);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] bytes = cipher.doFinal(inputStr.getBytes(StandardCharsets.UTF_8));
byte[] encode = Base64.getEncoder().encode(bytes);
return new String(encode);
}
/**
* 解密
*
* @param encryptStr 密文
* @param key
* @return
* @throws Exception
*/
public static String aesDecrypt(String encryptStr, String key) throws Exception {
if (encryptStr == null || encryptStr.isEmpty()) return encryptStr;
if (key == null || key.isEmpty()) {
throw new Exception("参数错误");
}
String algorithm = "AES";
Cipher cipher = Cipher.getInstance(algorithm);
KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes(StandardCharsets.UTF_8));
keyGenerator.init(128, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
byte[] encodeKey = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(encodeKey, algorithm);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] bytes = cipher.doFinal(Base64.getDecoder().decode(encryptStr.getBytes(StandardCharsets.UTF_8)));
return new String(bytes);
}
/**
* 枚举转换JSONObject
*
* @param enu
* @return
*/
public static JSONObject enumToJsonObject(Enum enu) {
JSONObject jsonObject = new JSONObject();
BeanWrapper src = new BeanWrapperImpl(enu);
PropertyDescriptor[] pds = src.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
String key = pd.getName();
if ("class".equals(key) || "declaringClass".equals(key)) {
continue;
}
jsonObject.put(key, src.getPropertyValue(key));
}
return jsonObject;
}
/**
* 枚举转换JSONArray
*
* @param enus
* @return
*/
public static JSONArray enumToJsonArray(Enum[] enus) {
JSONArray jsonObject = new JSONArray();
for (Enum at : enus) {
jsonObject.add(enumToJsonObject(at));
}
return jsonObject;
}
/**
* 获取请求集合,先从body获取,若无取参数
*
* @param value
* @return
*/
public static Long[] stringToLongArray(String value) {
if (StringUtils.isEmpty(value)) return null;
if (JSONValidator.from(value).validate()) {
return JSON.parseObject(value, Long[].class);
}
return Convert.toLongArray(value);
}
/**
* 将前段传回的字符串,转换成字符串数组
*
* @param value
* @return
*/
public static String[] stringToStringArray(String value) {
if (StringUtils.isEmpty(value)) return null;
if (JSONValidator.from(value).validate()) {
return JSON.parseObject(value, String[].class);
}
return Convert.toStrArray(value);
}
// /**
// * 从html中获取去掉标签的文本
// *
// * @param HTML html字符串
// * @param ExceptTags 排除的标签 如:?!img
// * @return
// */
// public static String getTextFromHTML(String HTML, String ExceptTags) {
// //"?!img"
// String exceptStr = "?!br|p|img";
// if (StringUtils.isNotEmpty(ExceptTags)) {
// exceptStr = ExceptTags;
// }
// String regex = "</?(" + exceptStr + ")[^>]*>";
// String blankRegex = " ";
// String _html = HTML.replaceAll(blankRegex, " ");
// return _html.replaceAll(regex, "");
//}
/**
* 从txt文件流读取数据
*
* @param txtStream
* @return
* @throws IOException
*/
public static List<String> readFromTxt(InputStream txtStream) throws IOException {
List<String> paragraphList = new ArrayList<>();
List<InputStream> stemList = copySteam(txtStream, 2);
LabelValuePair<InputStream, Charset> result = getStreamCharset(stemList.get(0));
Charset cs = result.getValue();
BOMInputStream bomInputStream = new BOMInputStream(result.getLabel());
boolean hasBom = bomInputStream.hasBOM();
InputStreamReader sr = null;
if (hasBom) {
sr = new InputStreamReader(bomInputStream, Charset.forName(bomInputStream.getBOMCharsetName()));
stemList.get(1).close();
} else {
sr = new InputStreamReader(stemList.get(1), cs);
}
// InputStreamReader sr = hasBom ?
// new InputStreamReader(bomInputStream, Charset.forName(bomInputStream.getBOMCharsetName())) :
// new InputStreamReader(bomInputStream, cs);
BufferedReader br = new BufferedReader(sr);
String line = null;
Integer lineIndex = 0;
while ((line = br.readLine()) != null) {
if (!hasBom && lineIndex == 0) {
lineIndex++;
if (StringUtils.isNotEmpty(line)) {
byte[] bts = line.getBytes(cs);
if ((bts[0] == -1 && bts[1] == -2) || (bts[0] == -2 && bts[1] == -1)) {
byte[] newBts = new byte[bts.length - 2];
for (int i = 2; i < bts.length; i++) {
newBts[i - 2] = bts[i];
}
line = new String(newBts, cs);
}
}
}
if (StringUtils.isNotEmpty(line) && StringUtils.isNotEmpty(line.trim())) {
paragraphList.add(line);
//log.info("读取数据:{},长度:{},value:{}", line, line.trim().length(), line.getBytes(cs));
}
}
br.close();
sr.close();
return paragraphList;
}
/**
* 判断获取字节流 编码格式,主要用于txt文件内容读取
* 再次读取流,使用返回结果中的流
*
* @param stream
* @return
*/
public static LabelValuePair<InputStream, Charset> getStreamCharset(InputStream stream) throws IOException {
LabelValuePair<InputStream, byte[]> result = readSteam(stream, true);
byte[] buffer = result.getValue();
if (buffer.length < 2)
return new LabelValuePair<>(result.getLabel(), CharsetKit.CHARSET_GBK);
String encode = getFileCharSet(new BufferedInputStream(new ByteArrayInputStream(result.getValue())));// getBytesCharset(buffer);
return new LabelValuePair<>(result.getLabel(), CharsetKit.charset(encode));
}
/**
* 判断txt编码格式方法
*
* @param sourceFile
* @return
*/
public static String getFileCharSet(File sourceFile) {
String charset = "GBK";
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1) {
return charset; //文件编码为 ANSI
} else if (first3Bytes[0] == (byte) 0xFF
&& first3Bytes[1] == (byte) 0xFE) {
charset = "UTF-16LE"; //文件编码为 Unicode
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE
&& first3Bytes[1] == (byte) 0xFF) {
charset = "UTF-16BE"; //文件编码为 Unicode big endian
checked = true;
} else if (first3Bytes[0] == (byte) 0xEF
&& first3Bytes[1] == (byte) 0xBB
&& first3Bytes[2] == (byte) 0xBF) {
charset = "UTF-8"; //文件编码为 UTF-8
checked = true;
}
bis.reset();
if (!checked) {
int loc = 0;
while ((read = bis.read()) != -1) {
loc++;
if (read >= 0xF0)
break;
if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK
break;
if (0xC0 <= read && read <= 0xDF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)
// (0x80
// - 0xBF),也可能在GB编码内
continue;
else
break;
} else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
charset = "UTF-8";
break;
} else
break;
} else
break;
}
}
}
bis.close();
} catch (Exception e) {
log.error("获取文件编码方式异常", e);
}
return charset;
}
/**
* 判断txt编码格式方法
*
* @param bis
* @return
*/
public static String getFileCharSet(BufferedInputStream bis) {
String charset = "GBK";
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1) {
return charset; //文件编码为 ANSI
} else if (first3Bytes[0] == (byte) 0xFF
&& first3Bytes[1] == (byte) 0xFE) {
charset = "UTF-16LE"; //文件编码为 Unicode
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE
&& first3Bytes[1] == (byte) 0xFF) {
charset = "UTF-16BE"; //文件编码为 Unicode big endian
checked = true;
} else if (first3Bytes[0] == (byte) 0xEF
&& first3Bytes[1] == (byte) 0xBB
&& first3Bytes[2] == (byte) 0xBF) {
charset = "UTF-8"; //文件编码为 UTF-8
checked = true;
}
bis.reset();
if (!checked) {
int loc = 0;
while ((read = bis.read()) != -1) {
loc++;
if (read >= 0xF0)
break;
if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK
break;
if (0xC0 <= read && read <= 0xDF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)
// (0x80
// - 0xBF),也可能在GB编码内
continue;
else
break;
} else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
charset = "UTF-8";
break;
} else
break;
} else
break;
}
}
}
bis.close();
} catch (Exception e) {
log.error("获取文件编码方式异常", e);
}
return charset;
}
/**
* 读取流
*
* @param inputStream 输入流
* @param isRepeat 是否重复读取
* @return
*/
public static LabelValuePair<InputStream, byte[]> readSteam(InputStream inputStream, boolean isRepeat) throws IOException {
byte[] fs = IOUtils.toByteArray(inputStream);
inputStream.close();
return new LabelValuePair<>(new ByteArrayInputStream(fs), fs);
}
public static List<InputStream> copySteam(InputStream inputStream, int num) throws IOException {
List<InputStream> list = new ArrayList<>();
byte[] fs = IOUtils.toByteArray(inputStream);
for (int i = 0; i < num; i++) {
list.add(new ByteArrayInputStream(fs));
}
return list;
}
/**
* 通过地址获取base64位图片数据
* 包含 data:image/png;base64
*
* @param url
* @return
*/
public static String getImageBase64FromServer(String url) {
String base64Str = "";
InputStream is = null;
try {
String imageType = FileTypeUtils.getFileType(url);
URL realUrl = new URL(url);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.connect();
is = connection.getInputStream();
ByteArrayOutputStream result = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
result.write(buffer);
}
byte[] imageData = result.toByteArray();
result.close();
if (StringUtils.isEmpty(imageType)) imageType = FileTypeUtils.getFileExtendName(imageData);
if (StringUtils.isEmpty(imageType)) imageType = "jpg";
base64Str = StringUtils.format("data:image/{};base64,{}", imageType.toLowerCase(), com.xxxxx.common.core.utils.sign.Base64.encode(imageData));
} catch (ConnectException e) {
log.error("调用UtilTools.getImageBase64FromServer ConnectException, url=" + url, e);
} catch (SocketTimeoutException e) {
log.error("调用UtilTools.getImageBase64FromServer SocketTimeoutException, url=" + url, e);
} catch (IOException e) {
log.error("调用UtilTools.getImageBase64FromServer IOException, url=" + url, e);
} catch (Exception e) {
log.error("调用UtilTools.getImageBase64FromServer Exception, url=" + url, e);
} finally {
try {
if (is != null) {
is.close();
}
} catch (Exception ex) {
log.error("调用is.close Exception, url=" + url, ex);
}
}
return base64Str;
}
public static byte[] getImageDataFromServer(String url) {
InputStream is = null;
try {
URL realUrl = new URL(url);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.connect();
is = connection.getInputStream();
ByteArrayOutputStream result = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
result.write(buffer);
}
byte[] imageData = result.toByteArray();
result.close();
return imageData;
} catch (ConnectException e) {
log.error("调用UtilTools.getImageBase64FromServer ConnectException, url=" + url, e);
} catch (SocketTimeoutException e) {
log.error("调用UtilTools.getImageBase64FromServer SocketTimeoutException, url=" + url, e);
} catch (IOException e) {
log.error("调用UtilTools.getImageBase64FromServer IOException, url=" + url, e);
} catch (Exception e) {
log.error("调用UtilTools.getImageBase64FromServer Exception, url=" + url, e);
} finally {
try {
if (is != null) {
is.close();
}
} catch (Exception ex) {
log.error("调用is.close Exception, url=" + url, ex);
}
}
return null;
}
/**
* 每厘米567缇
*/
private static final double twipPerCM = 567;
/**
* 每厘米28.35磅
*/
private static final double poundPerCm = 28.35;
/**
* 每英寸2.54厘米
*/
private static final double cmPerInch = 2.54;
/**
* 每英寸多少缇
*/
private static final double twipPerInch = 1440;
/**
* 每英寸多少像素
*/
private static final double pixelPerInch = 96;
/**
* 一英尺72磅
*/
private static final double poundPerInch = 72;
/**
* 根据磅,获取缇
*
* @return
*/
public static BigInteger getTwipFromPound(double pound) {
Double d = pound * twipPerCM / poundPerCm;
return BigInteger.valueOf(d.longValue());
}
/**
* 通过像素获取缇
*
* @param prixel
* @return
*/
public static BigInteger getTwipFromPixel(double prixel) {
Double d = prixel * twipPerInch / pixelPerInch;
return BigInteger.valueOf(d.longValue());
}
/**
* 根据磅获取像素
*
* @param pound
* @return
*/
public static BigInteger getPixelFromPond(double pound) {
Double d = pound * pixelPerInch / poundPerInch;
return BigInteger.valueOf(d.longValue());
}
/**
* 根据厘米,获取缇
*
* @param cm
* @return
*/
public static BigInteger getTwipFromCM(double cm) {
Double d = cm * twipPerCM;
return BigInteger.valueOf(d.longValue());
}
/**
* 获取resource目录下文件内容
*
* @param fileName
* @return
* @throws IOException
*/
public static String getResourceFileContent(String fileName) throws IOException {
String content = "";
URL url = Class.class.getClassLoader().getResource(fileName);
if (url != null) {
return getFileContent(url.getPath());
}
return content;
}
/**
* 获取文件内容
*
* @param filePath
* @return
* @throws IOException
*/
public static String getFileContent(String filePath) throws IOException {
if (StringUtils.isEmpty(filePath)) return "";
StringBuffer content = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filePath));
String line;
while ((line = br.readLine()) != null) {
content.append(line);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (br != null) IOUtils.close(br);
}
return content.toString();
}
/**
* @param content
* @param filePath
* @throws IOException
*/
public static void saveResourceFileContent(String content, String filePath) throws IOException {
if (StringUtils.isEmpty(filePath)) return;
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(filePath);
fileWriter.write(content);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (fileWriter != null) IOUtils.close(fileWriter);
}
}
/**
* 将阿拉伯数字转换为中文
*
* @param num
* @return
*/
public static String formatNumberCN(int num) {
String[] numList = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
String[] unitList = new String[]{"十", "百", "千", "万"};
String[] numStr = (num + "").split(""); //1234
if (numStr.length > 5) return (num + "");
String rStr = "";
for (int i = 0; i < numStr.length; i++) {
int n = Integer.valueOf(numStr[i]);
String n_c = numList[n];
int u_i = numStr.length - 2 - i;
String unit = u_i > -1 && n > 0 ? unitList[u_i] : "";
rStr += (n_c + unit);
}
return rStr;
}
/**
* 判断字符是否全角
*
* @param c
* @return
*/
public static boolean isFullWidth(char c) {
int code = (int) c;
return (code >= 0xFF01 && code <= 0xFF5E) || (code >= 0x3000 && code <= 0x303F);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南