从SQL数据库中使用Spring Boot下载图像文件。

huangapple go评论77阅读模式
英文:

downloading image file from sql database using Springboot

问题

在我的项目中,我需要从SQL数据库中提取数据并将其存储到一个表中。
除了图像文件,我能够提取所有数据。所以我现在有了路径

image.path.idProofPhoto=/opt/static/images/idProofPhoto

我需要从那里提取数据。

好的,现在我做了什么

我必须将数据提取为这样的格式 "id_imagename.jpeg"
其中 idJPEG 来自数据库中存储的数据,id 是员工的 id,jpeg 是文件的扩展名也被存储了。
现在我提取了 id 和扩展名,现在我的文件名看起来像 "1536_alex.jpeg"
在这个名称中,该文件在我们的数据库中,路径是
image.path.idProofPhoto=/opt/static/images/idProofPhoto

所以现在我的问题是如何获取这些图像并如何显示?

有人可以帮帮我吗?

英文:

In my project, I need to fetch data from SQL database and store it to a table.
I am able to fetch all the data except the image files. So I have now
the path

image.path.idProofPhoto=/opt/static/images/idProofPhoto

I need to fetch data from there

ok now what I did

I had to fetch the data as a format like "id_imagename.jpeg"
where id and JPEG are from the data stored in the database and id the id of employee and jpeg is the extension name of that file is also stored.
now I fetched the id and the extension now my file is something like "1536_alex.jpeg"
In this name, the file is in our database and the path is
image.path.idProofPhoto=/opt/static/images/idProofPhoto

So now my problem is how can I get those images and how to display?

can anyone help me with this?

答案1

得分: 1

你可以使用Spring图像资源来提供图像。我建议不要从本地服务器位置提供内容,而是采用以下方法:

  1. 使用CDN网址/云存储桶网址来提供此图像内容,并为网址设置预签名的有限到期时间。

  2. 这将使您的图像快速呈现,避免单一故障点,同时也将成为您图像的生命周期管理方式。

  3. 从您的数据库中存储和检索图像网址。创建一个新列并保存图像路径,通常使用一些随机图像链接,只是为了避免猜测网址。不要创建可猜测的网址,因为任何人都可以猜测并创建网址来检索内容。

如果您有无法在云上托管的内部应用程序,那么您可以使用Spring Boot,然后参考这个链接:http://zetcode.com/springboot/serveimage/。

英文:

You can use Spring image resource to serve images. I would suggest not to serve content from the local server location instead follow below approach.

  1. Use CDN url/ Cloud bucket storage URL to serve this image content with pre-signed limited expiry for the URLS.

  2. This will make your image to render really quick and avoid single point of failure also it will be you lifecycle management for your images.

  3. Store and retrieve image URL's from your database. Create a new column and save image path mostly with some random image links, just to avoid guessing of the URL's. Don't create URL's which are guessable as anybody can guess and create URL to retrieve content.

If you have internal application where you can't host it on cloud then you can use Spring boot then refer to this http://zetcode.com/springboot/serveimage/

huangapple
  • 本文由 发表于 2020年8月30日 20:20:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63657361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定