Ali cloud official document has been doing very detailed, address below, we strongly recommend a look at the official documentation
https://help.aliyun.com/document_detail/84781.html?spm=a2c4g.11186623.6.788.32037815QD8fgC
step1
First, fill in their respective configuration information application.yml configuration file (the configuration information can be viewed in the console aliyun official website)
Introduced in the respective dependent file pom
<dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.8.0</version> </dependency>
step2
Creation tools, configuration information (this step can be skipped, directly in the service layer in) application.yml documents through the tools, configuration information is defined as a static variable, it can be directly through the "class name." The way to get
. 1 Package com.atzjhydx.oss.utils; 2 . 3 Import the org.springframework.beans.factory.InitializingBean; . 4 Import org.springframework.beans.factory.annotation.Value; . 5 Import org.springframework.stereotype.Component; . 6 . 7 @ the Component 8 // InitializingBean role is to perform customized operations after bean initialization. . 9 public class ConstantPropertiesUtils the implements the InitializingBean { 10 . 11 @Value ( "Aliyun.aFile.accessKeyId $ {}" ) 12 is Private String accessKeyId; 13 is 14 @Value("${Aliyun.aFile.accessKeySecret}") 15 private String accessKeySecret; 16 17 @Value("${Aliyun.aFile.endpoint}") 18 private String endpoint; 19 20 @Value("${Aliyun.aFile.bucketName}") 21 private String bucketName; 22 23 public static String END_POINT; 24 public static String BUCKET_NAME; 25 public static String ACCESS_KEY_ID; 26 public static String ACCESS_KEY_SECRET; 27 28 @Override 29 public void afterPropertiesSet() throws Exception { 30 END_POINT = endpoint; 31 BUCKET_NAME = bucketName; 32 ACCESS_KEY_ID = accessKeyId; 33 ACCESS_KEY_SECRET = accessKeySecret; 34 } 35 }
step3
service layer in the achievement of specific upload function
1 package com.atzjhydx.oss.service.impl; 2 3 import com.aliyun.oss.HttpMethod; 4 import com.aliyun.oss.OSS; 5 import com.aliyun.oss.OSSClientBuilder; 6 import com.aliyun.oss.model.GeneratePresignedUrlRequest; 7 import com.atzjhydx.oss.service.OssService; 8 import com.atzjhydx.oss.utils.ConstantPropertiesUtils; 9 import org.joda.time.DateTime; 10 import org.springframework.stereotype.Service; 11 import org.springframework.web.multipart.MultipartFile; 12 is 13 is Import a java.io.InputStream; 14 Import the java.net.URL; 15 Import java.util.Date; 16 Import java.util.UUID; . 17 18 is @Service . 19 public class OssServiceImpl the implements OssService { 20 is @Override 21 is public String uploadFileAvatar (a MultipartFile file) { 22 is 23 is // acquired connection configuration profile (via custom tools acquired) 24 String Endpoint = ConstantPropertiesUtils.END_POINT; 25 String = accessKeyIdConstantPropertiesUtils.ACCESS_KEY_ID; 26 is String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET; 27 String bucketName = ConstantPropertiesUtils.BUCKET_NAME; 28 29 the OSS ossClient = null ; 30 31 is the try { 32 // Create instance oss 33 is ossClient = new new . OSSClientBuilder () Build (Endpoint, accessKeyId , accessKeySecret); 34 is 35 // Get upload file input stream 36 the inputStream inputStream = file.getInputStream (); 37 [ 38 is //Acquiring the file name (using the existing file uuid avoid coverage problems) 39 String OriginalFilename = file.getOriginalFilename (); 40 String uuid = UUID.randomUUID () toString () the replaceAll ( "-", ".". ); 41 is String fileName UUID OriginalFilename + = ; 42 is // the file classified by date (date tools using joda-time, may be used java own tools) 43 is String = dataPath new new the DateTime () toString ( "YYYY / the MM. / dd " ); 44 is // file date constituting the file name and the folder path merge 45 fileName = dataPath +" / "+ fileName; 46 is 47 // calls the method to upload oss 48 @Bucket first parameter name 49 // second parameter upload a file path and file name oss A / B / C / 123.jpg 50 // third parameter upload file input stream 51 is ossClient.putObject (bucketName, fileName, inputStream); 52 is 53 is // URL returned set period of 10 years 54 is a Date expiration = new new a Date ( new new a Date () the getTime () + 10 * 365 * 24 * 60 * 60 * 1000. ); 55 GeneratePresignedUrlRequest REQ = new new GeneratePresignedUrlRequest (bucketName, fileName, HttpMethod.GET); 56 is req.setExpiration (expiration); 57 is 58 // echo URL 59 URL url = ossClient.generatePresignedUrl(req); 60 return url.toString(); 61 62 }catch (Exception e){ 63 e.printStackTrace(); 64 return null; 65 }finally { 66 if (ossClient!=null){ 67 ossClient.shutdown(); 68 } 69 } 70 } 71 }
Controller layer, url information back to the front page
1 package com.atzjhydx.oss.controller; 2 3 import com.atzjhydx.commonutils.R; 4 import com.atzjhydx.oss.service.OssService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.CrossOrigin; 7 import org.springframework.web.bind.annotation.PostMapping; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController; 10 importorg.springframework.web.multipart.MultipartFile; . 11 12 is @RestController 13 is @RequestMapping ( "/ eduoss / fileoss" ) 14 @CrossOrigin 15 public class OssController { 16 . 17 @Autowired 18 is Private OssService ossService; . 19 20 is // upload an avatar method 21 is @PostMapping 22 is public R & lt uploadOssFile (a MultipartFile file) { 23 is 24 // Get upload 25 // returns upload oss path 26 is String URL = ossService.uploadFileAvatar(file); 27 28 return R.ok().data("url",url); 29 } 30 }