简体   繁体   English

如何在 Flutter+Spring Boot+PostgreSQL+Heroku 结构中发送和存储图像?

[英]How to send and store image in a Flutter+Spring Boot+PostgreSQL+Heroku structure?

I am developing a mobile application with Flutter framework.我正在使用Flutter框架开发移动应用程序。

In backend API side, I use Spring Boot framework and deploy it to the Heroku (free-plan).在后端 API 端,我使用Spring Boot框架并将其部署到Heroku (免费计划)。

In database side, I use PostgreSQL add-on in Heroku .在数据库方面,我在Heroku中使用PostgreSQL插件。

Everything okay before working with the images.在处理图像之前一切正常。 I am confused when I need to send image to server and store it.当我需要将图像发送到服务器并存储它时,我感到很困惑。 What is best practice of it?它的最佳实践是什么? I saw two option after the some searching.经过一番搜索,我看到了两个选项。 These are:这些是:

First Option第一个选项

  1. In Flutter side, take the image from the userFlutter端,取用户的图像
  2. In Flutter side, convert the image to the BASE64 string format.Flutter端,将图像转换为BASE64字符串格式。
  3. In Flutter side, POST it as a JSON object to backend.Flutter端,将其作为POST JSON发布到后端。
  4. In Spring Boot side, get the BASE64 string and store it to the PostgreSQL db.Spring Boot端,获取BASE64字符串并将其存储到PostgreSQL db。

Second Option第二种选择

  1. In Flutter side, take the image from the userFlutter端,取用户的图像
  2. In Flutter side, convert the image to the BASE64 string format.Flutter端,将图像转换为BASE64字符串格式。
  3. In Flutter side, POST it as a JSON object to backend.Flutter端,将其作为POST JSON发布到后端。
  4. In Spring Boot side, get the BASE64 string and convert it to the real image file;Spring Boot端,获取BASE64字符串并转换为真实镜像文件;
  5. In Spring Boot side, save the actual image file into the file system of hosting machine and store path of the image to the PostgreSQL db.Spring Boot端,将实际镜像文件保存到主机文件系统中,并将镜像路径存储到PostgreSQL db。 (But Heroku doesn't allow writes on its filesystem) (Even it is possible to write on its filesystem, Every new deployment, the images would be gone) (但 Heroku 不允许在其文件系统上写入)(即使可以在其文件系统上写入,每次新部署,图像都会消失)

if I choose second option, what should I do for solving the saving image in file system of Heroku ?如果我选择第二个选项,我应该如何解决Heroku文件系统中的保存图像?

Which option should I use?我应该使用哪个选项?

Are there any another good option?还有其他好的选择吗?

Saving images in your database is usually not recommended.通常不建议将图像保存在数据库中。 Instead you could try hosting on another platform (for example AWS) that does allow file storage.相反,您可以尝试在另一个允许文件存储的平台(例如 AWS)上托管。

However, if you do not have too many images and won't access them very often, you can store them in the database.但是,如果您没有太多图像并且不会经常访问它们,则可以将它们存储在数据库中。 Instead of the first option however, I recommend letting Spring boot convert your BASE64 string to an actual image.然而,而不是第一个选项,我建议让 Spring 引导将您的 BASE64 字符串转换为实际图像。 You can then store this image as a BLOB in your database.然后,您可以将此图像作为 BLOB 存储在数据库中。 This makes sure the database optimizes for BLOBs and doesn't create indexes and other optimizations for text entries.这可确保数据库针对 BLOB 进行优化,并且不会为文本条目创建索引和其他优化。

i think you should:我觉得你应该:

  1. take the image from the user in the flutter app;在 flutter 应用程序中从用户那里获取图像;
  2. convert the image in the base64 format;将图像转换为 base64 格式;
  3. send to the backend by rest api in json format;通过 rest api 以 json 格式发送到后端;
  4. in the spring app convert the string into a blob data and save in the database;在 spring 应用程序中将字符串转换为 blob 数据并保存在数据库中;

when you need to read that:当您需要阅读时:

  1. retrieve the blob from the db;从数据库中检索 blob;
  2. convert to the string;转换为字符串;
  3. send to flutter;发送至 flutter;
  4. convert to real image;转换为真实图像;

i usually face up this requirement with this approach and it works really well.我通常用这种方法来面对这个要求,而且效果很好。 i write bellow a simple function from/to blob/string that you can use我在下面写了一个简单的 function 从/到你可以使用的 blob/string

toBlob到Blob

public static Blob toBlob(String s) throws SQLException {
   if (Objects.nonNull(s)) {
      return new SerialBlob(s.getBytes(StandardCharsets.UTF_8));
   } else return null;
}

toString到字符串

public static String toString(Blob b) throws SQLException {
   if (Objects.nonNull(b)) {
     return new String(b.getBytes(1L, (int) b.length()), StandardCharsets.UTF_8);
   } else return null;
}

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

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