您的位置:首页 > 其它

Download excel file from JAX-RS

2015-08-27 11:48 351 查看
In JAX-RS, for excel file, annotate the method with
@Produces("application/vnd.ms-excel")
:

Put
@Produces(“application/vnd.ms-excel”)
on service method.

Set “
Content-Disposition
” in Response header to prompt a download box.

1. Download Excel file in JAX-RS

Full example to download an excel file from JAX-RS.

import java.io.File;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

@Path("/excel")
public class ExcelService {

private static final String FILE_PATH = "c:\\excel-file.xls";

@GET
@Path("/get")
@Produces("application/vnd.ms-excel")
public Response getFile() {

File file = new File(FILE_PATH);

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

}

}


2. Demo

Access this URI pattern : “
/excel/get
“.

Figure : Excel file “
c:\\excel-file.xls
” from server is prompted for user to download, with a new file name “
new-excel-file.xls


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: