简体   繁体   中英

How Spring Boot JPA(Hibernate) saves Images

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.

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

Go to this repository and go to the display-image-from-db branch. The basic approach is the following:

  • In the entity you have:

     @Lob private Byte[] image; 
  • ImageController.java - you get the image via 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"; } 
  • Call the imageService to save the image passing the file 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(); } } 

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

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.

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 .

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.

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.

There is a getting started guides here and a video tutorial here .

HTH

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