Image 如何在play scala项目中查看本地目录中的图像
我是玩scala框架的新手,在我的项目中,我有一个功能,可以Image 如何在play scala项目中查看本地目录中的图像,image,scala,file,playframework,playframework-2.3,Image,Scala,File,Playframework,Playframework 2.3,我是玩scala框架的新手,在我的项目中,我有一个功能,可以上传图像和显示上传的图像到查看页面。我已经将上传的图像存储到我系统的本地文件夹中,并将图像路径传递到我的查看页面,但它没有显示图像。 我已将图像存储位置path作为imagepath传递,并使用下面的代码查看图像 <img src=@routes.Assets.at(imagepath) class="responsive" > 注意:如果我将图像存储在projectname/public/imagesprojectd
上传图像和显示上传的图像到查看页面。我已经将上传的图像存储到我系统的本地文件夹中,并将图像路径传递到我的查看页面,但它没有显示图像。
我已将图像存储位置path
作为imagepath
传递,并使用下面的代码查看图像
<img src=@routes.Assets.at(imagepath) class="responsive" >
注意:如果我将图像存储在projectname/public/images
projectdirectory
中,图像将显示在查看页面中。请帮助我解决此问题。您需要自己执行操作。正如我回答的那样
这是步骤
首先,在routes文件中添加如下内容:
GET /user/images/:name controllers.Application.imageAt(name:String)
并编写一个简单的文件读取器,在其中返回流中的文件。同样,我的示例是用Java编写的,但您应该使用Scala对其进行归档
File imageFile = new File(ReportFileHelper.getImagePath());
if (imageFile.exists()) {
//resource type such as image+png, image+jpg
String resourceType = "image+"+imageName.substring(imageName.length()-3);
return ok(new FileInputStream(imageFile)).as(resourceType);
} else {
return notFound(imageFile.getAbsoluteFile());
}
之后,可通过反向路由器访问图像:
<img src=@routes.Application.imageAt(imageName)>
或者直接通过url
<img src="/user/images/imageName">
试试这个:@Thomas非常感谢Terscar,Thomas
你的答案都一样。谢谢