创建spring boot项目
pom依赖
<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.17.4</version></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.1</version></dependency><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1.1</version></dependency><!-- no more than 2.3.3--><dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId><version>2.3.3</version></dependency>
yaml配置文件
application.yaml
server:port: 8089
spring:profiles:active: dev
alioss:endpoint: ${alioss.endpoint}accessKeyId: ${alioss.accessKeyId}accessKeySecret: ${alioss.accessKeySecret}bucketName: ${alioss.bucketName}
application-dev.yaml
alioss:endpoint: oss-cn-beijing.aliyuncs.comaccessKeyId: 写自己的keyIdaccessKeySecret: 写自己的密钥bucketName: 写自己的bucketName
文件上传工具类
package com.pkq.demo.utils;import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;/*** ClassName: AliOSSUtils* Package: com.pkq.demo.utils* Description:** @Author pkq* @Create 2024-07-05 15:09* @Version 1.0*/
@Component
@Slf4j
public class AliOSSUtils {@Value("${alioss.endpoint}")private String endpoint;@Value("${alioss.accessKeyId}")private String accessKeyId;@Value("${alioss.accessKeySecret}")private String accessKeySecret;@Value("${alioss.bucketName}")private String bucketName;/*** 实现上传图片到OSS*/public String upload(MultipartFile multipartFile) throws IOException {// 获取上传的文件的输入流InputStream inputStream = multipartFile.getInputStream();// 避免文件覆盖String originalFilename = multipartFile.getOriginalFilename();String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));//上传文件到 OSSOSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);ossClient.putObject(bucketName, fileName, inputStream);//文件访问路径String url ="https://" + bucketName + "." + endpoint + "/" + fileName;// 关闭ossClientossClient.shutdown();return url;// 把上传到oss的路径返回}
}
通用返回结果类
package com.pkq.demo.result;import lombok.Data;
import lombok.NoArgsConstructor;/*** ClassName: Result* Package: com.pkq.demo.result* Description:** @Author pkq* @Create 2024-07-05 15:23* @Version 1.0*/
@Data
@NoArgsConstructor
public class Result<T> {private Integer code;private String message;private T data;public Result(T data) {this(200,null,data);}public Result(int code, String message) {this(code,message,null);}public Result(int code, String message,T data) {this.code = code;this.message = message;this.data = data;}public static <T> Result<T> success(T data) {return new Result<>(data);}public static <T> Result<T> fail(String message) {return new Result<>(500, message);}public static <T> Result<T> fail(int code, String message) {return new Result<>(code, message);}
}
文件上传接口
package com.pkq.demo.controller;import com.pkq.demo.result.Result;
import com.pkq.demo.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;/*** ClassName: UploadController* Package: com.pkq.demo.controller* Description:** @Author pkq* @Create 2024-07-05 15:27* @Version 1.0*/
@Slf4j
@RestController
public class UploadController {@Autowiredprivate AliOSSUtils aliOSSUtils;@PostMapping("/upload")public Result<String> upload(MultipartFile image) throws IOException {//调用阿里云OSS工具类,将上传上来的文件存入阿里云String url = aliOSSUtils.upload(image);//将图片上传完成后的url返回,用于浏览器回显展示return Result.success(url);}}