java 使用Apache接口导出.docx格式的word文档打开报错,用wps或者后缀改成.doc却能打开
public static void exportWords(HttpServletRequest request, HttpServletResponse response, String text, String fileName) {
ServletOutputStream ostream = null;
POIFSFileSystem poifs = null;
ByteArrayInputStream bais = null;
try {
//设置编码
byte bArr[] = text.getBytes("utf-8");
bais = new ByteArrayInputStream(bArr);
//Apache office处理API
poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
request.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
response.addHeader("Content-Disposition", "attachment;filename=" +
new String(fileName.getBytes("GB2312"), "iso8859-1") + ".doc");
ostream = response.getOutputStream();
poifs.writeFilesystem(ostream);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bais != null) {
bais.close();
}
if (ostream != null) {
ostream.close();
}
if (poifs != null) {
poifs.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}