image - 如何在play scala项目中查看本地目录中的图像

标签 image scala file playframework playframework-2.3

我是Play scala框架的新手,在我的项目中我有上传图像显示的功能code> 将上传的图像放入 view 页面。我已将上传的图像存储到系统的local文件夹中,并传递图像path 进入我的 view 页面,但它不显示图像。 我已将图像存储位置 path 作为 imagepath 传递,并使用以下代码来查看图像。

<img <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0a3a2b3ed90a2bfa5a4b5a3fe91a3a3b5a4a3feb1a4" rel="noreferrer noopener nofollow">[email protected]</a>(imagepath) class="responsive" >

注意:如果我将图像存储在projectname/public/images项目目录中,图像将显示在 View 页面中。请帮我解决这个问题。

最佳答案

您需要自己实现一项操作。正如我在Play Framework: Handling dynamic created files (images) in PRODUCTION mode中回答的那样

步骤如下 首先在您的路由文件中添加如下内容:

GET /user/images/:name controllers.Application.imageAt(name:String)

并在 imageAt 操作中编写一个简单的文件读取器,它返回流中的文件。同样,我的示例是用 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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b787968364b79647e7f6e78254a7b7b6762686a7f6264652562666a6c6e4a7f" rel="noreferrer noopener nofollow">[email protected]</a>(imageName)>

或者直接通过url

 <img src="/user/images/imageName">

关于image - 如何在play scala项目中查看本地目录中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32241954/

相关文章:

html - float 图像旁边的居中文本

scala - 我们可以在德鲁伊中进行转换吗

java - 无法使用 Files.walkFileTree java nio 打印目录内的子目录

c - 如何检查 C 中的文件是否被禁止?

python - 如何在 .cfg 文件中存储带 # 的单词

html - 如何修复 'Hover text to image, text keeps it'的原始位置和背景中的img变化'

Python PIL 图像保存

html - 将图像强制到特定位置

scala - 创建数据框时面对 "scala.MatchError: 1201 (of class java.lang.Integer)"

scala - 由with产生的集合类型