简体   繁体   English

如何在play框架2.1中显示图像

[英]How to show images in play framework 2.1

public static void getImage(String fileName) {
        File file = new File( "~/" + fileName );
        response.contentType = "image/png";
        renderBinary( file );
    }

In this method response and renderBinary are showing errors 在此方法中,response和renderBinary显示错误

The code that you are using is for Playframework 1.x, and it is not compatible with the 2.x version. 您使用的代码是Playframework 1.x,它与2.x版本不兼容。 These two versions are totally different. 这两个版本完全不同。

As @rags mentioned, take a look at How to render a binary with play 2.0? 正如@rags所提到的那样,看看如何用play 2.0渲染二进制文件?

More explanations on Content-Type are available here: http://www.playframework.com/documentation/2.1.1/JavaResponse 有关Content-Type的更多说明,请访问: http//www.playframework.com/documentation/2.1.1/JavaResponse

public static Result getImage() throws IOException { File file = new File( "d:\\Images\\"+name+".jpg" ); public static Result getImage()throws IOException {File file = new File(“d:\\ Images \\”+ name +“。jpg”);

    return ok(org.apache.commons.io.FileUtils.readFileToByteArray(file)).as("image/jpeg");
}

Here I am using IOException because I am using file operations, this is another way used rather than renderBinary 这里我使用IOException因为我正在使用文件操作,这是另一种使用方式而不是renderBinary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM