play2.0文件下载

最近在做项目使需要将做好的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是指该目录下的所有文件)

原文地址:https://www.cnblogs.com/xinyuyuanm/p/2999203.html