解决导出Word内容乱码的正确方法:
public IZrarResponse bzdsexport(IZrarRequest req) throws Exception {
IZrarResponse res = new ZrarResponse();
String zlcode = req.getParameter("zlcode");
String zlnrsql = "select zlnr from zl_info_nr where zlcode=?";
ByteArrayOutputStream stream = daozsk.getBlob(zlnrsql, "zlnr", new Object[] {zlcode});
String zltitlesql = "select zltitle from zl_info where zlcode=?";
ZlInfoBO zlinfobo = daozsk.getBean(zltitlesql,ZlInfoBO.class, Arrays.asList(zlcode));
String zltitle = zlinfobo.getZltitle();
zltitle = zltitle +".doc";
//系统类型
String xtlx = PropertyZskManager.getProperty("xtlx","zsk");
byte[] b = null;
b = stream.toByteArray();
stream.close();
Blob fnr = daozsk.createBlob(b);
if("Windows".equals(xtlx)){
InputStream bais= fnr.getBinaryStream();
res.addInputStream(bais,zltitle,b.length);
}else {
InputStream bais= fnr.getBinaryStream();
zltitle = new Date().getTime() + ".doc";
res.addInputStream(bais,zltitle,b.length);
}
return res;
}
导出的内容由于是CLOB类型的字段,数据量较大,在获取的时候采用了getBlob的方法,返回类型是字节数组输出流ByteArrayOutputStream 。
ByteArrayOutputStream stream = daozsk.getBlob(zlnrsql, "zlnr", new Object[] {zlcode});
得到输出流stream后,在使用:
byte[] b = null;
b = stream.toByteArray();
stream.close();
Blob fnr = daozsk.createBlob(b);
把stream转换成一个字节数组,关闭流。把得到的字节数组存放到一个Blob里面去。
InputStream bais= fnr.getBinaryStream();
res.addInputStream(bais,zltitle,b.length);
从存放的Blob中取到这个值,转成二进制流,返回类型是输入流。最后直接输出。就能导出Word,不会出现乱码。图示如下:
不能解决导出Word乱码的代码如下:
主要原因是因为存在表里的字段包含charset=gb2312"
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
这种文本内容 又不能去修改数据库 手动测试过要是改成UTF-8导出就没问题。但是这种操作不可取呀!