简体   繁体   English

如何从文件夹中的 JAVA 中的 rest API 中将图像返回到浏览器?

[英]How to return an Image to browser in rest API in JAVA from folder?

I have a folder in my NetBeans project with images.我的 NetBeans 项目中有一个带有图像的文件夹。 I have a H2 database with a "properties" table.我有一个带有“属性”表的 H2 数据库。

The images are named after a column in the properties table for convenience.为方便起见,图像以属性表中的列命名。 This is my code so far.到目前为止,这是我的代码。

@PostMapping(value = "/image/large/{id}", produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<Image> getPicture(@PathVariable long id)throws Exception{
    System.out.println(id);
                //System.out.println(barcode);
                
    Properties prop1 = new Properties();
    prop1 = propService.findOne(id);
    
    String filepath = prop1.getPhoto();
    
    String img = "static/images/"+filepath;
    
    return img;
}

How can I implement this in my rest controller?如何在我的 rest controller 中实现这一点? Struggling to find a correct way to implement this, any help appreciated.努力寻找正确的方法来实现这一点,任何帮助表示赞赏。

From the code you have provided, you can return a string representing an image location or path.从您提供的代码中,您可以返回一个表示图像位置或路径的字符串。 This path can then be used in am image <img /> tag.然后可以在 am image <img />标记中使用此路径。

The second option is to read your file using an inputstream and convert the image to base46 (which you then return to the client).第二种选择是使用输入流读取文件并将图像转换为 base46(然后返回给客户端)。

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

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