docx格式附件下载后成为zip文件

office 2007各种格式附件下载后变成zip文件问题解决方法
从IIS或者Tomcat之类的Web服务器通过二进制流下载文件时(比如通过设置Header为Content-disposition:inline),如果被下载的文件是Office2007的dotx,docx,pot等后缀的时候,下载对话框中的图标会变成ZIP的,相应的文件名也会成为zip为扩展后缀的文件。其实出现这样的情况,是由于IE中对下载文件类型的判断,假如找不到相应后缀的MIME的话,会自动根据数据流来判断文件的类型,而微软对Office2007的格式做了和以前完全不同的处理。

以前的Office文档是100%的二进制格式。第三方的工具操作起来非常不方便,而 Office2007从整体上都是基于XML格式的,这里并不是说Office2007文档可以保存成XML格式。而是Office2007默认的文档格式就是XML的(Word的docx、Excel的xlsx等)。也许有人会感到奇怪,用文本编辑器打开docx后,显示的仍然是二进制格式,并不是什么 XML。其实docx并不是普通的XML格式,当然,也不只是一个XML文件,docx本质上是一个zip文件,里面有一系列的xml、目录和其他的文件。如果我们将docx改成zip。就可以用winzip等软件将其解开。

正是由于这样的原因,当MIME中不存在相对应的Office2007类型的情况下,被自动识别为ZIP文件了。

解决的方法:

tomcat->conf->web.xml中增加如下代码就可以了

  <mime-mapping>
  <extension>docx</extension>
  <mime-type>application/vnd.openxm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java和LibreOffice(UNO API)可以将docx文件转换为pdf文件。下面是实现此操作的步骤: 1. 首先,需要安装LibreOffice软件并启动它。 2. 然后,使用Java代码连接到LibreOffice的UNO API。 3. 接下来,使用UNO API打开docx文件。 4. 然后,使用UNO API将docx文件转换为pdf文件。 5. 最后,关闭docx文件和LibreOffice。 下面是一个简单的Java代码示例,演示如何使用LibreOffice(UNO API)将docx文件转换为pdf文件: ```java import com.sun.star.beans.PropertyValue; import com.sun.star.frame.XComponentLoader; import com.sun.star.lang.XComponent; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; import com.sun.star.util.XCloseable; public class DocxToPdfConverter { public static void main(String[] args) { String inputFile = "input.docx"; String outputFile = "output.pdf"; XComponentContext context = null; XComponentLoader loader = null; XComponent document = null; try { // Connect to LibreOffice UNO API context = com.sun.star.comp.helper.Bootstrap.bootstrap(); loader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, context.getServiceManager().createInstanceWithContext("com.sun.star.frame.Desktop", context)); // Open docx file PropertyValue[] propertyValues = new PropertyValue[1]; propertyValues[0] = new PropertyValue(); propertyValues[0].Name = "Hidden"; propertyValues[0].Value = Boolean.TRUE; document = loader.loadComponentFromURL("file:///" + inputFile, "_blank", 0, propertyValues); // Convert docx to pdf PropertyValue[] convertProperties = new PropertyValue[2]; convertProperties[0] = new PropertyValue(); convertProperties[0].Name = "FilterName"; convertProperties[0].Value = "writer_pdf_Export"; convertProperties[1] = new PropertyValue(); convertProperties[1].Name = "Overwrite"; convertProperties[1].Value = Boolean.TRUE; ((com.sun.star.frame.XStorable) UnoRuntime.queryInterface(com.sun.star.frame.XStorable.class, document)).storeToURL("file:///" + outputFile, convertProperties); // Close docx file and LibreOffice ((XCloseable) UnoRuntime.queryInterface(XCloseable.class, document)).close(true); document.dispose(); context.getServiceManager().createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", context).queryInterface(XComponent.class).dispose(); context.dispose(); } catch (Exception e) { e.printStackTrace(); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值