记录下工作内容
/*
* @b 为html原文的byte流 byte b[] = content.getBytes("utf-8"); 参考
*/
public void downloadWord( byte[] b, OutputStream out)
throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
try {
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
poifs.writeFilesystem(out);
bais.close();
out.close();
}catch(Exception e){
e.printStackTrace();
}finally {
if(bais!=null)bais.close();
if(out!=null) {out.flush();out.close();}
}
}
以上放到工具类里
第一个参数byte[] b为html文本转换的
第二个参数是OutputStream out 为response.getOutputStream()
以下为web服务端调用代码,根据不同的调用下载方式,以下代码也不尽相同。
response.setContentType("application/msword");//导出word格式
response.setHeader("Content-Disposition", "attachment; filename=" + new String(filename.getBytes(), "8859_1"));
String htmlcontent ="<html><body></body></html>";byte b[] = htmlcontent.getBytes("utf-8");
downloadnoticedocutil.downloadWord(b, response.getOutputStream());
response.flushBuffer();
response.getOutputStream().close();