我将一个 doc 文件转换为 html 并在我的 jeditorepane 上显示该 doc 文件。
该程序正在我的管理员帐户上运行。当我在我的 windows xp 中使用用户帐户登录时,该文件不显示。
try
{
File docFile=new File("c:\\159.doc"); // file object was created
FileInputStream finStream=new FileInputStream(docFile.getAbsolutePath());
HWPFDocument doc=new HWPFDocument(finStream);
WordExtractor wordExtract=new WordExtractor(doc);
Document newDocument = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument();
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(newDocument) ;
wordToHtmlConverter.processDocument(doc);
StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance()
.newTransformer();
transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
transformer.setOutputProperty( OutputKeys.METHOD, "html" );
transformer.transform(
new DOMSource( wordToHtmlConverter.getDocument() ),
new StreamResult( stringWriter ) );
String html = stringWriter.toString();
System.out.println(html);
FileOutputStream fos;
DataOutputStream dos;
File file= new File("C:\\my.html");
fos = new FileOutputStream(file);
dos=new DataOutputStream(fos);
// dos.writeInt();
dos.writeUTF(html);
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setEditable(false);
editorPane.setPage(file.toURL());
JScrollPane scrollPane = new JScrollPane(editorPane);
JFrame f = new JFrame("O'Reilly & Associates");
// Next line requires Java 1.3
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setSize(512, 342);
f.setVisible(true);
}catch(Exception e)
{
}