第一步:准备好需要的必要参数,(我是在application.properties中配置的)
aliyun.oss.endpoint=oss-cn-shenzhen.aliyuncs.com
aliyun.oss.accessKeyId=LTAI5tRCNVNaTDQmDmBYjdi7
aliyun.oss.accessKeySecret=KNeq5AzJ4SwwrEAVvedbMX2uSuLvoY
aliyun.oss.bucketName=vgcsgo
第二步:接下来配置参数交由spring管理,并编写上传方法
import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.ObjectMetadata; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; @Configuration public class AliyunConfiguration { @Value("${aliyun.oss.endpoint}") private String endpoint; @Value("${aliyun.oss.accessKeyId}") private String accessKeyId; @Value("${aliyun.oss.accessKeySecret}") private String accessKeySecret; @Value("${aliyun.oss.bucketName}") private String bucketName; @Bean public OSS ossClient(){ return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); } public String uploadAliyun(MultipartFile file, String fileName) throws IOException { //外面获取文件输入流,最后方便关闭 InputStream in = file.getInputStream(); try { //2 创建OssClient对象 OSS ossClient =new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret); //3 获取文件信息,为了上传 // meta设置请求头 ObjectMetadata meta = new ObjectMetadata(); meta.setContentType("image/jpg"); //4 设置知道文件夹 ossClient.putObject(bucketName,fileName,in, meta); //5 关闭ossClient ossClient.shutdown(); //6 返回上传之后地址,拼接地址 String uploadUrl = "https://"+bucketName+"."+endpoint+"/"+fileName; return uploadUrl; }catch(Exception e) { e.printStackTrace(); return null; }finally { in.close(); } } }
最后一步:上传接口
import com.example.csgo.util.AliyunConfiguration; import com.example.csgo.util.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; @RestController @RequestMapping("/ali/upload") public class UploadCommon { @Autowired private AliyunConfiguration aliyunConfiguration; /** 上传文件 **/ @PostMapping("/file") public Result uploadFile(MultipartFile file) throws Exception { try { SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); // 生成文件名称 String nameSuffix = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")) .replaceAll(" ", "_").replaceAll(",", "") + format.format(new Date()) + new Random().nextInt(1000); //上传原始图片到阿里云 String uploadPath = aliyunConfiguration.uploadAliyun(file,nameSuffix); System.out.println(uploadPath); return new Result().result(uploadPath); } catch (IOException e) { throw new IOException("文件流异常"); } } }
需要的pom文件
<dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.1.0</version> </dependency>
可直接直接复制代码使用