在JAX-RS中,对于excel文件,请使用@Produces("application/vnd.ms-excel")
注释该方法:
- 将@Produces(“ application / vnd.ms-excel”)放在服务方法上。
- 在Response标头中设置“ Content-Disposition ”以提示下载框。
1.在JAX-RS中下载Excel文件
从JAX-RS下载excel文件的完整示例。
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.演示
访问此URI模式:“ / excel / get ”。
图片:服务器提示用户从服务器下载Excel文件“ c:\\ excel-file.xls ”,新文件名为“ new-excel-file.xls ”
下载源代码
下载它– JAX-RS-Download-Excel-File-Example.zip (6 KB)
参考文献
翻译自: https://mkyong.com/webservices/jax-rs/download-excel-file-from-jax-rs/