0

I am having a situation where i receive a ms-word (docx) document as a stream/bytearray from a webservice.

I then try to recreate the file, giving it the same name and content as before.

If i compare the original file and the one created after the download, then they are identical.

However, when i try to open the new one in word i get an error, and if accept the riscs i can open it.

If i look at the properties af the file in windows, the new one is missing a lot of information.

Any one know how to recreate the properties so the file can be opened without errors?


Just an extra piece of information.. If i use .doc (word97-2003) documents all is working fine, only .docx documents are a problem (also .xlsx and all the office 2007-2010 documents).

This is my code creating the files..

private static void saveBytesAsFile(String path, String filename, byte[] data){
    try {
        File dir = new File(path);
        dir.mkdirs();
        OutputStream os = new FileOutputStream(path + "/" + filename);
        os.write(data);
        os.flush();
        os.close();
    } catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

I compared the original and the recreated file in notepad++, and got the result that they are identical.

This is how i see that some properties are missing.

Image of properties

These are the warnings i get from word:

If i press ok on the first and yes on the second i can open the document anyway.

Word Warnings

4

2 回答 2

0

如果您被要求“接受风险”,那么这听起来更像是从 Internet 下载文档时 Word 的默认行为,而不是错误。您可以从 Word 选项、信任中心更改 Word 行为(假设您使用的是 Word 2007 或更高版本)。

所以我怀疑缺少的属性是一个问题。可以通过在构建新文档之前更改系统时钟来更改您正在重新创建的文档的创建日期(基于先前文档的内容)。我不推荐这些步骤。

于 2013-07-01T23:06:07.503 回答
0

解决方案

事实证明,属性没有问题。

只是在提供文档数据的服务中的某个地方,在文件数据的末尾添加了一个额外的空白字符。

这导致预期文件长度与实际文件长度不匹配,因此办公组件在尝试打开文档时会抱怨。

这也阻止了文件的属性被解析。

令人讨厌的是,文件比较工具没有捕捉到这一点。(或者可能需要一些不被忽略的尾随空格配置。)

于 2013-08-30T14:35:25.263 回答