vue3+springboot+minio,实现文件上传功能

前端:Vue 3

安装依赖

首先,确保你已经安装了Vue CLI,然后创建一个新的Vue项目:

```bash
npm install -g @vue/cli
vue create my-project
cd my-project
```

安装axios用于发送HTTP请求:

```bash
npm install axios
```

创建文件上传组件

src/components目录下创建一个FileUpload.vue组件:

<template>
  <div>
    <input type="file" @change="handleFileUpload" />
    <button @click="submitFile">Upload</button>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      file: null,
    };
  },
  methods: {
    handleFileUpload(event) {
      this.file = event.target.files[0];
    },
    async submitFile() {
      const formData = new FormData();
      formData.append('file', this.file);

      try {
        const response = await axios.post('http://localhost:8080/upload', formData, {
          headers: {
            'Content-Type': 'multipart/form-data',
          },
        });
        console.log('File uploaded successfully', response.data);
      } catch (error) {
        console.error('Error uploading file', error);
      }
    },
  },
};
</script>

后端:Spring Boot

添加依赖

在你的pom.xml文件中添加以下依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>io.minio</groupId>
        <artifactId>minio</artifactId>
        <version>8.3.4</version>
    </dependency>
    <!-- 其他依赖 -->
</dependencies>

配置MinIO

application.properties中添加MinIO的配置:

```properties
minio.url=http://localhost:9000
minio.access-key=minioadmin
minio.secret-key=minioadmin
minio.bucket-name=mybucket
```

创建配置类

创建一个配置类用于读取配置:

```java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.minio.MinioClient;

@Configuration
public class MinioConfig {
@Value(“${minio.url}”)
private String minioUrl;

@Value("${minio.access-key}")
private String accessKey;

@Value("${minio.secret-key}")
private String secretKey;

@Bean
public MinioClient minioClient() {
    return MinioClient.builder()
            .endpoint(minioUrl)
            .credentials(accessKey, secretKey)
            .build();
}

}
```

创建文件上传控制器

创建一个控制器类用于处理文件上传请求:

```java
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping(“/upload”)
public class FileUploadController {

@Autowired
private MinioClient minioClient;

@Value("${minio.bucket-name}")
private String bucketName;

@PostMapping
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
    try {
        minioClient.putObject(
            PutObjectArgs.builder()
                .bucket(bucketName)
                .object(file.getOriginalFilename())
                .stream(file.getInputStream(), file.getSize(), -1)
                .contentType(file.getContentType())
                .build()
        );
        return ResponseEntity.ok("File uploaded successfully");
    } catch (Exception e) {
        e.printStackTrace();
        return ResponseEntity.status(500).body("Error uploading file");
    }
}

}
```

启动项目

  1. 启动Spring Boot应用:在项目根目录下运行mvn spring-boot:run
  2. 启动Vue项目:在Vue项目根目录下运行npm run serve

完成

现在,你应该可以通过Vue 3前端选择文件并上传到后端Spring Boot,然后存储到MinIO中了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天进步2015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值