Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How SpringBoot integrates minio

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how SpringBoot integrates minio". The editor shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how SpringBoot integrates minio" can help you solve the problem.

First add the dependency of Minio

Io.minio minio 3.0.10

Then write a controller class

This is just a simple demo, without any encapsulation, can be encapsulated according to the actual situation.

Package com.file.server.controller;import io.minio.MinioClient;import org.apache.tomcat.util.http.fileupload.IOUtils;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse;import java.io.InputStream;@RestControllerpublic class MinioController {private static String url = "http://127.0.0.1:9000"; / / IP port of the minio service private static String accessKey =" W2ZWITFFDWFM5TWS3WI9 "; private static String secretKey =" dNx++XsRJpjmWVQHWv8djMCFJ0A3YXbEr4qfKHR+ " / / upload files to the minio service @ PostMapping ("upload") public String upload (@ RequestParam ("fileName") MultipartFile file) {try {MinioClient minioClient = new MinioClient (url, accessKey, secretKey); InputStream is= file.getInputStream (); / / get the file stream String fileName = file.getOriginalFilename (); / / File name String contentType = file.getContentType (); / / Type minioClient.putObject ("file", fileName,is,contentType) / / put the file in the Minio bucket (folder) return "upload successful";} catch (Exception e) {return "upload failed";}} / download the file of the minio service @ GetMapping ("download") public String download (HttpServletResponse response) {try {MinioClient minioClient = new MinioClient (url, accessKey, secretKey) InputStream fileInputStream = minioClient.getObject ("file", "test.jpg"); response.setHeader ("Content-Disposition", "attachment;filename=" + "test.jpg"); response.setContentType ("application/force-download"); response.setCharacterEncoding ("UTF-8"); IOUtils.copy (fileInputStream,response.getOutputStream ()); return "download complete" } catch (Exception e) {return "download failed";}} / / obtain the download address of the minio file @ GetMapping ("url") public String getUrl () {try {MinioClient minioClient = new MinioClient (url, accessKey, secretKey); String url = minioClient.presignedGetObject ("file", "test.jpg"); return url } catch (Exception e) {return "failed to get";} this is the end of the introduction on "how SpringBoot integrates minio". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report