JAVA WOPI : What should be the return type of GetFileContent API?

标签 java ms-office ms-wopi

我已在 Office 365 云存储合作伙伴计划下使用 Microsoft Office Online 客户端成功实现了 JAVA WOPI Host

现在我想使用自定义 WOPI 客户端实现相同的流程(Office Web Apps 服务器)。
我假设 wopi 主机代码的变化很小。到目前为止,这些是我已经实现的事情:

  • 已部署 Office Web Apps Server在 Windows Server 2012 R2 上。
  • 启动 WOPI 主机
  • GetFileInfo API 是第一个应该被调用的 API,是的,它正在被调用,并且我可以看到具有正确值的 JSON。
  • GetFileContent API 也在 GetFileInfo API 调用后立即被调用,但我在浏览器上看不到该文件。

enter image description here


下面是获取文件内容的 JAVA 代码片段,我可以看到该方法被调用并以字节流形式发送回文件内容,但我在浏览器上看不到该文件。

    @GET
    @Path("/{fileName}/contents")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response getFileContent(@PathParam("fileName") String fileName,@Context HttpServletRequest httpRequest) {

    final String filePath = "C:/wopi-docs/"+fileName;
    File file = new File(filePath);

    byte[] content = null;
     try {  

        content = FileUtils.readFileToByteArray(file);      

    } catch (IOException e) {
        e.printStackTrace();
    }
     return Response.status(Response.Status.OK).entity(new ByteArrayInputStream(content)).build();
} 

这与我们在 Office Online 中使用时有效但不适用于 Office Web Apps Server 时的方法相同。

任何输入或指针将不胜感激。

最佳答案

Optional<java.nio.file.Path> first = findFile(fileId);
        if (first.isPresent()) {
            return Response.ok(new File(first.get().toUri()), APPLICATION_OCTET_STREAM).build();
        } else {
            return Response.status(Status.BAD_REQUEST.getStatusCode(), "No file found with the " +
                    "name " + fileId).build();
        }

private Optional<java.nio.file.Path> findFile(String fileName) throws IOException {
    File filePath= getPath();

    try (Stream<java.nio.file.Path> stream = Files.find(filePath, 1, (path, attr) -> path.getFileName().toString().toLowerCase().equals(fileName.toLowerCase()))) {
        return stream.findFirst();
    }
}

这段代码对我有用。你可以试试这个

关于JAVA WOPI : What should be the return type of GetFileContent API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37432724/

相关文章:

c# - Nodejs 中的 WOPI 主机实现?

ms-wopi - WOPI 主机实现问题

java - 如何在 Smalltalk 中访问和使用 Java 库/包

java - 逗号运算符在 "eval()"中做什么?

java - 使用java运行时(在Windows机器上)将Linux命令发送到Linux服务器

hyperlink - 如何使用 tika 从办公文档中提取超链接

javascript - 什么是 WOPI 操作 URL?

Java-Eclipse : Externalize Strings?

c# - 使用 Microsoft.Office.Interop 打开文件时如何禁用弹出窗口

excel - 使用宏或添加项覆盖内置 Excel 函数