In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to use Springboot to integrate GridFS to achieve file operation". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Catalogue
GridFsOperations to upload, download and delete GridFS files
Implementation of upload, download and deletion function
test
Upload
download
Delete
GridFsOperations to upload, download and delete GridFS files
Recently learn GridFS, want to use it to integrate springboot to get a file upload and download.
A lot of information found on the Internet is achieved by using GridFsTemplate and GridFSBucket, which requires a variety of additional configurations of Bean. But looking at the official documentation of spring-data-mongodb, as well as the sample code, they only use GridFsOperations and do not need any other configuration.
Then use GridFsOperations to write a file upload and download demo, it is still very convenient to use, give you a reference.
Implementation of upload, download and deletion function
Pom.xml
Org.springframework.boot spring-boot-starter-data-mongodb
Application.properties
# File upload / download configuration spring.servlet.multipart.max-file-size=1024MBspring.servlet.multipart.max-request-size=1024MB
FileController
Package com.example.tryRedis.controller;import static org.springframework.data.mongodb.core.query.Query.*;import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;import com.mongodb.client.gridfs.model.GridFSFile;import io.swagger.v3.oas.annotations.Parameter;import org.bson.types.ObjectId;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.mongodb.gridfs.GridFsOperations;import org.springframework.data.mongodb.gridfs.GridFsResource;import org.springframework.http.MediaType Import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletResponse;import java.io.*;import java.util.HashMap;import java.util.Map;@RestController@RequestMapping ("/ file") public class FileController {@ Autowired GridFsOperations gridFsOperations / / upload file @ PostMapping (value = "/ upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public Map upload (@ Parameter @ RequestPart (value = "file") MultipartFile file) {/ / start time long begin = System.nanoTime (); Map map = new HashMap (); try {InputStream streamForUpload = file.getInputStream () ObjectId objectId = gridFsOperations.store (streamForUpload,file.getOriginalFilename (), file.getContentType ()); / / end of upload long end = System.nanoTime (); long time = end-begin; System.out.println ("this upload takes a total time:" + time); System.out.println ("upload succeeded! File name: "+ file.getOriginalFilename () +". File ID: "+ objectId); map.put (file.getOriginalFilename (), objectId);} catch (Exception e) {e.printStackTrace ();} return map;} / / query and download the file @ GetMapping (" / download ") public String download (String filename, HttpServletResponse response) throws IOException {/ / start time long begin = System.nanoTime () / / query file GridFSFile result = gridFsOperations.findOne (query (whereFilename (). Is (filename)); GridFsResource gridFsResource= gridFsOperations.getResource (result); String contentType = gridFsResource.getContentType (); System.out.println ("contentType:" + contentType); System.out.println ("filename:" + gridFsResource.getFilename ()); response.reset (); response.setContentType (contentType) / Note: if header is not set in the following line, the contents of the file will be output directly on the page as the response body instead of the download file response.setHeader ("Content-Disposition", "attachment;filename=" + filename); / / specify the download file name ServletOutputStream outputStream = response.getOutputStream (); InputStream is = gridFsResource.getInputStream (); byte [] bytes = new byte [1024] Int len= 0; while ((len=is.read (bytes))! =-1) {outputStream.write (bytes,0,len);} is.close (); outputStream.close (); / / download ends long end = System.nanoTime (); long time = end-begin; System.out.println ("this download takes time:" + time) Return contentType;} @ DeleteMapping ("/ delete") public String deleteFile (@ Parameter @ RequestParam ("filename") String filename) {gridFsOperations.delete (query (whereFilename (). Is (filename)); return "delete success";}} Test upload
download
Click download in the red circle to download it. Or type localhost:8080/file/download?filename=todo.txt directly in the address bar or download the file directly (the todo.txt here is the file I tested, fill in the name of the file you uploaded, don't forget to add the suffix! )
Delete
When you test the upload and delete functions above, you can also take a look at mongodb's database.
This is the end of the content of "how to use Springboot to integrate GridFS to achieve file operation". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.