简体   繁体   English

Java如何在DEV环境中将图像作为资源访问

[英]Java how to access image as resource in DEV environment

My question seems to be a general problem but actually it is not. 我的问题似乎是一个普遍的问题,但实际上不是。

We have a DEV environment, in which we have the FrontEnd running that is communicating with the BE. 我们有一个DEV环境,其中正在运行与BE通信的FrontEnd。 If the user is on the FE and gives inputs like name, surname, city and zipcode, then based on these inputs the Backend will generate an QR Code as png file. 如果用户在FE上并提供诸如姓名,姓氏,城市和邮政编码之类的输入,则后端将基于这些输入生成QR码(png文件)。

This png file is saved locally in src/main/.... 此png文件保存在本地src / main / ....

The problem: How can I access this file so that I can show the QR Code to the user in the FE.This image that is located on my HDD should be delivered to the FE. 问题:如何访问此文件,以便可以在FE中向用户显示QR码。位于HDD上的此图像应交付给FE。

What I have tried: 我尝试过的

How to reference a local image in java? 如何在Java中引用本地图像?

Trying to cast an Image to a BufferedImage 尝试将图像投射到BufferedImage

ClassPathResource does not get the classpath ClassPathResource无法获取类路径

Nothing worked. 没事。 The image won't shown. 图片不会显示。

You should not be saving files under src/main/... directory. 您不应将文件保存在src/main/...目录下。 This location is valid for only during local development when you are using a build tool like Maven. 该位置仅在使用Maven之类的构建工具时仅在本地开发期间有效。 When the application get's packaged this directory is no longer present. 打包应用程序获取包时,此目录不再存在。

You want to save the image somewhere on the hard drive eg using File.createTempFile() . 您想将图像保存在硬盘上的某个位置,例如使用File.createTempFile() You most likely want to create a new API in the backed to return this file eg for POST /api/user/{id}/qrcode you want to respond with Content-type: image/png and the QR code image. 您最有可能希望在支持中创建新的API以返回此文件,例如对于您要以Content-type: image/png和QR码image进行响应的POST /api/user/{id}/qrcode

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

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