生成的docx图片显示不出来

qq_38984067 2019-06-02 06:09:59
java用模板生成docx后图片不显示 然后解压后重新压缩(未修改任何的文件) 图片就能显示了,是怎么回事

过程将模板docx解压 用freemarker引擎将模板填好 再把对应的图片粘贴到media下


String xmlDocumentXmlRelsComment = FreeMarkUtils.getFreemarkerContent(dataMap, xmlDocumentXmlRels,
templatePath);
ByteArrayInputStream documentXmlRelsInput = new ByteArrayInputStream(xmlDocumentXmlRelsComment.getBytes());

ByteArrayInputStream headerInput = FreeMarkUtils.getFreemarkerContentInputStream(dataMap, xmlHeader,
templatePath);

ByteArrayInputStream documentInput = FreeMarkUtils.getFreemarkerContentInputStream(dataMap, xmlDocument,
templatePath);
File documentxml = new File(Common.getRealPath()+"tempExport"+File.separator+dataMap.get("id")+File.separator+"word"+File.separator+"document.xml");
OutputStream os = new FileOutputStream(documentxml);
DebuggingLog.printDetailedInfo("------------------------------------");
DebuggingLog.printDetailedInfo("documentxml:"+documentxml.exists());
DebuggingLog.printDetailedInfo("documentxml:"+documentxml.getPath());
int len = -1;
byte[] buffer = new byte[1024];
if (documentInput != null) {
while ((len = documentInput.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
documentInput.close();
}
os.close();

//document.rels
File documentXmlRels = new File(Common.getRealPath()+"tempExport"+File.separator+dataMap.get("id")+File.separator+"word"+File.separator+"_rels"+File.separator+xmlDocumentXmlRels);
OutputStream os1 = new FileOutputStream(documentXmlRels);
len = -1;
buffer = new byte[1024];
if (documentXmlRelsInput != null) {
while ((len = documentXmlRelsInput.read(buffer)) != -1) {
os1.write(buffer, 0, len);
}
documentXmlRelsInput.close();
}
os1.close();

//header.xml
File headerxml = new File(Common.getRealPath()+"tempExport"+File.separator+dataMap.get("id")+File.separator+"word"+File.separator+xmlHeader);
OutputStream os2 = new FileOutputStream(headerxml);
len = -1;
buffer = new byte[1024];
if (headerInput != null) {
while ((len = headerInput.read(buffer)) != -1) {
os2.write(buffer, 0, len);
}
headerInput.close();
}
os2.close();


List<ProductPhoto> proPhotoList = (List<ProductPhoto>) dataMap.get("proPhotoList");
List<PasswordPhoto> pwPhotoList = (List<PasswordPhoto>) dataMap.get("pwPhotoList");

SAXBuilder sb = new SAXBuilder();
Document doc = null;
doc = sb.build(documentXmlRels.getPath());
Element root = doc.getRootElement();


for (int i = 0; i < proPhotoList.size(); i++) {
List<Map<String, String>> maplist= proPhotoList.get(i).getDocxMap();
for (int j = 0; j < maplist.size(); j++) {
Map photo = maplist.get(j);
//将图片信息写入rels
Element children = new Element("Relationship");
children.setAttribute("Id", photo.get("rId").toString());
children.setAttribute("Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
children.setAttribute("Target","media/"+photo.get("name").toString());
root.addContent(children);
//将图片复制到文件夹media下
FileUtil.copyFile(photo.get("path").toString(),Common.getRealPath()+"tempExport"+File.separator+dataMap.get("id")+File.separator+"word/media/"+photo.get("name").toString());

}

}
for (int i = 0; i < pwPhotoList.size(); i++) {
List<Map<String, String>> maplist= pwPhotoList.get(i).getDocxMap();
for (int j = 0; j < maplist.size(); j++) {
Map photo = maplist.get(j);
//将图片信息写入rels
Element children = new Element("Relationship");
children.setAttribute("Id", photo.get("rId").toString());
children.setAttribute("Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
children.setAttribute("Target","media/"+photo.get("name").toString());
root.addContent(children);
//将图片复制到文件夹media下
FileUtil.copyFile(photo.get("path").toString(),Common.getRealPath()+"tempExport"+File.separator+dataMap.get("id")+File.separator+"word/media/"+photo.get("name"));
}
}
//保存document.xml.rels
XmlUtils.saveXML(doc, documentXmlRels.getPath());

//把修改好的文件压缩回docx
ZipUtils.doCompress(zipSrcPath, outputPath, true);
...全文
868 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
as21665 2019-08-02
  • 打赏
  • 举报
回复
引用 4 楼 qq_21955687的回复:
引用 2 楼 as21665 的回复:
楼主解决了吗?我也是替换图片正常下载就是图不显示,手机打开倒是能显示。
我是手机上不显示图片,电脑上能看到图片
我动态替换了压缩包media里面的图片,我试了名称不变,只要换图片就有问题,
qq_21955687 2019-08-02
  • 打赏
  • 举报
回复
引用 2 楼 as21665 的回复:
楼主解决了吗?我也是替换图片正常下载就是图不显示,手机打开倒是能显示。
我是手机上不显示图片,电脑上能看到图片
医手 2019-08-01
  • 打赏
  • 举报
回复
这个时候,就该我冒出来推销一下我的产品了 把freemarker扔了吧 换这个 https://github.com/bajie2/WordX
as21665 2019-07-31
  • 打赏
  • 举报
回复
楼主解决了吗?我也是替换图片正常下载就是图不显示,手机打开倒是能显示。
qq_38984067 2019-07-01
  • 打赏
  • 举报
回复

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧