2013-03-26 42 views
1

我在寫一個服務來通過使用傳遞給我的服務的json來返回一個xls文件。 我正在使用JAX-RS和WINk。 由於json傳遞給我的服務太複雜,無法成爲URL中的@QueryParam,所以我想使用@POST方法而不是@GET。如何使用post方法來實現寧靜的下載服務

現在的問題是: 如果我使用@GET,我知道我可以在瀏覽器中粘貼url來下載服務返回的文件,但是如果我使用@POST,我該如何下載服務返回的文件?

目標是當用戶向該服務發佈請求時,會彈出一個窗口詢問「打開」,「下載」或「取消」。

回答

2

最簡單的方法是使用HTML form

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

而且

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

作品像顯示Chrome和IE的保存對話框魅力。

+0

謝謝!這有助於 – doranT 2013-03-26 13:15:09

+0

另一種方式,特別是我喜歡我,是使用JQuery。看['jQuery.post()'](http://stackoverflow.com/a/3506018/870248)。 – 2013-03-26 15:18:33