UTF-8 在实时服务器中不工作
UTF-8 not working in live server
我是 docx4j 的新手。我正在使用 docx4j 创建新的 .docx 文件。在将项目从 eclipse 部署到 tomcat 服务器时,我的代码运行良好。但它不适用于实时服务器。下面是我的代码:
String html = "<html><head><meta charset=\"UTF-8\"><title></title>"
+ "</head><body>"
+ "<div><div style='width:100%;display:inline; text-align:right;'>मितिः "
+new Date()+"</div><div class=\"block p-top-2\" style='display:block; float:left;'>"
+ "च.नं. "
+ "<span style=\"display: inline !important;\">"+10088+" - "+73 + "/" + 74
+ "</span></div></div></body></html>";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html"));
afiPart.setBinaryData(html.getBytes());
afiPart.setContentType(new ContentType("text/html"));
Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart);
afiPart.registerInContentTypeManager();
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId() );
wordMLPackage.getMainDocumentPart().addObject(ac);
wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html;charset=UTF-8");
wordMLPackage.save(new File("created_doc.docx"));
下面是我本地的截图:
下面是我的实时服务器的屏幕截图
我正在使用 Tomcat 和 Spring Framework.Project 使用 Maven 创建并部署在 windows 服务器 2012 r2 上。
我怀疑这可能是问题所在:
afiPart.setBinaryData(html.getBytes());
这将使用 platform-default 编码 - 这在您的本地计算机和服务器之间可能明显不同。我强烈建议您始终 指定编码。如果你想要 UTF-8,使用:
afiPart.setBinaryData(html.getBytes(StandardCharsets.UTF_8));
我是 docx4j 的新手。我正在使用 docx4j 创建新的 .docx 文件。在将项目从 eclipse 部署到 tomcat 服务器时,我的代码运行良好。但它不适用于实时服务器。下面是我的代码:
String html = "<html><head><meta charset=\"UTF-8\"><title></title>"
+ "</head><body>"
+ "<div><div style='width:100%;display:inline; text-align:right;'>मितिः "
+new Date()+"</div><div class=\"block p-top-2\" style='display:block; float:left;'>"
+ "च.नं. "
+ "<span style=\"display: inline !important;\">"+10088+" - "+73 + "/" + 74
+ "</span></div></div></body></html>";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html"));
afiPart.setBinaryData(html.getBytes());
afiPart.setContentType(new ContentType("text/html"));
Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart);
afiPart.registerInContentTypeManager();
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId() );
wordMLPackage.getMainDocumentPart().addObject(ac);
wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html;charset=UTF-8");
wordMLPackage.save(new File("created_doc.docx"));
下面是我本地的截图:
下面是我的实时服务器的屏幕截图
我正在使用 Tomcat 和 Spring Framework.Project 使用 Maven 创建并部署在 windows 服务器 2012 r2 上。
我怀疑这可能是问题所在:
afiPart.setBinaryData(html.getBytes());
这将使用 platform-default 编码 - 这在您的本地计算机和服务器之间可能明显不同。我强烈建议您始终 指定编码。如果你想要 UTF-8,使用:
afiPart.setBinaryData(html.getBytes(StandardCharsets.UTF_8));