本人现在就被这样一个问题困扰,html转word保存时,在windows上好好的,到linux上后,图片无法转换,图片转换后变成红X,找了很多资料都解决不了,给出来大家一起研究研究,也希望能够解决问题,谢谢!
程序如下:
public static void convertHtmlToWord(String saveWordPath,String sourceHtml) throws Exception{
//String b=FileUtil.readFileByChars("d:/test.xml");
ByteArrayInputStream bais=null;
bais = new ByteArrayInputStream (sourceHtml.getBytes("utf-8"));
POIFSFileSystem poifs = new POIFSFileSystem ();
DirectoryEntry directory = poifs.getRoot ();
directory.createDocument ("WordDocument", bais);
FileOutputStream outStream = null;
outStream = new FileOutputStream(saveWordPath);
poifs.writeFilesystem (outStream);
outStream.close();
bais.close ();
}
补充一下sourceHtml,关键看图片路径/opt/workspace/test/webapps/test/test.jpg:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>组装html</title>
</head>
<body>
<DIV>
<DIV>
<DIV>
<DIV>
5<FONT color=#09f738 size=6>4</FONT><FONT color=#f70968>655</FONT>
<FONT size=6>5
<IMG border=0 src="/opt/workspace/test/webapps/test/test.jpg">
</FONT>
</DIV>
</DIV>
</DIV>
</DIV>
</body>
</html>
附件保存路径saveWordPath:/opt/workspace/test/webapps/test/test1.doc;
得出linux下word文档如下图,说明一下,在windows系统下是能够正常转换的的: