Preface
Here is a complete example showing how to upload and download files using MinIO and store file information in the databasefile
surface. We will use the Spring Boot framework to implement this functionality.
Project structure
src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ ├── controller
│ │ │ └──
│ │ ├── service
│ │ │ ├──
│ │ │ └──
│ │ ├── repository
│ │ │ └──
│ │ ├── model
│ │ │ └──
│ │ ├── config
│ │ │ └──
│ │ └──
│ └── resources
│ └──
1. Add dependencies
existAdd necessary dependencies to
<dependencies> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>minio</artifactId> <version>8.3.0</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies>
2. Configure MinIO
existAdd MinIO configuration information to:
=http://localhost:9000 -key=minioadmin -key=minioadmin -name=mybucket =jdbc:h2:mem:testdb =org. =sa =password -platform=.H2Dialect
3. Create MinioConfig
package ; import ; import ; import ; import ; @Configuration public class MinioConfig { @Value("${}") private String minioUrl; @Value("${-key}") private String minioAccessKey; @Value("${-key}") private String minioSecretKey; @Bean public MinioClient minioClient() { return () .endpoint(minioUrl) .credentials(minioAccessKey, minioSecretKey) .build(); } }
4. Create FileEntity
package ; import ; import ; import ; import ; @Entity public class FileEntity { @Id @GeneratedValue(strategy = ) private Long id; private String fileName; private String fileUrl; // Getters and Setters }
5. Create FileRepository
package ; import ; import ; public interface FileRepository extends JpaRepository<FileEntity, Long> { }
6. Create FileService
package ; import ; import ; import ; public interface FileService { FileEntity uploadFile(MultipartFile file) throws IOException; byte[] downloadFile(String fileName) throws Exception; }
7. Create FileServiceImpl
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; @Service public class FileServiceImpl implements FileService { @Autowired private MinioClient minioClient; @Autowired private FileRepository fileRepository; @Value("${-name}") private String bucketName; @Override public FileEntity uploadFile(MultipartFile file) throws IOException { String fileName = (); try { ( bucketName, fileName, (), () ); String fileUrl = (bucketName, fileName); FileEntity fileEntity = new FileEntity(); (fileName); (fileUrl); return (fileEntity); } catch (MinioException e) { throw new IOException("Error occurred while uploading file to MinIO", e); } } @Override public byte[] downloadFile(String fileName) throws Exception { try (InputStream stream = (bucketName, fileName); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { byte[] buffer = new byte[1024]; int length; while ((length = (buffer)) != -1) { (buffer, 0, length); } return (); } catch (MinioException e) { throw new Exception("Error occurred while downloading file from MinIO", e); } } }
8. Create FileController
package ; import ; import ; import ; import ; import ; import ; import .*; import ; import ; @RestController @RequestMapping("/files") public class FileController { @Autowired private FileService fileService; @PostMapping("/upload") public ResponseEntity<FileEntity> uploadFile(@RequestParam("file") MultipartFile file) throws IOException { FileEntity fileEntity = (file); return (fileEntity); } @GetMapping("/download/{fileName}") public ResponseEntity<byte[]> downloadFile(@PathVariable String fileName) throws Exception { byte[] data = (fileName); return () .contentType(MediaType.APPLICATION_OCTET_STREAM) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"") .body(data); } }
9. Main application class
package ; import ; import ; @SpringBootApplication public class Application { public static void main(String[] args) { (, args); } }
Summarize
Through the above steps, we created a complete Spring Boot application that uses MinIO to upload and download files and store file information in the databasefile
surface. You can pass/files/upload
Upload files on the interface, through/files/download/{fileName}
Interface download file.
This is the article about uploading and downloading files with minio in Java. For more related Java uploading and downloading files with minio, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!