Playframework 播放框架下载文件

Playframework 播放框架下载文件,playframework,playframework-1.x,Playframework,Playframework 1.x,我是游戏框架中的新手,我希望通过以下方式下载文件: 从数据库中获取文件路径。 单击该路径可以在web表单中打开文件。 有可能在游戏框架内进行吗?如果有,请给我一些想法 谢谢如果您指的是服务器上的文件路径,那么这很简单。只需使用renderBinary方法。这可能需要输入流或文件对象,因此 renderBinary(new File(filepath)); 应该能做到这一点。举个例子 File file = new File("file pathfr"); return ok(file); 使

我是游戏框架中的新手,我希望通过以下方式下载文件:

从数据库中获取文件路径。 单击该路径可以在web表单中打开文件。 有可能在游戏框架内进行吗?如果有,请给我一些想法


谢谢

如果您指的是服务器上的文件路径,那么这很简单。只需使用renderBinary方法。这可能需要输入流或文件对象,因此

renderBinary(new File(filepath));
应该能做到这一点。

举个例子

File file = new File("file pathfr");
return ok(file);
使用JPA的模型

@Lob
@Column(name="NY_FILE",length=100000, nullable=false)
private byte[] myFile;

@Column(name="MIME_TYPE", nullable=false)
private String mimeType;
控制器

@play.db.jpa.Transactional(readOnly=true)
public Result download(Long id){
    myPOJO = arquivo.findByid(JPAapi.em(), id);
    return ok(MyPOJO.getMyFile()).as(myPOJO.getMimeType());
}
路由文件

GET     /download/:id   @controllers.MyController.download(id:Long)
HTML链接

<a id="linkDownload" href="/download/@myPOJO.getMyFile.getId" target="_blank">
   @myPOJO.getMyFile.getName
</a>

使用play controller可以轻松下载文件,如下所示:

public Result downloadFile(){
    File fileToDownload = new File("/path/fileToDownload.pdf"):

    response().setContentType("application/x-download");
    response().setHeader("Content-disposition", "attachment; filename=fileToDownload.pdf");

    return ok(fileToDownload);
}
斯卡拉路


请考虑用你的解决方案添加一些解释,这样每个人都可以向你学习!
def downloadFile = Action {
  Ok.sendFile(
  content = new java.io.File("/path/fileToDownload.pdf"),
  fileName = _ => "fileToDownload.pdf"
  )
}