2015-12-15 34 views
1

我在我的項目中使用球衣API。我堅持要求下載文件的案例,但事實並非如此。我的代碼如下文件沒有得到下載使用JERSEY

@GET 
@Produces("application/download") 
public Response downloadFile(){ 
    String data = getDatas(); 
    ResponseBuilder response = Response.ok(data); 
    response.header("Content-Disposition","attachment;filename=UserData.txt"); 
    response.header("charset", "UTF-8"); 
    return Response.build(); 
} 

我已經添加了所有的包,路徑也很好。沒有錯誤來了。

當我調用這個API時,數據進入響應。我希望這些數據在文件中並且是可下載的格式。

我也試過@Produces(MediaType.APPLICATION_OCTET_STREAM)

請糾正我,如果我做錯

+0

可能重複,http://stackoverflow.com/questions/12239868/whats-the-correct-way-to-send-a -file-from-rest-web-service-to-client – svarog

回答

0

我想你是不是送你建立的響應。

首先您使用的是ResponseBuilder打造resposne

ResponseBuilder response = Response.ok(data); 
response.header("Content-Disposition","attachment;filename=UserData.txt"); 
response.header("charset", "UTF-8"); 

,那麼你正在返回靜態Response.build()(注意資金R)對象,裏面是空的

你應該返回response.build()

另外,您應該在您的方法中生成octet-stream註釋

@Produces(MediaType.APPLICATION_OCTET_STREAM) 

,而不是

@Produces("application/download") 

是指這樣一個問題:what's the correct way to send a file from REST web service to client?

+0

我只使用了response.build()。這是一個錯字錯誤,我也嘗試使用@Produces(MediaType.APPLICATION_OCTET_STREAM)。它沒有得到下載 – user3760732

相關問題