JAX-RS 文件下载,多种内容类型

JAX-RS file downloads, multiple content types

让我先提供一些背景信息。我正在开发一个与 Microsoft SharePoint 2010 集成的系统,这并不是真正的 SharePoint 作为系统,而是它的文件系统、文档库等的虚拟表示...用户将文件上传到 SharePoint,我的系统监视这些文件并为它们编制索引进入搜索引擎(包括文件内容)。用户可以通过 REST 接口与该系统进行交互。

我已经创建了一个 REST 接口来为我的搜索引擎中某个条目对应的用户获取文件。这使用绝对网络路径作为其标识符。一个例子是 //corporateserver//library1/filex.docx。由于同源政策,我无法在客户端加载此文件。因此我试图通过服务器传输它。

我使用 JAX-RS 传输数据取得了一些成功,但是,我遇到了以下问题:

用户希望下载的文件可以是多种内容类型,其中大部分是microsoft office格式。我查看了注册的 MIME 类型列表,发现 application/mswordapplication/vnd.ms-powerpoint

我的问题:是否有包含 Microsoft Office 文件的内容类型?如果不是,如何才能将正确的内容类型与所请求的文件相匹配。如果服务器内容类型为 text/plain 的 word 文件会怎样?

如能提供有关此主题的任何帮助,我们将不胜感激。

编辑

我用来传输数据的代码:

package com.fujitsu.consensus.rest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

import org.apache.commons.io.IOUtils;
import org.codehaus.jettison.json.JSONException;

@Path("/fetcher")
public class FetcherService {

    @GET
    @Path("/fetch")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response fetchFile(@QueryParam("path") String path) 
        throws JSONException, IOException {

        final File file = new File(path);
        System.out.println(path);

        StreamingOutput stream = new StreamingOutput() {
            @Override
            public void write(OutputStream output) throws IOException {
                try {
                    output.write(IOUtils.toByteArray(new FileInputStream(file)));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM)
            .header("Content-Disposition", "inline; filename=\"" + file.getName() + "\"") 
            .build();
    }
}

JavaScript代码:

 $.ajax({
   url: "../rest/fetcher/fetch",
   type: "GET", //send it through get method
   data:{path:obj.id},
   success: function(response) {
   console.log(response);},
   error: function(xhr) {//Do Something to handle error}
 });

我在客户端得到的响应:

编辑 2

我添加了 HTTP 跟踪作为 headers 和数据实际上正在传输的证据,但未显示下载对话框。

Content-Disposition header 似乎无法使用内联或附件。

您可以使用 application/octet-stream 作为内容类型并执行以下操作来下载文件:

@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadFile(String fileName) {
    File file = ... // Find your file
    return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
        .header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"")
        .build();
}

由于您正在使用 JavaScript 下载文件,请查看 here and here

修复它,使用 content-type:application/octet-stream。我还添加了上面提到的header:

 return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
        .header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"")
        .build();

我的错误是认为在 ajax 调用后文件将在同一个 window 中下载。更改了我的客户端代码以在另一个 window 中执行请求,使用:

window.open(resturl);

反应是浏览器会打开一个新的 window,将文件下载到下载托盘中,然后 return 到您单击下载的网页,同时关闭下载选项卡。 (大约 0.2 秒)。