最近在做項目使需要將做好的excel表格提供下載,廢話不多說,貼代碼:
public static Result view() {
String filename="sims.xls";
File file = new File(Play.application().path().getAbsolutePath() + "/public/images/" + filename);
response().setContentType("application/x-excel");//可選擇不同類型
response().setHeader("Content-Disposition", "attachment; filename=" + filename);
return ok(file);
}
爲了說明問題,我直接使用了
GET /assets/*file controllers.Assets.at(path="/public", file)
如果想更換目錄,可新建一個目錄並仿照assets文件夾的配置方式配置(*file是指該目錄下的所有文件)