使用play framework和scala如何显示数据库中的图像路径

使用play framework和scala如何显示数据库中的图像路径,scala,playframework,Scala,Playframework,如何在play framework和scala应用程序的查看页面上显示图像, 从数据库的图像路径?我知道如何静态显示文件中的图像,但我不知道如何做到这一点。请所有知道答案的人在这里分享。您可以像这样在控制器上设置一些方法。 我有一个控制器Profile,我添加了一个方法getImage(stringid)从db返回此人的照片 public class Profile extends Controller { public static void getImage(String id)

如何在play framework和scala应用程序的查看页面上显示图像,
从数据库的图像路径?我知道如何静态显示文件中的图像,但我不知道如何做到这一点。请所有知道答案的人在这里分享。

您可以像这样在控制器上设置一些方法。 我有一个控制器
Profile
,我添加了一个方法
getImage(stringid)
从db返回此人的照片

public class Profile extends Controller {
    public static void getImage(String id)
        {
            models.Person person = models.Person .findById(id);
            if(person.photo!=null)
            {
                ByteArrayInputStream input = new ByteArrayInputStream(person.photo);
                renderBinary(input);
                return;
            }
            VirtualFile vf = VirtualFile.fromRelativePath("/public/images/no_photo.jpg");

            File f  = vf.getRealFile();
            renderBinary(f);
        }
    }
在你看来,

你可以用

<img src=@{Profile.getImage(person.id)}>


希望这对您有所帮助。:)

您可以像这样在控制器上放置一些方法。 我有一个控制器
Profile
,我添加了一个方法
getImage(stringid)
从db返回此人的照片

public class Profile extends Controller {
    public static void getImage(String id)
        {
            models.Person person = models.Person .findById(id);
            if(person.photo!=null)
            {
                ByteArrayInputStream input = new ByteArrayInputStream(person.photo);
                renderBinary(input);
                return;
            }
            VirtualFile vf = VirtualFile.fromRelativePath("/public/images/no_photo.jpg");

            File f  = vf.getRealFile();
            renderBinary(f);
        }
    }
在你看来,

你可以用

<img src=@{Profile.getImage(person.id)}>


希望这对您有所帮助。:)

不,它来自数据库。。。person.photo是字节格式。。然后使用ByteArrayInputStream(person.photo)加载;然后渲染为二进制以显示为图像,并可在视图上使用。如果数据库中没有图像,则此部分正在加载静态图像。。。VirtualFile.fromRelativePath(“/public/images/no_photo.jpg”);这是第一出戏吗?虚拟文件似乎不存在于Play 2的API中。不,它来自数据库。。。person.photo是字节格式。。然后使用ByteArrayInputStream(person.photo)加载;然后渲染为二进制以显示为图像,并可在视图上使用。如果数据库中没有图像,则此部分正在加载静态图像。。。VirtualFile.fromRelativePath(“/public/images/no_photo.jpg”);这是第一出戏吗?虚拟文件似乎不存在于Play 2的API中。