Set an excel file as response in REST Service

I used the below example to test. REST Api works, but it gives result in stream format. But,I expect result with file option. I used this example to ingrate with Angular JS framework.

@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();

  }

application/octet-stream is defined as "arbitrary binary data" in RFC 2046, so given that the file is an excel one change response.header("Content-Type","application/octet-stream"); for response.header("Content-Type","application/vnd.ms-excel");

take a look at this answer about octet stream mime type, might be useful, and this one about excel mime type.

链接地址: http://www.djcxy.com/p/46850.html

上一篇: 从byte []数组中为ashx创建一个excel文件以供客户端下载

下一篇: 在REST服务中设置一个Excel文件作为响应