问题遇到的现象和发生背景
springboot用poi把富文本转成word保存到本地,在windows下打开生成的word可以看到图片,但是群晖nas上打开不显示图片,我把群晖nas服务器上生成的word下载到windows可以查看图片,
用代码块功能插入代码,请勿粘贴截图
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
Article article= articleService.getById(id);
String title = article.getTitle();
String text = article.getContent();
try {
//word内容
String content="<html><body>" +
"<p style=\"text-align: center;\"><span style=\"font-family: 宋体, SimHei; font-size: 24px;\">"
+ title + "</span></p>" + text + "</body></html>";
byte b[] = content.getBytes("GBK"); //这里是必须要设置编码的,不然导出中文就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
/*
* 关键地方
* 生成word格式 */
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
directory.createDocument("WordDocument", bais);
String account="temp";
if (ShrioUtil.getLoginUser() != null) {
account = ShrioUtil.getLoginUser().getAccount();
}
String uploadPath = Constants.SEPARATOR+account + Constants.SEPARATOR+Constants.FILE+ Constants.SEPARATOR;
String dirPath = webUploadPath + uploadPath;
File dir = new File(dirPath);
if (!dir.exists()){
boolean mkdirs = dir.mkdirs();
if (!mkdirs) {
throw new Exception("创建目录失败!" + dir.getAbsolutePath());
}
}
//保存到文件服务器
FileCopyUtils.copy(poifs.createDocumentInputStream("WordDocument"),new FileOutputStream(dirPath+title+".docx"));
运行结果及报错内容
这是windows上的word
这是在群晖上打开的word
我的解答思路和尝试过的方法
修改编码等等都无法达到效果,有遇到这种情况的老哥吗?
我想要达到的结果
服务器上的word能够显示图片