导出原理
docx是一个压缩包,我们新建一个docx文档,添加一些内容,后缀名改为.zip进行解压
其中word下面结构如下
其中document.xml主要就是文档内容。
打开document.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
<w:body>
<w:p w:rsidR="005E21F0" w:rsidRDefault="00EC00A2">
<w:r>
<w:t>Aaa</w:t>
</w:r>
<w:bookmarkStart w:id="0" w:name="_GoBack"/>
<w:bookmarkEnd w:id="0"/>
</w:p>
<w:sectPr w:rsidR="005E21F0">
<w:pgSz w:w="11906" w:h="16838"/>
<w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="851" w:footer="992"
w:gutter="0"/>
<w:cols w:space="425"/>
<w:docGrid w:type="lines" w:linePitch="312"/>
</w:sectPr>
</w:body>
</w:document>
可以看到刚才输入的内容Aaa
所以我们只需用freemarker生成一个叫document.xml的文档替换我们设计好的模板下的同名文件重新打包就可以导出指定格式的docx文档了。
需要注意打包时,选择的时解压出来新建文档 文件夹下所有内容,而不是外面的文件夹
下面从头开始演示如何生成
新建一个docx文档,并设计好需要的格式
修改后缀为zip并解压
把word下面的document.xml剪切出来
剩下的重新打成zip包
把zip包和document.xml重新命名为crossExport.zip 和crossExport.ftl
修改crossExport.ftl
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://purl.oclc.org/ooxml/officeDocument/relationships"
xmlns:m="http://purl.oclc.org/ooxml/officeDocument/math" xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
xmlns:wp="http://purl.oclc.org/ooxml/drawingml/wordprocessingDrawing"
xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://purl.oclc.org/ooxml/wordprocessingml/main"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" mc:Ignorable="w14 w15 wne wp14"
w:conformance="strict">
<w:body>
<w:p w:rsidR="00E35097" w:rsidRPr="00EF0F10" w:rsidRDefault="00EF0F10">
<w:pPr>
<w:adjustRightInd w:val="0"/>
<w:snapToGrid w:val="0"/>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
<w:rPr>
<w:rFonts w:ascii="仿宋" w:eastAsia="仿宋" w:hAnsi="仿宋"/>
<w:b/>
<w:bCs/>
<w:sz w:val="72"/>
<w:szCs w:val="48"/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00EF0F10">
<w:rPr>
<w:rFonts w:ascii="仿宋" w:eastAsia="仿宋" w:hAnsi="仿宋" w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
<w:sz w:val="56"/>
<w:szCs w:val="48"/>
</w:rPr>
<w:t>${projectName}交叉跨越校验报告</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00E35097" w:rsidRDefault="005A31A9">
<w:pPr>
<w:adjustRightInd w:val="0"/>
<w:snapToGrid w:val="0"/>
<w:ind w:firstLine="48.20pt"/>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:bCs/>
<w:sz w:val="48"/>
<w:szCs w:val="48"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
<w:sz w:val="48"/>
<w:szCs w:val="48"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
<w:sz w:val="48"/>
<w:szCs w:val="48"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
<w:sz w:val="48"/>
<w:szCs w:val="48"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
<w:sz w:val="48"/>
<w:szCs w:val="48"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
<w:sz w:val="48"/>
<w:szCs w:val="48"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
<w:sz w:val="48"/>
<w:szCs w:val="48"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
<w:sz w:val="48"/>
<w:szCs w:val="48"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
<w:sz w:val="48"/>
<w:szCs w:val="48"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
<w:sz w:val="48"/>
<w:szCs w:val="48"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
<w:sz w:val="48"/>
<w:szCs w:val="48"/>
</w:rPr>
<w:tab/>
</w:r>
</w:p>
<w:p w:rsidR="00E35097" w:rsidRDefault="00E35097">
<w:pPr>
<w:adjustRightInd w:val="0"/>
<w:snapToGrid w:val="0"/>
<w:ind w:firstLine="36pt"/>
<w:jc w:val="center"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:spacing w:val="20"/>
<w:sz w:val="32"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p w:rsidR="00E35097" w:rsidRDefault="00E35097">
<w:pPr>
<w:adjustRightInd w:val="0"/>
<w:snapToGrid w:val="0"/>
<w:ind w:firstLine="36pt"/>
<w:jc w:val="center"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:spacing w:val="20"/>
<w:sz w:val="32"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p w:rsidR="00E35097" w:rsidRDefault="00E35097">
<w:pPr>
<w:adjustRightInd w:val="0"/>
<w:snapToGrid w:val="0"/>
<w:ind w:firstLine="36pt"/>
<w:jc w:val="center"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:spacing w:val="20"/>
<w:sz w:val="32"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p w:rsidR="00E35097" w:rsidRDefault="00E35097">
<w:pPr>
<w:adjustRightInd w:val="0"/>
<w:snapToGrid w:val="0"/>
<w:ind w:firstLine="36pt"/>
<w:jc w:val="center"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:spacing w:val="20"/>
<w:sz w:val="32"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p w:rsidR="00E35097" w:rsidRDefault="00E35097">
<w:pPr>
<w:adjustRightInd w:val="0"/>
<w:snapToGrid w:val="0"/>
<w:ind w:firstLine="36pt"/>
<w:jc w:val="center"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:spacing w:val="20"/>
<w:sz w:val="32"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p w:rsidR="00E35097" w:rsidRDefault="00E35097" w:rsidP="005F6F99">
<w:pPr>
<w:spacing w:line="26pt" w:lineRule="exact"/>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p w:rsidR="00E35097" w:rsidRDefault="007071AD">
<w:pPr>
<w:adjustRightInd w:val="0"/>
<w:snapToGrid w:val="0"/>
<w:ind w:firstLineChars="0"/>
<w:jc w:val="center"/>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>${year}年${month}月${day}日</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00E35097" w:rsidRDefault="006C13D7">
<w:pPr>
<w:adjustRightInd w:val="0"/>
<w:snapToGrid w:val="0"/>
<w:ind w:start="210pt" w:firstLineChars="0" w:firstLine="21pt"/>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t xml:space="preserve"> </w:t>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
</w:p>
<w:p w:rsidR="00EF0F10" w:rsidRDefault="006C13D7">
<w:pPr>
<w:adjustRightInd w:val="0"/>
<w:snapToGrid w:val="0"/>
<w:ind w:start="210pt" w:firstLineChars="0" w:firstLine="21pt"/>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t xml:space="preserve"> </w:t>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
</w:p>
<w:p w:rsidR="00EF0F10" w:rsidRDefault="006C13D7">
<w:pPr>
<w:adjustRightInd w:val="0"/>
<w:snapToGrid w:val="0"/>
<w:ind w:start="210pt" w:firstLineChars="0" w:firstLine="21pt"/>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t xml:space="preserve"> </w:t>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidR="005F6F99">
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:tab/>
</w:r>
</w:p>
<w:p w:rsidR="00E35097" w:rsidRPr="005F6F99" w:rsidRDefault="00E35097" w:rsidP="005F6F99">
<w:pPr>
<w:adjustRightInd w:val="0"/>
<w:snapToGrid w:val="0"/>
<w:ind w:start="210pt" w:firstLineChars="0" w:firstLine="21pt"/>
<w:rPr>
<w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:b/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:sectPr w:rsidR="00E35097" w:rsidRPr="005F6F99" w:rsidSect="005F6F99">
<w:headerReference w:type="even" r:id="rId9"/>
<w:headerReference w:type="default" r:id="rId10"/>
<w:footerReference w:type="even" r:id="rId11"/>
<w:footerReference w:type="default" r:id="rId12"/>
<w:headerReference w:type="first" r:id="rId13"/>
<w:footerReference w:type="first" r:id="rId14"/>
<w:pgSz w:w="842pt" w:h="595.35pt" w:orient="landscape"/>
<w:pgMar w:top="65.20pt" w:right="72pt" w:bottom="62.35pt" w:left="72pt" w:header="42.55pt"
w:footer="49.60pt" w:gutter="0pt"/>
<w:pgNumType w:fmt="upperRoman" w:start="1"/>
<w:cols w:space="21.25pt"/>
<w:formProt w:val="0"/>
<w:titlePg/>
<w:docGrid w:linePitch="380"/>
</w:sectPr>
</w:pPr>
</w:p>
<w:p w:rsidR="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="005F6F99">
<w:pPr>
<w:pStyle w:val="2"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="2"/>
</w:numPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:lastRenderedPageBreak/>
<w:t>交叉跨越校验报告</w:t>
</w:r>
</w:p>
<w:tbl>
<w:tblPr>
<w:tblW w:w="701.70pt" w:type="dxa"/>
<w:jc w:val="center"/>
<w:tblBorders>
<w:top w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:start w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:bottom w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:end w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:insideV w:val="single" w:sz="4" w:space="0" w:color="auto"/>
</w:tblBorders>
<w:tblLayout w:type="fixed"/>
<w:tblLook w:firstRow="0" w:lastRow="0" w:firstColumn="0" w:lastColumn="0" w:noHBand="0" w:noVBand="0"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="709"/>
<w:gridCol w:w="1028"/>
<w:gridCol w:w="956"/>
<w:gridCol w:w="843"/>
<w:gridCol w:w="1001"/>
<w:gridCol w:w="1128"/>
<w:gridCol w:w="2127"/>
<w:gridCol w:w="2551"/>
<w:gridCol w:w="2273"/>
<w:gridCol w:w="1418"/>
</w:tblGrid>
<w:tr w:rsidR="009E62D0" w:rsidRPr="00A46150" w:rsidTr="009E62D0">
<w:trPr>
<w:jc w:val="center"/>
</w:trPr>
<w:tc>
<w:tcPr>
<w:tcW w:w="35.45pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>序号</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="51.40pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>校验工况</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="47.80pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>杆塔编号</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="42.15pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>回路</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="50.05pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>相序</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="56.40pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>地物名称</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="106.35pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="150" w:firstLine="18pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>垂直</w:t>
</w:r>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>(m)</w:t>
</w:r>
</w:p>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="150" w:firstLine="18pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>测量值</w:t>
</w:r>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>/</w:t>
</w:r>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>规程值</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="127.55pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="150" w:firstLine="18pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>水平</w:t>
</w:r>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>(m)</w:t>
</w:r>
</w:p>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>测量值</w:t>
</w:r>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>/</w:t>
</w:r>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>规程值</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="113.65pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="150" w:firstLine="18pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>净空</w:t>
</w:r>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>(m)</w:t>
</w:r>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>测量值</w:t>
</w:r>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>/</w:t>
</w:r>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>规程值</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="70.90pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>是否安全</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
<#list resultList as result>
<w:tr w:rsidR="009E62D0" w:rsidRPr="00A46150" w:rsidTr="009E62D0">
<w:trPr>
<w:jc w:val="center"/>
</w:trPr>
<w:tc>
<w:tcPr>
<w:tcW w:w="35.45pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>${result_index+1}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="51.40pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>${(result.gimWorkingName)!""}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="47.80pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>${(result.spanName)!""}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="42.15pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>${(result.loopName)!""}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="50.05pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>${(result.phaseName)!""}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="56.40pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="150" w:firstLine="18pt"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>${(result.groundObjectName)!""}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="106.35pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="150" w:firstLine="18pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>${(result.verticalDistanceStr)!""}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="127.55pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="150" w:firstLine="18pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>${(result.horizontalDistanceStr)!""}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="113.65pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>${(result.clearanceStr)!""}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="70.90pt" w:type="dxa"/>
<w:shd w:val="clear" w:color="auto" w:fill="auto"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p w:rsidR="005F6F99" w:rsidRPr="005F6F99" w:rsidRDefault="005F6F99" w:rsidP="009E62D0">
<w:pPr>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="005F6F99">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>${(result.isSecurity)!""}</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</#list>
</w:tbl>
<w:p w:rsidR="005A31A9" w:rsidRDefault="00173FF2" w:rsidP="006C13D7">
<w:pPr>
<w:pStyle w:val="a4"/>
<w:ind w:firstLineChars="0" w:firstLine="0pt"/>
<w:rPr>
<w:rFonts w:ascii="宋体" w:eastAsia="宋体" w:hAnsi="宋体"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:ascii="宋体" w:eastAsia="宋体" w:hAnsi="宋体" w:hint="eastAsia"/>
</w:rPr>
<w:t xml:space="preserve"> </w:t>
</w:r>
</w:p>
<w:sectPr w:rsidR="005A31A9" w:rsidSect="005F6F99">
<w:footerReference w:type="even" r:id="rId15"/>
<w:pgSz w:w="841.90pt" w:h="595.30pt" w:orient="landscape"/>
<w:pgMar w:top="79.40pt" w:right="72pt" w:bottom="79.40pt" w:left="72pt" w:header="28.35pt"
w:footer="42.55pt" w:gutter="0pt"/>
<w:cols w:space="21.25pt"/>
<w:docGrid w:linePitch="380"/>
</w:sectPr>
</w:body>
</w:document>
controller部分代码如下
@GetMapping("/exportCrossResult")
public void exportCrossResult(HttpServletRequest request,HttpServletResponse response,String projectName,String key){
Map<String, Object> map = new HashMap();
map.put("docFileName",projectName+"交叉跨越校验报告.docx");
map.put("filetype","crossExport");//filetype ftl模板名
Calendar calendar = Calendar.getInstance();
map.put("year",calendar.get(Calendar.YEAR)+"");
map.put("month",calendar.get(Calendar.MONTH)+1);
map.put("day",calendar.get(Calendar.DATE));
map.put("projectName",projectName);
List<LineStrideAcrossCheckData> list = (List<LineStrideAcrossCheckData>) request.getSession().getAttribute(key);
request.getSession().removeAttribute(key);
map.put("resultList",list);
//导出文档
Word07Generatorutil.except07Word(map,response);
}
其中docFileName是导出文件的名称
fileType是ftl文件的名称
其他数据是ftl文件所需数据
生成的工具类
package com.bochao.util;
import com.bochao.common.util.UUIDUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName Word07Generatorutil
* @Description TODO
* @Author yueyiming
* @Date 2020/6/15 14:03
* Version 1.0
**/
@Component
public class Word07Generatorutil {
/**
* 资料临时文件夹
*/
private static String tempDirPath;
@Value("${file.tempDirPath}")
public void setTempDirPath(String tempDirPath) {
Word07Generatorutil.tempDirPath = tempDirPath;
}
protected final static Logger logger = LoggerFactory.getLogger(WordGenerator.class);
private static Configuration configuration;
private static Map<String, Template> allTemplates;
static {
logger.info("Word07Generatorutil--------------------");
configuration = new Configuration(Configuration.VERSION_2_3_23);
configuration.setDefaultEncoding("utf-8");
configuration.setClassForTemplateLoading(WordGenerator.class, "/docxFtl");
allTemplates = new HashMap();
}
/**
* 根据模板在临时路径生成 word/document.xml
* 并把其他文件复制到临时目录下
*
* @param dataMap
* @param type
* @return
*/
public static String createDocx(Map<?, ?> dataMap, String type) throws Exception {
allTemplates.put(type, configuration.getTemplate(type + ".ftl"));
String path = tempDirPath + File.separator + UUIDUtil.getPrimaryKeyUUID() + File.separator + "word";
File pFile = new File(path);
if (!pFile.exists()) {
pFile.mkdirs();
}
File f = new File(path, "document.xml");
Template t = allTemplates.get(type);
if (!f.exists()) {
f.createNewFile();
}
try (Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8")) {
t.process(dataMap, w);
}
File zipfile = null;
try (InputStream in = Word07Generatorutil.class.getClassLoader().getResourceAsStream("docxTemp" + File.separator + type + ".zip")) {
zipfile = new File(tempDirPath + File.separator + UUIDUtil.getPrimaryKeyUUID() + File.separator + type + ".zip");
File pZipfile = new File(zipfile.getParent());
// if(pZipfile.isDirectory()){
pZipfile.mkdirs();
// }
if (!zipfile.exists()) {
zipfile.createNewFile();
}
try (FileOutputStream fileOutputStream = new FileOutputStream(zipfile)) {
IOUtils.copy(in, fileOutputStream);
}
BoChaoPackageUtil.unZip(zipfile.getAbsolutePath(), pFile.getParent());
} finally {
if (zipfile != null) {
deleteFileOrDir(zipfile.getParent());
}
}
return pFile.getParent();
}
/**
* @Author: yueyiming
* @Date: 2020/6/17 9:41
* @param: [map, response]
* @return: void
* @Description: 导出07word
**/
public static void except07Word(Map<String, Object> map, HttpServletResponse response) {
File file = null;
String path = null;
try {
path = createDocx(map, map.get("filetype").toString());
//打包
String dpath = tempDirPath + File.separator + UUIDUtil.getPrimaryKeyUUID();
File pFile = new File(dpath);
if (!pFile.exists()) {
pFile.mkdirs();
}
String docxFileName = map.get("docFileName").toString();
file = new File(pFile, docxFileName);
if (!file.exists()) {
file.createNewFile();
}
BoChaoPackageUtil.pag(file.getAbsolutePath(), path);
} catch (Exception e) {
e.printStackTrace();
}
try (InputStream fin = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream()) {
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
String docFileName = map.get("docFileName").toString();
response.addHeader("Content-Disposition", "attachment;filename=" + new String(docFileName.getBytes("gb2312"), "ISO8859-1"));
byte[] buffer = new byte[512];
//boolean var11 = true;
int bytesToRead;
while ((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
} catch (Exception var16) {
logger.error(var16.getMessage(), var16);
} finally {
deleteFileOrDir(file.getParent());
deleteFileOrDir(path);
}
}
/**
* 删除文件或文件夹
*
* @param path
* @return
*/
public static boolean deleteFileOrDir(String path) {
File file = new File(path);
if (!file.exists()) {
throw new RuntimeException("删除的path" + path + "不存在!");
}
//文件删除
if (file.isFile()) {
file.delete();
return true;
}
//文件夹删除
deleterDir(path);
return true;
}
/**
* 删除文件夹
*
* @param path
* @return
*/
public static boolean deleterDir(String path) {
File file = new File(path);
if (file.isFile()) {
return false;
}
//文件夹删除
for (File childFile : file.listFiles()) {
if (childFile.isFile()) {
childFile.delete();
} else {
//删除文件夹
deleterDir(childFile.getAbsolutePath());
}
}
file.delete();
return true;
}
}
打包解压的工具类
package com.bochao.util;
import java.io.*;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @ClassName BoChaoPackageUtil
* @Description 打包工具不压缩
* @Author yueyiming
* @Date 2020/6/15 11:16
* Version 1.0
**/
public class BoChaoPackageUtil {
/**
* @param zipPath 压缩后的文件路径
* @param srcPathName 要压缩的文件路径
* 参数srcPathName也可以定义成数组形式,需调用方把参数封装到数组中传过来即可
* @Author: yueyiming
* @Date: 2020/6/16 9:08
* @return: void
* @Description: 压缩单个或多文件方法
**/
private static void compress(String zipPath, String[] srcPathName) {
//压缩后的文件对象
File zipFile = new File(zipPath);
//创建写出流操作
try (FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); ZipOutputStream out = new ZipOutputStream(cos)) {
out.setLevel(9);
for (String srcPath : srcPathName) {
//创建需要压缩的文件对象
File file = new File(srcPath);
if (!file.exists()) {
throw new RuntimeException(srcPath + "不存在!");
}
// (1) 如果在zip压缩文件中不需要一级文件目录,定义String basedir = "";
// 下面的compress方法中当判断文件file是目录后不需要加上basedir = basedir + file.getName() + File.separator;
// (2) 如果只是想在压缩后的zip文件里包含一级文件目录,不包含二级以下目录,
// 直接在这定义String basedir = file.getName() + File.separator;
// 下面的compress方法中当判断文件file是目录后不需要加上basedir = basedir + file.getName() + File.separator;
// (3) 如果想压缩后的zip文件里包含一级文件目录,也包含二级以下目录,即zip文件里的目录结构和原文件一样
// 在此定义String basedir = "";
// 下面的compress方法中当判断文件file是目录后需要加上basedir = basedir + file.getName() + File.separator;
//String basedir = file.getName() + File.separator;
String basedir = "";
compress(file, out, basedir);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Author: yueyiming
* @Date: 2020/6/16 9:11
* @param: [file, out, basedir]
* @return: void
* @Description:
**/
private static void compress(File file, ZipOutputStream out, String basedir) {
/*
* 判断是目录还是文件
*/
if (file.isDirectory()) {
basedir += file.getName() + File.separator;
compressDirectory(file, out, basedir);
} else {
compressFile(file, out, basedir);
}
}
/**
* @Author: yueyiming
* @Date: 2020/6/16 9:11
* @param: [dir, out, basedir]
* @return: void
* @Description: 压缩目录
**/
private static void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists()) {
return;
}
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
/* 递归 */
compress(files[i], out, basedir);
}
}
/**
* 压缩一个文件
*/
private static void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
return;
}
try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file))) {
//创建Zip实体,并添加进压缩包
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
//读取待压缩的文件并写进压缩包里
int count;
byte data[] = new byte[8192];
while ((count = bis.read(data, 0, 8192)) != -1) {
out.write(data, 0, count);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Author: yueyiming
* @Date: 2020/6/16 9:11
* @param: [zipPath, srcPathName]
* @return: void
* @Description: 打包目录下srcPathName的文件
**/
public static void pag(String zipPath, String srcPathName) {
File file = new File(srcPathName);
File[] files = file.listFiles();
String[] strings = Arrays.stream(files).map(f -> f.getAbsolutePath()).toArray(String[]::new);
compress(zipPath, strings);
}
/**
* 解压zip文件
*
* @param zipFileName 待解压的zip文件路径,例如:c:\\a.zip
* @param outputDirectory 解压目标文件夹,例如:c:\\a\
*/
public static void unZip(String zipFileName, String outputDirectory)
throws Exception {
org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(zipFileName);
try {
Enumeration<?> e = zipFile.getEntries();
org.apache.tools.zip.ZipEntry zipEntry = null;
createDirectory(outputDirectory, "");
while (e.hasMoreElements()) {
zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
} else {
String fileName = zipEntry.getName();
fileName = fileName.replace('\\', '/');
if (fileName.indexOf("/") != -1) {
createDirectory(outputDirectory, fileName.substring(0,
fileName.lastIndexOf("/")));
fileName = fileName.substring(
fileName.lastIndexOf("/") + 1
);
}
File f = new File(outputDirectory + File.separator
+ zipEntry.getName());
f.createNewFile();
try (InputStream in = zipFile.getInputStream(zipEntry); FileOutputStream out = new FileOutputStream(f);) {
byte[] by = new byte[1024];
int c;
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
}
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
zipFile.close();
}
}
/**
* @param directory
* @param subDirectory
* @Author: yueyiming
* @Date: 2020/6/17 12:51
* @return: void
* @Description: 创建目录
**/
private static void createDirectory(String directory, String subDirectory) {
String dir[];
File fl = new File(directory);
try {
if (subDirectory == "" && fl.exists() != true) {
fl.mkdir();
} else if (subDirectory != "") {
dir = subDirectory.replace('\\', '/').split("/");
for (int i = 0; i < dir.length; i++) {
File subFile = new File(directory + File.separator + dir[i]);
if (subFile.exists() == false)
subFile.mkdir();
directory += File.separator + dir[i];
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}