JAX-RSでは、excelファイルのために、メソッドに `@Produces(" application/vnd.ms-excel ")`の注釈を付けます:
-
@ Produces( "application/vnd.ms-excel") をサービスメソッドに置きます.
-
レスポンスヘッダに " 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();
}
}
デモ
このURIパターンにアクセスしてください: " /excel/get "。
__Figure:新しいファイル名 " new-excel-file.xls "を使用して、サーバーからのExcelファイル " c:\\ excel-file.xls
ソースコードをダウンロードする
ダウンロードする - JAX-RS-Download-Excel-File-Example.zip (6 KB)
参考文献
@Produces JavaDoc]。 http://en.wikipedia.org/wiki/Internet media type#Type__multipart—list
アプリケーションタイプの一覧]