简体   繁体   中英

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. 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.

This png file is saved locally in 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.

What I have tried:

How to reference a local image in java?

Trying to cast an Image to a BufferedImage

ClassPathResource does not get the classpath

Nothing worked. The image won't shown.

You should not be saving files under src/main/... directory. This location is valid for only during local development when you are using a build tool like 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() . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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