【问题标题】:Unexpected character encountered while parsing value when validating content as JSON in WOPI在 WOPI 中将内容验证为 JSON 时解析值时遇到意外字符
【发布时间】:2026-02-22 10:05:02
【问题描述】:

我目前正在使用我的应用程序实现 WOPI。我们的域已经被Microsoft 列入白名单。在实施过程中,我目前面临以下两个问题:

  1. 尝试将内容验证为 JSON 时引发异常:“解析值时遇到意外字符。”我正在发送响应“Value=application/octet-stream”,但我不明白为什么服务器试图将流解析为 JSON。
  2. 在来自“iframe”的每个新请求都在 JAVA 中启动新会话之后。

这里有更多细节:

我当前的 URL 是 https://onenote.officeapps-df.live.com/hosting/WopiTestFrame.aspx?ui=en-US&rs=en-US&dchat=1&hid=26D7CA2A10F60A68720106BF599F84B9&&WOPISrc=https://domain/wopiEditor/files/73346e47-697b-11e6-a8bc-c26cd8f74b91/courses/independentConcepts/concept_adminGlo_5/assets/Setting url for static ip.docx&access_token=DEADBEEFDEADBEEFDEADBEEF&access_token_ttl=1532765580679

我的Java代码如下:

public void getFile(HttpServletRequest request, HttpServletResponse response, String name) {
        Println.getInstance().log(request.getSession().getId() + "re" + request.getRequestURI());
        InputStream fis = null;
        OutputStream toClient = null;
        try {
            String path = getFilePath(request) + name;
            File file = new File(path);
            String filename = file.getName();
            // XWPFDocument xDoc = new XWPFDocument(OPCPackage.open(fis));
            fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            response.reset();

            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("utf-8"), "ISO-8859-1"));
            response.addHeader("Content-Length", "" + file.length());
            response.addHeader("Content-Type", "" + "application/octet-stream");
            //Println.getInstance().log(file.length() + "l" + file);
            toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);

            toClient.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                fis.close();
                toClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

测试帧图片附

【问题讨论】:

    标签: java jsp tomcat9 ms-wopi


    【解决方案1】:

    您看到的错误出现在 CheckFileInfo 请求上,该请求应该以 JSON 形式返回。您提供的 Java 代码段用于 getFile 请求,该请求是从 Office Online 服务器发出的单独调用。您应该查看 https://wopi.readthedocs.io/projects/wopirest/en/latest/ 以了解如何编写您的实现。

    【讨论】:

    • 不,实际上我在每个请求上都收到此错误,除了开始 4 个主机框架。所以你是说每个请求都需要单独的回调。
    • 另一件事你没有回答第二个问题,为什么要创建新会话
    【解决方案2】:

    一个想法是您可能需要更具体地设置 Content-Type 标头而不是您发送的 application/octet-stream?

    还有很多其他你应该返回的标头值,其中一些可能也很重要:

    https://wopi.readthedocs.io/projects/wopirest/en/latest/common_headers.html#common-headers

    【讨论】: