springboot 如何将图片上传至阿里云OSS并存入mysql
时间: 2024-06-08 09:08:26 浏览: 18
将图片上传至阿里云OSS并存入MySQL可以分为以下几个步骤:
1. 添加阿里云OSS依赖
```xml
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
```
2. 配置阿里云OSS参数
```java
@Configuration
public class OSSConfig {
@Value("${spring.aliyun.oss.access-key-id}")
private String accessKeyId;
@Value("${spring.aliyun.oss.access-key-secret}")
private String accessKeySecret;
@Value("${spring.aliyun.oss.endpoint}")
private String endpoint;
@Value("${spring.aliyun.oss.bucket-name}")
private String bucketName;
@Bean
public OSSClient ossClient() {
return new OSSClient(endpoint, accessKeyId, accessKeySecret);
}
@Bean
public OSSProperties ossProperties() {
return new OSSProperties(accessKeyId, accessKeySecret, endpoint, bucketName);
}
}
```
3. 编写上传图片的服务类
```java
@Service
public class ImageService {
private final OSSClient ossClient;
private final OSSProperties ossProperties;
@Autowired
public ImageService(OSSClient ossClient, OSSProperties ossProperties) {
this.ossClient = ossClient;
this.ossProperties = ossProperties;
}
public String uploadImage(MultipartFile file) throws Exception {
String filename = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
String key = UUID.randomUUID().toString() + "-" + filename;
ossClient.putObject(ossProperties.getBucketName(), key, inputStream);
return key;
}
}
```
4. 将上传的图片信息存入MySQL
```java
@Service
public class ImageInfoService {
private final ImageInfoRepository imageInfoRepository;
@Autowired
public ImageInfoService(ImageInfoRepository imageInfoRepository) {
this.imageInfoRepository = imageInfoRepository;
}
public void saveImageInfo(String filename, String key) {
ImageInfo imageInfo = new ImageInfo();
imageInfo.setFilename(filename);
imageInfo.setKey(key);
imageInfoRepository.save(imageInfo);
}
}
```
5. 控制器中调用上传图片的服务类和存入MySQL的服务类
```java
@RestController
@RequestMapping("/image")
public class ImageController {
private final ImageService imageService;
private final ImageInfoService imageInfoService;
@Autowired
public ImageController(ImageService imageService, ImageInfoService imageInfoService) {
this.imageService = imageService;
this.imageInfoService = imageInfoService;
}
@PostMapping("/upload")
public Result uploadImage(@RequestParam("file") MultipartFile file) {
try {
String key = imageService.uploadImage(file);
imageInfoService.saveImageInfo(file.getOriginalFilename(), key);
return Result.success("上传成功");
} catch (Exception e) {
e.printStackTrace();
return Result.error("上传失败");
}
}
}
```
以上就是将图片上传至阿里云OSS并存入MySQL的实现过程。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)