Java Spring Boot JPA(Hibernate) 如何保存图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50363639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Spring Boot JPA(Hibernate) saves Images
提问by Manoj Sharma
I just want to simply ask when Spring Boot Web Application's JPA saves Data or BLOB(using @LOB) or byte array data in the Database what is the real form of Saving The Images in Database. is it going to save the Whole byte data in Database or it is going to save only the reference or Address for that byte Array Object and in reality saving it into the System's file Space.
我只想简单地问一下Spring Boot Web Application的JPA何时在数据库中保存Data或BLOB(使用@LOB)或字节数组数据时,在数据库中保存图像的真实形式是什么。它是将整个字节数据保存在数据库中,还是只保存该字节数组对象的引用或地址,实际上将其保存到系统的文件空间中。
I want to ask Specifically for Spring Boot JPA Repository. Please explain it. and if any demo example to Test it out please provide it
我想专门询问 Spring Boot JPA 存储库。请解释一下。如果有任何演示示例来测试它,请提供它
回答by Ahmed Itani
It will save all the bytes in the database it will not export it to a file system and save the directory. You have to specifically do that part in the code.
它将保存数据库中的所有字节,不会将其导出到文件系统并保存目录。您必须在代码中专门执行该部分。
回答by Ph03n1x
Go to this repositoryand go to the display-image-from-db
branch. The basic approach is the following:
转到此存储库并转到display-image-from-db
分支。基本方法如下:
In the entity you have:
@Lob private Byte[] image;
ImageController.java
- you get the image via aMultipartFile
@PostMapping("recipe/{id}/image") public String handleImagePost(@PathVariable String id, @RequestParam("imagefile") MultipartFile file){ imageService.saveImageFile(Long.valueOf(id), file); return "redirect:/recipe/" + id + "/show"; }
Call the
imageService
to save the image passing thefile
as an argument.The service basically copies the image content to a byte array, and finally you assign this byte array to your entity.
@Override @Transactional public void saveImageFile(Long recipeId, MultipartFile file) { try { Recipe recipe = recipeRepository.findById(recipeId).get(); Byte[] byteObjects = new Byte[file.getBytes().length]; int i = 0; for (byte b : file.getBytes()){ byteObjects[i++] = b; } recipe.setImage(byteObjects); recipeRepository.save(recipe); } catch (IOException e) { //todo handle better log.error("Error occurred", e); e.printStackTrace(); } }
在实体中,您拥有:
@Lob private Byte[] image;
ImageController.java
- 你通过a获得图像MultipartFile
@PostMapping("recipe/{id}/image") public String handleImagePost(@PathVariable String id, @RequestParam("imagefile") MultipartFile file){ imageService.saveImageFile(Long.valueOf(id), file); return "redirect:/recipe/" + id + "/show"; }
调用
imageService
以保存将 传递file
为参数的图像。该服务基本上将图像内容复制到一个字节数组中,最后您将这个字节数组分配给您的实体。
@Override @Transactional public void saveImageFile(Long recipeId, MultipartFile file) { try { Recipe recipe = recipeRepository.findById(recipeId).get(); Byte[] byteObjects = new Byte[file.getBytes().length]; int i = 0; for (byte b : file.getBytes()){ byteObjects[i++] = b; } recipe.setImage(byteObjects); recipeRepository.save(recipe); } catch (IOException e) { //todo handle better log.error("Error occurred", e); e.printStackTrace(); } }
For the full source code go to the repo, that will definitely help. However I strongly suggest storing the files on the disk and not in the DB. The DB should only store the path to the files. For such solution here is an example: link
对于完整的源代码,请访问 repo,这肯定会有所帮助。但是我强烈建议将文件存储在磁盘上而不是数据库中。DB 应该只存储文件的路径。对于这样的解决方案,这里有一个例子:链接
回答by Paul Warren
To answer the question directly the content is stored in the database. This may, or may not work for you. As @dgarceran mentions there are a bunch of pros and cons. Either way I would recommend that you take a look at Spring Content.
为了直接回答问题,内容存储在数据库中。这可能适用,也可能不适用于您。正如@dgarceran 提到的,有很多优点和缺点。无论哪种方式,我都建议您查看Spring Content。
This project provides an abstraction for content/BLOBs. It is to unstructured data, what Spring Data is to structured data. It has modules that support JPA (BLOBs), Filesystem, Mongo's GridFS and S3. Regardless of the module you choose it will save you from having to write any of that boilerplate code like that provided by @Ph03n1x.
该项目为内容/BLOB 提供了抽象。它是非结构化数据,Spring Data 是结构化数据。它具有支持 JPA (BLOB)、文件系统、Mongo 的 GridFS 和 S3 的模块。无论您选择哪个模块,它都会使您不必编写任何由@Ph03n1x 提供的样板代码。
With Spring Content all you need to do is create an a ContentStore
interface and Spring Content will provide the implementation and controller for you. The implementation is efficient as it will stream content between client and database (rather than load the entire file into memory as Ph03n1x's example does). The abstraction also makes it easy to change the storage model later, should you need to.
使用 Spring Content,您需要做的就是创建一个ContentStore
接口,Spring Content 将为您提供实现和控制器。该实现是高效的,因为它将在客户端和数据库之间传输内容(而不是像 Ph03n1x 的示例那样将整个文件加载到内存中)。如果您需要,抽象还使以后更改存储模型变得容易。
There is a getting started guides hereand a video tutorial here.
HTH
HTH