2014-10-18 126 views
1

我用下面的例子来测试。 REST Api可以工作,但它会以流格式提供结果。 但是,我期待与文件选项结果。我使用这个例子来嵌入Angular JS框架。在REST服务中设置一个excel文件作为响应

@GET 
    @Path("/get") 
    @Produces("application/vnd.ms-excel") 
    public Response getFile() { 
    LOG.info("get - Function begins from here"); 

    File file = new File("/home/test/file.xls"); 

    ResponseBuilder response = Response.ok((Object) file); 
    response.header("Content-Disposition", 
     "attachment; filename=new-excel-file.xls"); 
    response.header("Content-Type","application/octet-stream"); 
    return response.build(); 

    } 
+0

**,我发现这里的解决方案,以我的问题 http://stackoverflow.com/questions/22447952/angularjs-http-post-convert-binary-to-excel-file-and-download** – Srini 2014-11-11 12:16:59

回答

1

应用/八位字节流在RFC 2046定义为“任意的二进制数据”,所以给出该文件是为response.header("Content-Type","application/vnd.ms-excel");

看一看this answer约字节流MIME一个excel一个变化response.header("Content-Type","application/octet-stream");类型,可能是有用的,并且关于excel mime类型的this one

+0

是,感谢您的快速回答。我已经删除了** response.header(“Content-Type”,“application/octet-stream”); **并尝试过,但仍然没有提供文件保存对话框。 – Srini 2014-10-20 09:35:17

+0

@ user1702169我相信如何处理文件的行为是通过浏览器进行处理的,而不是通过您可以在API的服务器端设置的内容进行处理。 – th3morg 2014-10-20 17:58:41

+0

@ th3morg确定你可以,只需设置Content-Disposition标题,就像这个答案链接到的第一个答案一样。 – 2014-12-12 19:54:26