简体   繁体   English

如何使用发布方式而不是实现宁静的下载服务

[英]How to use post method instead of get to realize restful downloading service

I am writing a service to return a xls file by using the json passed to my service. 我正在编写服务,以使用传递给我的服务的json返回xls文件。 I am using JAX-RS and WINk. 我正在使用JAX-RS和WINk。 Since the json passed to my service is too complex to be a @QueryParam in the url, so I want to use @POST method instead of @GET. 由于传递给我的服务的json太复杂了,无法在URL中成为@QueryParam,因此我想使用@POST方法而不是@GET。

The question is: If I use @GET, I know I can paste the url in the browser to download the file returned by service, but if I use @POST, how can I download the file returned by the service? 问题是:如果使用@GET,我知道可以将URL粘贴到浏览器中以下载服务返回的文件,但是如果使用@POST,如何下载服务返回的文件?

The goal is when user post the request to this service, a window will pop up asking either"OPEN", "Download" or "Cancel". 目的是当用户将请求发布到该服务时,将弹出一个窗口,询问“ OPEN”,“ Download”或“ Cancel”。

The most simple way is to use a HTML form : 最简单的方法是使用HTML form

<form action="rest/report/users" method="post">
ID: <input type="text" name="id"><br>
<input type="submit">
</form>

And

@Path("/report")
public class ReportResource {

    @Path("users")
    @POST
    @Produces(MediaTypeUtils.MS_EXCEL)
    public Response getUsers(@FormParam("id") String id ) {

        // Build the report and get the instance of java.io.File

        ResponseBuilder response = Response.ok(file);
        response.header("Content-Disposition","attachment; filename=report.xls");
        return response.build();
    }
}

The works like a charm showing the save dialog in Chrome and IE. 作品就像一个魅力一样显示在Chrome和IE中的“保存”对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM