Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
邬吉朋
practical_train
Commits
0562fc83
"src/main/git@gitlab.ruzhoukj.com:wujipeng/relation.git" did not exist on "bcbb30bdeab75fecb16c03a5a233ffbc5c410029"
Commit
0562fc83
authored
Oct 28, 2026
by
wujipeng
Browse files
优化
parent
2f15900c
Changes
6
Expand all
Show whitespace changes
Inline
Side-by-side
docs/教师业务档案导出接口文档.md
View file @
0562fc83
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
档案管理中的教师管理支持导出 J2 教师业务档案:
档案管理中的教师管理支持导出 J2 教师业务档案:
-
勾选一名教师时,直接下载一份 Word
97-2003
`.doc`
文件。
-
勾选一名教师时,直接下载一份
可由
Word
/WPS 打开的
`.doc`
文件。
-
勾选多名教师时,下载 ZIP 压缩包,每名教师对应一份
`.doc`
文件。
-
勾选多名教师时,下载 ZIP 压缩包,每名教师对应一份
`.doc`
文件。
-
本文接口路径均为相对业务服务路径,鉴权 Token 沿用项目现有请求拦截器。
-
本文接口路径均为相对业务服务路径,鉴权 Token 沿用项目现有请求拦截器。
...
@@ -35,7 +35,7 @@ Content-Type: application/msword
...
@@ -35,7 +35,7 @@ Content-Type: application/msword
Content-Disposition: attachment; filename*=UTF-8''%E5%BC%A0%E4%B8%89_J2%E6%95%99%E5%B8%88%E4%B8%9A%E5%8A%A1%E6%A1%A3%E6%A1%88.doc
Content-Disposition: attachment; filename*=UTF-8''%E5%BC%A0%E4%B8%89_J2%E6%95%99%E5%B8%88%E4%B8%9A%E5%8A%A1%E6%A1%A3%E6%A1%88.doc
```
```
响应体为
Word 97-2003
`.doc`
文件
的二进制
数据。
响应体为
保留 J2 原模板版式、可由 Word/WPS 直接打开的
`.doc`
文件数据。
文件名格式:
文件名格式:
...
...
practical-business/src/main/java/com/platform/business/service/impl/StudentCertificationAdminServiceImpl.java
View file @
0562fc83
...
@@ -9,17 +9,11 @@ import com.platform.business.model.vo.StudentCertificationScopeVO;
...
@@ -9,17 +9,11 @@ import com.platform.business.model.vo.StudentCertificationScopeVO;
import
com.platform.business.model.vo.StudentCertificationTreeVO
;
import
com.platform.business.model.vo.StudentCertificationTreeVO
;
import
com.platform.business.service.StudentCertificationAdminService
;
import
com.platform.business.service.StudentCertificationAdminService
;
import
com.platform.exception.BusinessException
;
import
com.platform.exception.BusinessException
;
import
org.apache.poi.hwpf.HWPFDocument
;
import
org.apache.poi.hwpf.usermodel.Table
;
import
org.apache.poi.hwpf.usermodel.TableCell
;
import
org.apache.poi.hwpf.usermodel.TableIterator
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.ByteArrayOutputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.InputStream
;
import
java.net.URLEncoder
;
import
java.net.URLEncoder
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.charset.StandardCharsets
;
import
java.text.SimpleDateFormat
;
import
java.text.SimpleDateFormat
;
...
@@ -37,12 +31,12 @@ import java.util.zip.ZipOutputStream;
...
@@ -37,12 +31,12 @@ import java.util.zip.ZipOutputStream;
/**
/**
* 管理端学员认证实现。
* 管理端学员认证实现。
*
*
* <p>B2文件始终在原始Word 97-2003模板副本上写入,既保留页面尺寸、合并单元格和
* <p>B2文件使用Word/WPS均原生支持的RTF格式生成,并继续沿用历史接口的 {@code .doc}
* 打印版式,也避免多个请求共享并修改同一个模板对象。</p>
* 扩展名。原Word 97-2003模板包含POI无法完整保留的OfficeArt记录,使用HWPF重写后虽然
* 文件容器仍然存在,但正文会在WPS中显示为空;直接生成RTF可以从根本上避免该兼容问题。</p>
*/
*/
@Service
@Service
public
class
StudentCertificationAdminServiceImpl
implements
StudentCertificationAdminService
{
public
class
StudentCertificationAdminServiceImpl
implements
StudentCertificationAdminService
{
private
static
final
String
TEMPLATE
=
"templates/certification/B2-student-training-assessment-registration.doc"
;
private
static
final
int
MAX_EXPORT
=
500
;
private
static
final
int
MAX_EXPORT
=
500
;
@Resource
private
StudentCertificationMapper
mapper
;
@Resource
private
StudentCertificationMapper
mapper
;
...
@@ -130,68 +124,128 @@ public class StudentCertificationAdminServiceImpl implements StudentCertificatio
...
@@ -130,68 +124,128 @@ public class StudentCertificationAdminServiceImpl implements StudentCertificatio
}
}
}
}
/** 根据已确认的模板行列坐标填值;模板升级时测试会校验行数,防止静默错位。 */
/**
* 生成可由Microsoft Word和WPS直接打开的RTF报名表。
*
* <p>这里不再通过HWPF修改旧版二进制DOC模板。该模板含有损坏或非标准的OfficeArt、
* 段落格式记录,POI写出时会静默丢弃这些记录,最终造成下载文件正文为空。RTF是文本型
* Word格式,不依赖OfficeArt,且能够稳定表达中文、表格、边框和勾选项。</p>
*/
byte
[]
createDocument
(
StudentCertificationProfileVO
p
)
{
byte
[]
createDocument
(
StudentCertificationProfileVO
p
)
{
try
(
InputStream
input
=
new
ClassPathResource
(
TEMPLATE
).
getInputStream
();
StringBuilder
rtf
=
new
StringBuilder
(
8192
);
HWPFDocument
document
=
new
HWPFDocument
(
input
);
// A4纵向页面,正文使用宋体;RTF统一转义为ASCII,避免服务器默认编码影响中文。
ByteArrayOutputStream
output
=
new
ByteArrayOutputStream
())
{
rtf
.
append
(
"{\\rtf1\\ansi\\ansicpg936\\deff0"
)
TableIterator
iterator
=
new
TableIterator
(
document
.
getRange
());
.
append
(
"{\\fonttbl{\\f0\\fnil\\fcharset134 SimSun;}}"
)
if
(!
iterator
.
hasNext
())
throw
new
BusinessException
(
"B2模板中未找到报名表表格"
);
.
append
(
"\\paperw11906\\paperh16838\\margl720\\margr720\\margt720\\margb720"
)
Table
table
=
iterator
.
next
();
.
append
(
"\\viewkind4\\uc1\\f0\\fs20 "
);
if
(
table
.
numRows
()
<
33
)
throw
new
BusinessException
(
"B2模板结构已变化,请重新确认字段映射"
);
appendParagraph
(
rtf
,
"B2 学员培训鉴定报名表"
,
true
,
32
);
// HWPF的Range不会在前方插入后自动重算全部旧引用,因此必须严格从后向前填充。
appendFieldRow
(
rtf
,
"填表日期"
,
formatDate
(
p
.
getSubmitTime
()),
"班级编号"
,
string
(
p
.
getClassId
()));
markLevel
(
table
,
21
,
p
.
getAssessmentLevel
());
put
(
table
,
19
,
2
,
p
.
getAssessmentOccupation
());
markBoolean
(
table
,
18
,
p
.
getSubsidyFlag
(),
7
,
8
);
markTrainingCategory
(
table
,
18
,
p
.
getTrainingCategory
());
appendSection
(
rtf
,
"基本信息"
);
markLevel
(
table
,
16
,
p
.
getTrainingLevel
());
put
(
table
,
14
,
2
,
p
.
getTrainingOccupation
());
appendFieldRow
(
rtf
,
"姓名"
,
p
.
getName
(),
"性别"
,
p
.
getGender
());
put
(
table
,
12
,
6
,
p
.
getJobYears
());
put
(
table
,
12
,
4
,
p
.
getPreviousIssueDept
());
put
(
table
,
12
,
2
,
p
.
getPreviousIssueDate
());
appendFieldRow
(
rtf
,
"出生日期"
,
p
.
getBirthday
(),
"户口性质"
,
p
.
getHouseholdType
());
put
(
table
,
11
,
6
,
p
.
getPreviousCertNo
());
put
(
table
,
11
,
4
,
p
.
getPreviousLevel
());
put
(
table
,
11
,
2
,
p
.
getPreviousOccupation
());
appendFieldRow
(
rtf
,
"证件类型"
,
p
.
getIdType
(),
"证件号码"
,
p
.
getIdCard
());
put
(
table
,
10
,
4
,
p
.
getSocialSecurityYears
());
put
(
table
,
10
,
2
,
p
.
getWorkUnitAddress
());
appendFieldRow
(
rtf
,
"最高学历"
,
p
.
getHighestEducation
(),
"所学专业"
,
p
.
getMajor
());
put
(
table
,
9
,
4
,
p
.
getUnitPhone
());
put
(
table
,
9
,
2
,
p
.
getWorkUnit
());
appendFieldRow
(
rtf
,
"行政区"
,
p
.
getAdministrativeDistrict
(),
"人员类别"
,
personTypes
(
p
));
put
(
table
,
8
,
4
,
p
.
getPhoneNumber
());
put
(
table
,
8
,
2
,
p
.
getLaborBookNo
());
appendFieldRow
(
rtf
,
"联系地址"
,
p
.
getHomeAddress
(),
"邮编"
,
p
.
getZipCode
());
put
(
table
,
7
,
4
,
p
.
getZipCode
());
put
(
table
,
7
,
2
,
p
.
getHomeAddress
());
appendFieldRow
(
rtf
,
"劳动手册编号"
,
p
.
getLaborBookNo
(),
"联系电话"
,
p
.
getPhoneNumber
());
markMany
(
table
,
6
,
p
.
getPersonTypes
(),
new
int
[]{
4
,
5
,
6
,
7
,
8
});
put
(
table
,
5
,
2
,
p
.
getHighestEducation
());
put
(
table
,
4
,
2
,
p
.
getIdCard
());
appendSection
(
rtf
,
"工作信息"
);
mark
(
table
,
2
,
p
.
getHouseholdType
(),
"本市城镇"
,
4
,
"本市农村"
,
5
,
"外省市"
,
6
);
appendFieldRow
(
rtf
,
"工作单位"
,
p
.
getWorkUnit
(),
"单位联系电话"
,
p
.
getUnitPhone
());
mark
(
table
,
1
,
p
.
getGender
(),
"男"
,
6
,
"女"
,
8
);
put
(
table
,
1
,
4
,
p
.
getBirthday
());
put
(
table
,
1
,
2
,
p
.
getName
());
appendFieldRow
(
rtf
,
"单位地址"
,
p
.
getWorkUnitAddress
(),
"社保缴费年限"
,
years
(
p
.
getSocialSecurityYears
()));
put
(
table
,
0
,
14
,
string
(
p
.
getClassId
()));
put
(
table
,
0
,
1
,
formatDate
(
p
.
getSubmitTime
()));
appendFieldRow
(
rtf
,
"本工种工作年限"
,
years
(
p
.
getCurrentWorkYears
()),
"相关职业年限"
,
years
(
p
.
getJobYears
()));
// 写出前用HWPF内存模型核验关键字段,避免模板变更后生成内容为空。
String
filledText
=
document
.
getText
().
toString
();
appendSection
(
rtf
,
"原职业与教育背景"
);
if
(!
filledText
.
contains
(
string
(
p
.
getName
()))
||
!
filledText
.
contains
(
string
(
p
.
getIdCard
())))
{
appendFieldRow
(
rtf
,
"原职业(工种)"
,
p
.
getPreviousOccupation
(),
"原职业等级"
,
p
.
getPreviousLevel
());
throw
new
BusinessException
(
"B2模板字段填充校验失败"
);
appendFieldRow
(
rtf
,
"原职业证书编号"
,
p
.
getPreviousCertNo
(),
"上一等级证书编号"
,
p
.
getPreviousLevelCertNo
());
}
appendFieldRow
(
rtf
,
"发证日期"
,
p
.
getPreviousIssueDate
(),
"发证部门"
,
p
.
getPreviousIssueDept
());
document
.
write
(
output
);
return
output
.
toByteArray
();
appendFieldRow
(
rtf
,
"毕业院校"
,
p
.
getGraduationSchool
(),
"毕业日期"
,
p
.
getGraduationDate
());
}
catch
(
BusinessException
e
)
{
appendFieldRow
(
rtf
,
"毕业证书编号"
,
p
.
getGraduationCertNo
(),
""
,
""
);
throw
e
;
}
catch
(
Exception
e
)
{
appendSection
(
rtf
,
"培训信息"
);
throw
new
BusinessException
(
"生成B2报名表失败"
);
appendFieldRow
(
rtf
,
"培训职业(工种)"
,
p
.
getTrainingOccupation
(),
"培训等级"
,
p
.
getTrainingLevel
());
appendFieldRow
(
rtf
,
"培训类别"
,
options
(
p
.
getTrainingCategory
(),
"政府补贴培训"
,
"企业培训"
,
"院校培训"
),
"申请培训补贴"
,
Boolean
.
TRUE
.
equals
(
p
.
getSubsidyFlag
())
?
"是"
:
"否"
);
appendSection
(
rtf
,
"鉴定信息"
);
appendFieldRow
(
rtf
,
"鉴定职业(工种)"
,
p
.
getAssessmentOccupation
(),
"鉴定等级"
,
p
.
getAssessmentLevel
());
appendParagraph
(
rtf
,
"本人承诺以上信息及提交材料真实、准确,并遵守职业技能鉴定相关规定。"
,
false
,
20
);
appendParagraph
(
rtf
,
"申请人(签字):________________ 日期:____年__月__日"
,
false
,
20
);
rtf
.
append
(
'}'
);
return
rtf
.
toString
().
getBytes
(
StandardCharsets
.
US_ASCII
);
}
/** 写入普通段落;所有动态内容统一经过RTF转义,防止花括号或反斜杠破坏文档结构。 */
private
void
appendParagraph
(
StringBuilder
rtf
,
String
text
,
boolean
centered
,
int
fontSize
)
{
rtf
.
append
(
"\\pard"
).
append
(
centered
?
"\\qc"
:
"\\ql"
)
.
append
(
"\\sa120\\fs"
).
append
(
fontSize
).
append
(
' '
)
.
append
(
rtfText
(
text
)).
append
(
"\\par "
);
}
}
/** 分区标题单独成行,便于打印时快速区分报名表的资料模块。 */
private
void
appendSection
(
StringBuilder
rtf
,
String
title
)
{
rtf
.
append
(
"\\pard\\sb100\\sa50\\b\\fs22 "
).
append
(
rtfText
(
title
)).
append
(
"\\b0\\par "
);
}
}
private
void
put
(
Table
table
,
int
row
,
int
cell
,
Object
value
)
{
/**
String
text
=
string
(
value
);
if
(
text
.
isEmpty
())
return
;
* 输出一行两组“标签-内容”单元格。固定列宽与完整边框保证Word、WPS渲染一致,
TableCell
target
=
table
.
getRow
(
row
).
getCell
(
cell
);
* 同时允许较长地址在内容单元格内自动换行。
target
.
insertBefore
(
text
);
*/
private
void
appendFieldRow
(
StringBuilder
rtf
,
String
label1
,
Object
value1
,
String
label2
,
Object
value2
)
{
rtf
.
append
(
"\\trowd\\trgaph60\\trleft0\\trrh420"
)
.
append
(
cellDefinition
(
1600
)).
append
(
cellDefinition
(
5000
))
.
append
(
cellDefinition
(
6600
)).
append
(
cellDefinition
(
10000
));
appendCell
(
rtf
,
label1
,
true
);
appendCell
(
rtf
,
string
(
value1
),
false
);
appendCell
(
rtf
,
label2
,
true
);
appendCell
(
rtf
,
string
(
value2
),
false
);
rtf
.
append
(
"\\row "
);
}
}
private
void
mark
(
Table
table
,
int
row
,
String
actual
,
String
first
,
int
firstCell
,
String
second
,
int
secondCell
)
{
if
(
first
.
equals
(
actual
))
put
(
table
,
row
,
firstCell
,
"√"
);
else
if
(
second
.
equals
(
actual
))
put
(
table
,
row
,
secondCell
,
"√"
);
private
String
cellDefinition
(
int
right
)
{
return
"\\clvertalc\\clbrdrt\\brdrs\\brdrw10\\clbrdrl\\brdrs\\brdrw10"
+
"\\clbrdrb\\brdrs\\brdrw10\\clbrdrr\\brdrs\\brdrw10\\cellx"
+
right
;
}
}
private
void
mark
(
Table
table
,
int
row
,
String
actual
,
String
a
,
int
ac
,
String
b
,
int
bc
,
String
c
,
int
cc
)
{
if
(
a
.
equals
(
actual
))
put
(
table
,
row
,
ac
,
"√"
);
else
if
(
b
.
equals
(
actual
))
put
(
table
,
row
,
bc
,
"√"
);
else
if
(
c
.
equals
(
actual
))
put
(
table
,
row
,
cc
,
"√"
);
private
void
appendCell
(
StringBuilder
rtf
,
String
text
,
boolean
label
)
{
rtf
.
append
(
"\\pard\\intbl\\vertalc\\ql\\fs20 "
);
if
(
label
)
rtf
.
append
(
"\\b "
);
rtf
.
append
(
rtfText
(
text
));
if
(
label
)
rtf
.
append
(
"\\b0 "
);
rtf
.
append
(
"\\cell "
);
}
}
private
void
markMany
(
Table
table
,
int
row
,
List
<
String
>
selected
,
int
[]
cells
)
{
if
(
selected
==
null
)
return
;
/** RTF的Unicode控制字使用有符号16位值;ASCII控制字符则按语法逐一转义。 */
String
[]
names
=
{
"失业"
,
"协保"
,
"农村富余劳动力"
,
"在职"
,
"其他"
};
private
String
rtfText
(
String
value
)
{
for
(
int
i
=
names
.
length
-
1
;
i
>=
0
;
i
--)
if
(
selected
.
contains
(
names
[
i
]))
put
(
table
,
row
,
cells
[
i
],
"√"
);
String
text
=
string
(
value
);
StringBuilder
escaped
=
new
StringBuilder
(
text
.
length
()
*
2
);
for
(
int
i
=
0
;
i
<
text
.
length
();
i
++)
{
char
ch
=
text
.
charAt
(
i
);
if
(
ch
==
'\\'
||
ch
==
'{'
||
ch
==
'}'
)
escaped
.
append
(
'\\'
).
append
(
ch
);
else
if
(
ch
==
'\r'
)
{
/* CR由紧随其后的LF统一处理,避免生成两个换行。 */
}
else
if
(
ch
==
'\n'
)
escaped
.
append
(
"\\line "
);
else
if
(
ch
<
128
)
escaped
.
append
(
ch
);
else
escaped
.
append
(
"\\u"
).
append
((
short
)
ch
).
append
(
'?'
);
}
}
private
void
markLevel
(
Table
table
,
int
row
,
String
level
)
{
return
escaped
.
toString
();
String
[]
names
={
"五级"
,
"四级"
,
"三级"
,
"二级"
,
"一级"
,
"专项职业能力"
};
for
(
int
i
=
0
;
i
<
names
.
length
;
i
++)
if
(
names
[
i
].
equals
(
level
))
put
(
table
,
row
,
i
+
2
,
"√"
);
}
}
private
void
markTrainingCategory
(
Table
table
,
int
row
,
String
category
)
{
String
[]
names
={
"政府补贴培训"
,
"企业培训"
,
"院校培训"
};
private
String
personTypes
(
StudentCertificationProfileVO
p
)
{
for
(
int
i
=
0
;
i
<
names
.
length
;
i
++)
if
(
names
[
i
].
equals
(
category
))
put
(
table
,
row
,
i
+
2
,
"√"
);
if
(
p
.
getPersonTypes
()
==
null
||
p
.
getPersonTypes
().
isEmpty
())
return
""
;
List
<
String
>
values
=
new
ArrayList
<>(
p
.
getPersonTypes
());
if
(
values
.
contains
(
"其他"
)
&&
trim
(
p
.
getPersonTypeOther
())
!=
null
)
{
values
.
add
(
"其他说明:"
+
p
.
getPersonTypeOther
().
trim
());
}
return
String
.
join
(
"、"
,
values
);
}
}
private
void
markBoolean
(
Table
table
,
int
row
,
Boolean
flag
,
int
yesCell
,
int
noCell
)
{
put
(
table
,
row
,
Boolean
.
TRUE
.
equals
(
flag
)?
yesCell:
noCell
,
"√"
);
}
private
String
options
(
String
actual
,
String
...
values
)
{
List
<
String
>
marked
=
new
ArrayList
<>();
for
(
String
value
:
values
)
marked
.
add
(
value
.
equals
(
actual
)
?
"[√]"
+
value
:
"[ ]"
+
value
);
return
String
.
join
(
" "
,
marked
);
}
private
String
years
(
Integer
value
)
{
return
value
==
null
?
""
:
value
+
"年"
;
}
private
void
fillPersonTypes
(
StudentCertificationProfileVO
p
)
{
private
void
fillPersonTypes
(
StudentCertificationProfileVO
p
)
{
if
(
p
.
getPersonTypes
()
!=
null
)
return
;
if
(
p
.
getPersonTypes
()
!=
null
)
return
;
...
...
practical-business/src/main/java/com/platform/business/service/impl/TeacherArchiveExportService.java
View file @
0562fc83
This diff is collapsed.
Click to expand it.
practical-business/src/main/resources/templates/teacher/J2-teacher-business-archive.rtf
0 → 100644
View file @
0562fc83
This diff is collapsed.
Click to expand it.
practical-business/src/test/java/com/platform/business/service/impl/B2TemplateInspectionTest.java
View file @
0562fc83
package
com.platform.business.service.impl
;
package
com.platform.business.service.impl
;
import
org.apache.poi.hwpf.HWPFDocument
;
import
org.apache.poi.poifs.filesystem.POIFSFileSystem
;
import
com.platform.business.model.vo.StudentCertificationProfileVO
;
import
com.platform.business.model.vo.StudentCertificationProfileVO
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
import
javax.swing.text.DefaultStyledDocument
;
import
javax.swing.text.rtf.RTFEditorKit
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayInputStream
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Date
;
import
java.util.Date
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertFalse
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
/**
使用HWPF重新读取导出文件,验证关键字段和勾选标记确已写入原模板
。 */
/**
直接验证RTF正文,防止测试只检查文件外壳而漏掉Word/WPS打开后内容为空的问题
。 */
class
B2TemplateInspectionTest
{
class
B2TemplateInspectionTest
{
@Test
@Test
void
generatedDocumentShould
RemainReadableAnd
ContainProfileValues
()
throws
Exception
{
void
generatedDocumentShouldContainProfileValues
AndWordTableStructure
()
throws
Exception
{
StudentCertificationProfileVO
profile
=
new
StudentCertificationProfileVO
();
StudentCertificationProfileVO
profile
=
new
StudentCertificationProfileVO
();
profile
.
setName
(
"张三"
);
profile
.
setGender
(
"男"
);
profile
.
setBirthday
(
"1995-06-01"
);
profile
.
setName
(
"张三"
);
profile
.
setGender
(
"男"
);
profile
.
setBirthday
(
"1995-06-01"
);
profile
.
setHouseholdType
(
"本市城镇"
);
profile
.
setIdCard
(
"310101199506011234"
);
profile
.
setHouseholdType
(
"本市城镇"
);
profile
.
setIdCard
(
"310101199506011234"
);
...
@@ -33,12 +34,36 @@ class B2TemplateInspectionTest {
...
@@ -33,12 +34,36 @@ class B2TemplateInspectionTest {
profile
.
setClassId
(
12
);
profile
.
setSubmitTime
(
new
Date
());
profile
.
setClassId
(
12
);
profile
.
setSubmitTime
(
new
Date
());
byte
[]
bytes
=
new
StudentCertificationAdminServiceImpl
().
createDocument
(
profile
);
byte
[]
bytes
=
new
StudentCertificationAdminServiceImpl
().
createDocument
(
profile
);
// 用户提供的旧模板带有HWPF无法二次解析的OfficeArt记录,原文件空写也会触发该限制。
String
rtf
=
new
String
(
bytes
,
StandardCharsets
.
US_ASCII
);
// 因此这里使用POIFS验证输出仍是完整OLE复合文档,并验证关键Word数据流未丢失。
try
(
POIFSFileSystem
fileSystem
=
new
POIFSFileSystem
(
new
ByteArrayInputStream
(
bytes
)))
{
// RTF内容全部以ASCII控制字表达,测试可稳定检查中文对应的Unicode转义值。
assertTrue
(
fileSystem
.
getRoot
().
hasEntry
(
"WordDocument"
));
assertTrue
(
rtf
.
startsWith
(
"{\\rtf1"
));
assertTrue
(
fileSystem
.
getRoot
().
hasEntry
(
"1Table"
));
assertTrue
(
rtf
.
contains
(
"\\trowd"
));
assertTrue
(
bytes
.
length
>
50000
);
assertTrue
(
rtf
.
contains
(
toRtfUnicode
(
"张三"
)));
assertTrue
(
rtf
.
contains
(
"310101199506011234"
));
assertTrue
(
rtf
.
contains
(
"13800138000"
));
assertTrue
(
rtf
.
contains
(
toRtfUnicode
(
"企业培训"
)));
assertTrue
(
rtf
.
contains
(
toRtfUnicode
(
"污水处理工"
)));
assertTrue
(
rtf
.
contains
(
toRtfUnicode
(
"[√]企业培训"
)));
assertFalse
(
rtf
.
contains
(
"WordDocument"
));
// 使用JDK自带RTF解析器执行一次真实回读,证明输出不是“控制字存在但文档不可解析”。
DefaultStyledDocument
parsedDocument
=
new
DefaultStyledDocument
();
new
RTFEditorKit
().
read
(
new
ByteArrayInputStream
(
bytes
),
parsedDocument
,
0
);
String
visibleText
=
parsedDocument
.
getText
(
0
,
parsedDocument
.
getLength
());
assertTrue
(
visibleText
.
contains
(
"张三"
));
assertTrue
(
visibleText
.
contains
(
"310101199506011234"
));
assertTrue
(
visibleText
.
contains
(
"企业培训"
));
assertTrue
(
visibleText
.
contains
(
"污水处理工"
));
}
/** 与生产代码相同地把中文转换为RTF Unicode控制字,避免测试依赖本机字符集。 */
private
String
toRtfUnicode
(
String
value
)
{
StringBuilder
escaped
=
new
StringBuilder
();
for
(
char
ch
:
value
.
toCharArray
())
{
if
(
ch
<
128
)
escaped
.
append
(
ch
);
else
escaped
.
append
(
"\\u"
).
append
((
short
)
ch
).
append
(
'?'
);
}
}
return
escaped
.
toString
();
}
}
}
}
practical-business/src/test/java/com/platform/business/service/impl/TeacherArchiveExportServiceTest.java
View file @
0562fc83
...
@@ -16,6 +16,8 @@ import org.junit.jupiter.api.Test;
...
@@ -16,6 +16,8 @@ import org.junit.jupiter.api.Test;
import
org.springframework.mock.web.MockHttpServletResponse
;
import
org.springframework.mock.web.MockHttpServletResponse
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
javax.swing.text.Document
;
import
javax.swing.text.rtf.RTFEditorKit
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayInputStream
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Arrays
;
...
@@ -25,6 +27,7 @@ import java.util.zip.ZipEntry;
...
@@ -25,6 +27,7 @@ import java.util.zip.ZipEntry;
import
java.util.zip.ZipInputStream
;
import
java.util.zip.ZipInputStream
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertFalse
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertThrows
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertThrows
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
...
@@ -58,7 +61,7 @@ class TeacherArchiveExportServiceTest {
...
@@ -58,7 +61,7 @@ class TeacherArchiveExportServiceTest {
}
}
@Test
@Test
void
documentShouldFillBasicInformationAndExperiencesInOriginalTemplate
()
{
void
documentShouldFillBasicInformationAndExperiencesInOriginalTemplate
()
throws
Exception
{
UserVO
teacher
=
profile
(
"1"
,
"张三"
,
"310101199001011234"
);
UserVO
teacher
=
profile
(
"1"
,
"张三"
,
"310101199001011234"
);
teacher
.
setSex
(
"男"
);
teacher
.
setSex
(
"男"
);
teacher
.
setBirthday
(
"1990年1月"
);
teacher
.
setBirthday
(
"1990年1月"
);
...
@@ -93,12 +96,21 @@ class TeacherArchiveExportServiceTest {
...
@@ -93,12 +96,21 @@ class TeacherArchiveExportServiceTest {
byte
[]
bytes
=
service
.
createDocument
(
teacher
);
byte
[]
bytes
=
service
.
createDocument
(
teacher
);
assertTrue
(
bytes
.
length
>
0
);
assertTrue
(
bytes
.
length
>
0
);
// 原模板带有HWPF无法二次解析的OfficeArt记录,原文件空写后重开也会触发该限制。
assertEquals
(
"{\\rtf1"
,
new
String
(
bytes
,
0
,
6
,
java
.
nio
.
charset
.
StandardCharsets
.
US_ASCII
));
// 服务已在document.write前逐项核验内存表格;测试再确认输出仍是标准OLE复合文档文件头。
assertEquals
((
byte
)
0xD0
,
bytes
[
0
]);
// 用JDK自带RTF解析器重新打开输出文件,既验证文件没有损坏,也验证实际可见文字,
assertEquals
((
byte
)
0xCF
,
bytes
[
1
]);
// 避免只在原始字符串中找到控制字却未真正显示到Word/WPS页面上。
assertEquals
((
byte
)
0x11
,
bytes
[
2
]);
RTFEditorKit
editorKit
=
new
RTFEditorKit
();
assertEquals
((
byte
)
0xE0
,
bytes
[
3
]);
Document
document
=
editorKit
.
createDefaultDocument
();
editorKit
.
read
(
new
ByteArrayInputStream
(
bytes
),
document
,
0
);
String
visibleText
=
document
.
getText
(
0
,
document
.
getLength
());
assertTrue
(
visibleText
.
contains
(
"张三"
));
assertTrue
(
visibleText
.
contains
(
"环境工程"
));
assertTrue
(
visibleText
.
contains
(
"示例单位"
));
assertTrue
(
visibleText
.
contains
(
"污水处理"
));
assertTrue
(
visibleText
.
contains
(
"2021年1月-至今"
));
assertFalse
(
visibleText
.
contains
(
"孙建平"
));
assertFalse
(
visibleText
.
contains
(
"兼职"
));
}
}
@Test
@Test
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment