1 package com.svse.controller;
2
3 import javax.xml.parsers.DocumentBuilderFactory;
4 import javax.xml.parsers.ParserConfigurationException;
5 import javax.xml.transform.OutputKeys;
6 import javax.xml.transform.Transformer;
7 import javax.xml.transform.TransformerException;
8 import javax.xml.transform.TransformerFactory;
9 import javax.xml.transform.dom.DOMSource;
10 import javax.xml.transform.stream.StreamResult;
11
12 import org.apache.poi.hwpf.HWPFDocument;
13 import org.apache.poi.hwpf.converter.PicturesManager;
14 import org.apache.poi.hwpf.converter.WordToHtmlConverter;
15 import org.apache.poi.hwpf.usermodel.PictureType;
16 import org.apache.poi.xwpf.converter.core.BasicURIResolver;
17 import org.apache.poi.xwpf.converter.core.FileImageExtractor;
18 import org.apache.poi.xwpf.converter.core.FileURIResolver;
19 import org.apache.poi.xwpf.converter.core.IURIResolver;
20 import org.apache.poi.xwpf.converter.core.IXWPFConverter;
21 import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
22 import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
23 import org.apache.poi.xwpf.usermodel.XWPFDocument;
24
27
public
class
TestWordToHtml {
28
29
public
static
final
String STORAGEPATH=
"C://works//files//"
;
30
public
static
final
String IP=
"192.168.30.222"
;
31
public
static
final
String PORT=
"8010"
;
32
public
static
void main(String[] args) throws IOException, TransformerException, ParserConfigurationException {
33 TestWordToHtml wt=
new
TestWordToHtml();
34
35 wt.Word2007ToHtml(
"甲骨文考证.docx"
);
36
37 }
38
39
45
public
void Word2003ToHtml(String fileName) throws IOException, TransformerException, ParserConfigurationException {
46
47
final
String imagepath = STORAGEPATH+
"fileImage/"
;
48
final
String strRanString=getRandomNum();
49 String filepath =STORAGEPATH;
50 String htmlName =fileName.substring(0, fileName.indexOf(
"."
))+
"2003.html"
;
51
final
String file = filepath + fileName;
52 InputStream input =
new
FileInputStream(
new
File(file));
53 HWPFDocument wordDocument =
new
HWPFDocument(input);
54 WordToHtmlConverter wordToHtmlConverter =
new
WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
55
56 wordToHtmlConverter.setPicturesManager(
new
PicturesManager() {
57
public
String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {
58 File imgPath =
new
File(imagepath);
59
if
(!imgPath.exists()){
60 imgPath.mkdirs();
61 }
62
63 File file =
new
File(imagepath +strRanString+suggestedName);
64
try
{
65 OutputStream os =
new
FileOutputStream(file);
66 os.write(content);
67 os.close();
68 }
catch
(FileNotFoundException e) {
69 e.printStackTrace();
70 }
catch
(IOException e) {
71 e.printStackTrace();
72 }
73
74
return
"http://"
+IP+
":"
+PORT+
"//uploadFile/fileImage/"
+strRanString+suggestedName;
75
76 }
77 });
78
79
80 wordToHtmlConverter.processDocument(wordDocument);
81 Document htmlDocument = wordToHtmlConverter.getDocument();
82
83 File htmlFile =
new
File(filepath +strRanString+htmlName);
84 OutputStream outStream =
new
FileOutputStream(htmlFile);
85
86
87 DOMSource domSource =
new
DOMSource(htmlDocument);
88 StreamResult streamResult =
new
StreamResult(outStream);
89
90 TransformerFactory factory = TransformerFactory.newInstance();
91 Transformer serializer = factory.newTransformer();
92 serializer.setOutputProperty(OutputKeys.ENCODING,
"utf-8"
);
93 serializer.setOutputProperty(OutputKeys.INDENT,
"yes"
);
94 serializer.setOutputProperty(OutputKeys.METHOD,
"html"
);
95
96 serializer.transform(domSource, streamResult);
97 outStream.close();
98
99 System.out.println(
"生成html文件路径:"
+
"http://"
+IP+
":"
+PORT+
"//uploadFile/"
+strRanString+htmlName);
100 }
101
102
106
public
void Word2007ToHtml(String fileName) throws IOException {
107
108
final
String strRanString=getRandomNum();
109
110 String filepath = STORAGEPATH+strRanString;
111 String htmlName =fileName.substring(0, fileName.indexOf(
"."
))+
"2007.html"
;
112 File f =
new
File(STORAGEPATH+fileName);
113
if
(!f.exists()) {
114 System.out.println(
"Sorry File does not Exists!"
);
115 }
else
{
116
if
(f.getName().endsWith(
".docx"
) || f.getName().endsWith(
".DOCX"
)) {
117
try
{
118
119 InputStream in =
new
FileInputStream(f);
120 XWPFDocument document =
new
XWPFDocument(in);
121
122
123 File imageFolderFile =
new
File(filepath);
124 XHTMLOptions options = XHTMLOptions.create().URIResolver(
new
FileURIResolver(imageFolderFile));
125 options.setExtractor(
new
FileImageExtractor(imageFolderFile));
126 options.URIResolver(
new
IURIResolver() {
127
public
String resolve(String uri) {
128
129
return
"http://"
+IP+
":"
+PORT+
"//uploadFile/"
+strRanString +
"/"
+ uri;
130 }
131 });
132
133 options.setIgnoreStylesIfUnused(false);
134 options.setFragment(true);
135
136
137 OutputStream out =
new
FileOutputStream(
new
File(filepath + htmlName));
138 IXWPFConverter<XHTMLOptions> converter = XHTMLConverter.getInstance();
139 converter.convert(document,out, options);
140
141 System.out.println(
"html路径:"
+
"http://"
+IP+
":"
+PORT+
"//uploadFile/"
+strRanString+htmlName);
142 }
catch
(Exception e) {
143 e.printStackTrace();
144 }
145
146 }
else
{
147 System.out.println(
"Enter only MS Office 2007+ files"
);
148 }
149 }
150 }
151
152
158
public
static
String getRandomNum(){
159
Date
dt =
new
Date
();
160 SimpleDateFormat sdf =
new
SimpleDateFormat(
"yyyyMMddHHmmss"
);
161 String str=sdf.format(dt);
162
return
str;
163 }
164
165 }