[英]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第一个选项
Flutter
side, take the image from the userFlutter
端,取用户的图像Flutter
side, convert the image to the BASE64
string format.Flutter
端,将图像转换为BASE64
字符串格式。Flutter
side, POST
it as a JSON
object to backend.Flutter
端,将其作为POST
JSON
发布到后端。Spring Boot
side, get the BASE64
string and store it to the PostgreSQL
db.Spring Boot
端,获取BASE64
字符串并将其存储到PostgreSQL
db。 Second Option第二种选择
Flutter
side, take the image from the userFlutter
端,取用户的图像Flutter
side, convert the image to the BASE64
string format.Flutter
端,将图像转换为BASE64
字符串格式。Flutter
side, POST
it as a JSON
object to backend.Flutter
端,将其作为POST
JSON
发布到后端。Spring Boot
side, get the BASE64
string and convert it to the real image file;Spring Boot
端,获取BASE64
字符串并转换为真实镜像文件;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) 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:我觉得你应该:
when you need to read that:当您需要阅读时:
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.