JAX-RS文件下载,多种内容类型 [英] JAX-RS file downloads, multiple content types

查看:189
本文介绍了JAX-RS文件下载,多种内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let me provide some context first. I am working on a system that integrates with Microsoft SharePoint 2010, well not really SharePoint as a system but the virtual representation of it's filesystem, document libraries, etc... Users upload files to SharePoint, and my system monitors these files and indexes them into a search engine (including file content). User can interact with this system by means of REST interfaces.

我创建了一个REST接口来为用户获取文件,该文件对应于我的搜索引擎中的某个条目.这使用绝对网络路径作为其标识符.一个示例是//corporateserver//library1/filex.docx.由于相同的原始策略,但是我无法加载此文件客户端.因此,我正在尝试通过服务器进行传输.

I have created a REST interface to fetch a file for the user corresponding a certain entry in my search engine. This uses the absolute network path as its identifier. An example would be //corporateserver//library1/filex.docx. Due to the same origin policy however I can not load this file client side. Therefore I am trying to transmit it via the server.

我使用JAX-RS传输数据取得了一些成功,但是,我陷入了以下困境:

I had some success using JAX-RS to transmit data, however, I am getting stuck at the following:

用户希望下载的文件可以是多种内容类型,其中大多数是Microsoft Office格式.我浏览了已注册的MIME类型的列表,并发现了application/mswordapplication/vnd.ms-powerpoint

The file the user wishes to download can be of mutliple content types, most of them will be microsoft office formats. I had a look through the list of registered MIME types and came across application/msword or application/vnd.ms-powerpoint

我的问题:是否有包含Microsoft Office文件的内容类型?如果不是,那么如何继续将正确的内容类型与所请求的文件进行匹配.如果将一个内容类型为text/plain的word文件放入服务器,将会发生什么情况?

My question: is there a content type that would include Microsoft Office files? If not, how could one proceed to match the correct content types with a file that is being requested. What would happen if one would server a word file with content type text/plain?

在此问题上的任何帮助将不胜感激.

Any help on the subject would be greatly appreciated.

编辑

我用来传输数据的代码:

The code I use to transmit data:

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代码:

JavaScript code:

 $.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}
 });

我在客户端得到的响应:

The response I get on client side:

编辑2

我添加了HTTP跟踪,以证明实际上已传输了头和数据,但是未显示下载对话框.

I've added a HTTP trace as proof that the headers and data are in fact being transmitted, the download dialogue however is not shown.

Content-Disposition标头似乎无法与内联或附件一起使用.

The Content-Disposition header does not appear to be working with either inline or attachment.

推荐答案

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

You could use application/octet-stream as content type and do the following to download files:

@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下载文件,因此请在此处

Since you are using JavaScript to download files, have a look here and here.

这篇关于JAX-RS文件下载,多种内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆