生成的docx图片显示不出来
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);