gpt4 book ai didi

playframework-2.0 - 在浏览器中显示来自 Controller 的图像对象

转载 作者:行者123 更新时间:2023-12-04 22:12:50 26 4
gpt4 key购买 nike

我将 playframework 2.2.1 与 Java 一起使用,
我正在尝试传递 BufferedImage 或 ByteArray 或 ByteArrayInputStream
查看模板以便直接从内存中在浏览器中显示它,
无需保存到服务器存储。
在我的 View 模板中,我请求一个图像:

<img src="@{Application.getImage()}"/>

我的应用程序 Controller :

public static Result getImage() throws IOException{  
BufferedImage image = ImageIO.read(new File("public/img/1.jpg"));
//some image manipulations
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
return ok(baos.toByteArray()).as("image/jpg");
}

在生成的 html 中,我得到:

<img src="SimpleResult(200, Map(Content-Type -&gt; image/jpg))">

我找到了一些关于这个话题的信息( onetwothreefour ),
但它通常与旧版本或 Scala 版本的 Play 有关。
请提出任何建议或指出我的错误,
谢谢

最佳答案

您应该在 getImage() 属性中使用指向 Result 操作的路由,而不是其 src:

<img src='@routes.Application.getImage()'/>

顺便说一句:虽然我们不知道您对图像进行了什么样的处理,但有时将它们保存到光盘是有意义的 - 特别是如果它们有可能被重复使用。在这种情况下,您可以将其退回:
return ok(new File("/path/to/file"));

当然仍然使用 route 作为 src ;)

关于playframework-2.0 - 在浏览器中显示来自 Controller 的图像对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20317932/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com