【JAVA】使用jacob生成的html,关于文字乱码处理,图片无法显示等问题。

来源:互联网 发布:神话崔文子 知乎 编辑:程序博客网 时间:2024/05/29 18:05

最近项目中需要把word和excel直接通过ext导入到服务器上。用到了jacob,然后放在服务器目录上,问题出现了:

jacob网上例子很多就不说了,但是jdk版本和jar包版本必须保持一致。


1.图片无法显示:

因为生成html的时候会生成一个files文件,就和文件另存为网页一样会产生一个files文件一样,因为里面用了vml矢量!vml始终为false显示无法显示图片。

最后只有用自己又重新用流读写了此数据,替换掉了里面的vml矢量这样图片可以显示。

方法:

/** *  * @Title: writeHtmlReplaceVml * @Description: TODO(word转html后替换vml矢量,否则无法显示图片) * @param vmlFilePath * @throws */private static void writeHtmlReplaceVml(String vmlFilePath) {String strInfo = null;// 读取网页文件try {FileInputStream fis = new FileInputStream(vmlFilePath);InputStreamReader isr = new InputStreamReader(fis);BufferedReader buf = new BufferedReader(isr);int i = 0;String c;try {while ((c = buf.readLine()) != null) {if (i == 0) {strInfo = c;} else {strInfo = strInfo + "\n" + c;}i++;}buf.close();isr.close();fis.close();} catch (IOException e) {e.printStackTrace();}} catch (FileNotFoundException e) {e.printStackTrace();}// 处理网页内容,并重新生成新的网页文件try {File write = new File(vmlFilePath);//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(write),"utf-8"));BufferedWriter bw = new BufferedWriter(new FileWriter(write));strInfo = strInfo.replace("<![if !vml]>", "");strInfo = strInfo.replace("<![endif]>", "  ");//strInfo = strInfo.replace("charset=gb2312", "charset=gb2312");// strInfo=strInfo.replace("<td","<td nowrap");bw.write(strInfo);bw.close();} catch (IOException ee) {System.out.println(ee.getMessage());}// 删除临时生成的网页文件及其附属文件夹// try// {// File fl = new File(strFile);// if(fl.exists() && fl.isFile()){fl.delete();}//删除临时网页// int intLen=strFile.lastIndexOf(".");// String strFileFolder=strFile.substring(0,intLen+1)+"files";// delFolder(strFileFolder);//删除临时网页附属的文件夹// }catch (Exception ee) {System.out.println(ee.getMessage());}}

2.文字乱码。

在显示的时候嵌套在了iframe里面,生成的html文件格式为gb2312,项目为utf-8,显示的时候word正常,excel正文正常,但是下面若有sheet分页的话,分页中文乱码,开始时候思路是写入的时候改变为utf-8,但是在处理后发现部分文字正常,但是某些文字还是乱码。无奈之下又去寻找答案,想了下当时使用mht显示的时候配置如下代码:web配置:

<!-- 页面可以访问mht文件配置 --><mime-mapping><extension>mht</extension><mime-type>message/rfc822</mime-type></mime-mapping>

在网上有找了下,发现需配置web:

<web-app>  <!-- 页面可以访问html文件配置 -->   <jsp-config>        <jsp-property-group>        <description>HTML Encoding</description>        <display-name>HTML Encoding Config</display-name>        <url-pattern>*.html</url-pattern>        <el-ignored>true</el-ignored>        <page-encoding>gbk</page-encoding>        <scripting-invalid>true</scripting-invalid>      </jsp-property-group>    </jsp-config>  </web-app>  

这样gb2312也可以显示正常。

原创粉丝点击